线程休眠、线程停止、线程礼让、线程插队

本文介绍了Java中线程的四种控制方式:线程休眠通过Thread.sleep()实现,例如在抢票场景中控制线程行为;线程停止不推荐使用stop方法,建议使用标志变量控制循环;线程礼让通过Thread.yield()让当前线程变为就绪状态,但并不保证一定能礼让成功;线程插队则涉及线程的阻塞和调度,使得其他线程能够插入执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程休眠

采用Thread.sleep()
一个抢票的例子,三个线程同时访问一个静态资源,让票数递减,我们单独让一个线程休眠,比如500毫秒。

public class ThreadSleep {

    //把票数设置为唯一
    private static int stick=10;

    public static void main(String[] args) {

        //lambda表达式实现Runnable接口,并从写run()方法;
        Runnable runnable=()->{

            //如果当前线程是小明则线程睡眠5毫秒
            try {
                if(Thread.currentThread().getName().equals("小明")){
                    Thread.sleep(5);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //让票数递减
            while (true){
                if(stick<=0){
                    break;
                }
                System.out.println(Thread.currentThread().getName()+"抢到了第"+stick--);
            }

        };

        new Thread(runnable,"小明").start();
        new Thread(runnable,"小红").start();
        new Thread(runnable,"黄牛").start();

    }
}

线程停止

JDK不推荐我们直接使用,stop方法使线程停止。我们可以立一个Boolean类型的flag,使用while(flag){…}。改变flag的值来时方法暂停。

//1、建议线程正常停止--->利用次数,不建议死循环
//2、建议使用标志位---->设置一个标志
//3、不建议使用stop或者destroy等过时方法
public class ThreadStop implements Runnable{
    //1、设置标志位
    private boolean flag=true;
    @Override
    public void run() {
        int i=0;
        while (flag){
            System.out.println("分线程当前次数"+i++);
        }

    }

    //令标志位为false;
    public Boolean stop(){
        return flag=false;
    }

    public static void main(String[] args) {
        ThreadStop threadStop = new ThreadStop();
        new Thread(threadStop).start();
        for (int i = 0; i < 100; i++) {

            System.out.println("主线程当前运行次数:"+i);
            if (i==80){
                threadStop.stop();//调用stop-->令标志位为false
                System.out.println("线程停止");
            }

        }
    }
}

线程礼让

所谓线程礼让,就是让当前线程停止,但不阻塞,让线程从运行态转为就绪状态,等待CUP的重新调度。礼让别人不一定成功,因为转为就绪状态后,可能CUP下一个还调用你。
方法:Thread.yield();

public class ThreadYield implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始");
        Thread.yield();//线程礼让方法
        System.out.println(Thread.currentThread().getName()+"线程结束");
    }

    public static void main(String[] args) {
        new Thread(new ThreadYield(),"A").start();
        new Thread(new ThreadYield(),"B").start();
    }
}

线程插队

说白了就是,你运行到一半,突然有一个人插进来,等他运行完了你再运行。当中你会进入阻塞状态。

//线程插队,join方法
//分线程插队之后,其他线程进入阻塞状态,就等插队线程执行完,其他线程再执行
public class ThreadJoin {

    public static void main(String[] args) throws InterruptedException {

        //lambda表达式实现Runnable接口,重写run()方法
        Runnable runnable=()->{

            //让分线程先睡1秒
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < 300; i++) {
                System.out.println("VIP来了"+i);
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();

        //主线程
        int i=0;
        for (i = 0; i < 200; i++) {
            System.out.println("main"+i);
            if (i==100){
                thread.join();//在i==100时分线程插队,
            }
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值