LockSupport与线程中断
一、线程中断机制
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唤醒
①.是什么?
②.主要方法
③:代码
④:重点说明(重要)