Thread.interrupt()
打断线程 sleep ,wait,join的线程
打断sleep()的线程,会清空打断状态,以sleep()为例
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18@Slf4j(topic = "juc.test9") public class test9 { public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(()->{ try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } },"t1"); t1.start(); t1.sleep(500); t1.interrupt(); log.debug("打断状态:{}",t1.isInterrupted()); } } 运行结果:14:38:17.841 [main] DEBUG juc.test9 - 打断状态:false、
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package juc; import lombok.extern.slf4j.Slf4j; @Slf4j(topic = "juc.test10") public class test10 { public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(()->{ while (true){ boolean interrupted = Thread.currentThread().isInterrupted(); if (interrupted){ log.debug("被打断了,退出循环"); break; } } },"t1"); t1.start(); Thread.sleep(1000); log.debug("interript"); t1.interrupt(); } }
复制代码
1
2
316:40:34.133 [main] DEBUG juc.test10 - interript 16:40:34.143 [t1] DEBUG juc.test10 - 被打断了,退出循环
使用线程stop()方法停止线程
stop()方法没有真正杀死线程,如果这时线程锁住了共享资源,那么当它本杀死后就再也没有机会释放了,其他线程永远无法获取锁
使用System.exit(int)停止线程
目的是停止一个线程,但是这个方法会让整个线程都停止
两阶段终止模式
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46package juc; import lombok.extern.slf4j.Slf4j; @Slf4j(topic = "juc.test11") public class test11 { public static void main(String[] args) throws InterruptedException { TwoPhaseTermination tpt=new TwoPhaseTermination(); tpt.start(); Thread.sleep(3500); tpt.stop(); } } @Slf4j(topic = "juc.TwoPhaseTermination") class TwoPhaseTermination{ private Thread monitor; // 启动监控线程 public void start(){ monitor=new Thread(()->{ while (true){ Thread current=Thread.currentThread(); if (current.isInterrupted()){ log.debug("料理后事"); break; } try { Thread.sleep(1000); log.debug("执行监控记录"); } catch (InterruptedException e) { e.printStackTrace(); current.interrupt(); // 重新设置打断标记 } } }); monitor.start(); } // 停止监控线程 public void stop(){ monitor.interrupt(); } }
复制代码
1
2
3
4
5
6
723:29:37.354 [Thread-0] DEBUG juc.TwoPhaseTermination - 执行监控记录 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at juc.TwoPhaseTermination.lambda$start$0(test11.java:29) at java.lang.Thread.run(Thread.java:745) 23:29:37.816 [Thread-0] DEBUG juc.TwoPhaseTermination - 料理后事
isInterrupted(不****会清楚打断标记) 和 interrupted(会清楚打断标记)都是判断线程是否被打断
打断park线程
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package juc; import lombok.extern.slf4j.Slf4j; import java.util.concurrent.locks.LockSupport; @Slf4j(topic = "juc.test12") public class test12 { public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(()->{ log.debug("pack....."); LockSupport.park(); log.debug("upark"); log.debug("打断状态:{}",Thread.currentThread().isInterrupted()); },"t1"); t1.start(); t1.sleep(1000); t1.interrupt(); } }
复制代码
1
2
3
423:42:01.476 [t1] DEBUG juc.test12 - pack..... 23:42:02.486 [t1] DEBUG juc.test12 - upark 23:42:02.486 [t1] DEBUG juc.test12 - 打断状态:true
再一次打断就无法打断了, LockSupport.park();
如果没有打断的话, LockSupport.park();后面也不会在执行
Thread.intrrupted()这个方法就会为假, LockSupport.park();就会再一次生效
stop() 停止线程运行
suspend() 暂停线程运行
resume() 恢复线程运行
这三个方法都不推荐使用,容易造成同步代码块,造成死锁
@Deprecated 都有已经过时的注解
最后
以上就是发嗲小蘑菇最近收集整理的关于并发编程学习(三)的全部内容,更多相关并发编程学习(三)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复