线程等待唤醒机制(LockSupport) 与 线程中断(interrupt)


一、线程中断机制

1.什么是中断机制

2.中断的相关API方法之三大方法说明




3.大厂面试题中断机制考点

①:如何停止中断运行中的线程?

通过一个volatile 变量实现

public class InterruptDemo {
    static volatile  boolean isStop=false;
    public static void main(String[] args) {
        new Thread(()->{
            while (true){
                if (isStop){
                    System.out.println(Thread.currentThread().getName()+"\t isStop被修改为true,程序停止");
                    break;
                }
                System.out.println("--- hello volatile");
            }
        },"t1").start();

        //暂停毫秒
        try{ TimeUnit.MILLISECONDS.sleep(1); }catch (InterruptedException e){ e.printStackTrace(); }

        new Thread(()->{
            isStop=true;
        },"t2").start();

    }
}

运行结果

--- hello volatile
--- hello volatile
--- hello volatile
--- hello volatile
--- hello volatile
--- hello volatile
t1	 isStop被修改为true,程序停止

通过AtomicBoolean

public class InterruptDemo {
    static AtomicBoolean isStop=new AtomicBoolean(false);
    public static void main(String[] args) {
        new Thread(()->{
            while (true){
                if (isStop.get()){
                    System.out.println(Thread.currentThread().getName()+"\t isStop被修改为true,程序停止");
                    break;
                }
                System.out.println("--- hello volatile");
            }
        },"t1").start();

        //暂停毫秒
        try{ TimeUnit.MILLISECONDS.sleep(1); }catch (InterruptedException e){ e.printStackTrace(); }

        new Thread(()->{
            isStop.set(true);
        },"t2").start();

    }
}

运行结果

--- hello volatile
--- hello volatile
--- hello volatile
t1	 isStop被修改为true,程序停止

通过Thread类自带的中断api实例方法实现

public class InterruptDemo {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "\t isStop被修改为true,程序停止");
                    break;
                }
                System.out.println("--- hello Interrupt api");
            }
        }, "t1");

        t1.start();

        //暂停毫秒
        try{ TimeUnit.MILLISECONDS.sleep(1); }catch (InterruptedException e){ e.printStackTrace(); }

		//t2向t1发出协商,将t1的中断标志位设为true希望t1停下来
        new Thread(()->{
            t1.interrupt();
        },"t2").start();

    }
}

运行结果

--- hello Interrupt api
--- hello Interrupt api
--- hello Interrupt api
--- hello Interrupt api
t1	 isStop被修改为true,程序停止


②:当前线程的中断标识为true,是不是线程就立刻停止?

中断只是一种协商机制,修改中断标识位仅此而已,不是立刻stop打断

public class InterruptDemo {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "\t isStop被修改为true,程序停止");
                    break;
                }

                //暂停毫秒
                try{ TimeUnit.MILLISECONDS.sleep(200); }catch (InterruptedException e){
                    //Thread.currentThread().interrupt();
                    //没有它,程序不会停止,中断不打断,会无限循环
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
                System.out.println("--- hello Interrupt api");
            }
        }, "t1");

        t1.start();

        //暂停毫秒
        try{ TimeUnit.MILLISECONDS.sleep(2); }catch (InterruptedException e){ e.printStackTrace(); }

        new Thread(()->{
            t1.interrupt();
        },"t2").start();

    }
}



③:静态方法Thread.interrupted(),谈谈你的理解?


都会返回中断状态,两者对比


二、LockSupport

1.LockSupport是什么?(LockSupport是线程等待唤醒机制的加强版)



三、线程等待唤醒机制(LockSupport是线程等待唤醒机制的加强版)


1. 3种让线程等待和唤醒的方法

2.Object类中的wait和notify方法实现线程等待和唤醒



3.Condition接口中的await和signal方法实现线程的等待和唤醒


4.上述两个对象Object和Condition使用的限制条件


5.LockSupport类中的park等待和unpark唤醒


①.是什么?

②.主要方法


③:代码




④:重点说明(重要)



⑤:面试题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值