我是靠谱客的博主 悲凉鞋子,这篇文章主要介绍java 两阶段终止线程的正确做法,现在分享给大家,希望可以做个参考。

一、怎么优雅地关闭一个线程?

在一个线程T1中如何优雅地关闭线程T2(也就是说要给T2一个机会释放持有的资源)?

1.错误做法

使用stop()方法停止线程:

stop()方法会真正杀死线程,如果此时该线程持有锁,那么其他线程将永远无法获取锁。

使用System.exit()方法停止线程:

会让整个进程都退出

2.正确做法

思路:

代码实现:

复制代码
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
46
public class Test { public static void main(String[] args) throws InterruptedException { TwoPhaseTermination twoPhaseTermination = new TwoPhaseTermination(); twoPhaseTermination.start(); Thread.sleep(3000); twoPhaseTermination.stop(); } } class TwoPhaseTermination{ // 监控线程 private Thread monitorThread; public void start(){ monitorThread = new Thread(()->{ Thread current = Thread.currentThread(); while(true){ if(current.isInterrupted()){ System.out.println("线程要关闭了..."); break; } try { Thread.sleep(1000); // 阶段1 System.out.println("监控线程正在工作...."); // 阶段2 // 如果在阶段2被打断,线程的isInterrupted标志位为true,会捕抓到信号并关闭线程 // 如果在阶段1被打断,会进入catch语句块,并且isInterrupted标志位清空,无法关闭线程 } catch (InterruptedException e) { e.printStackTrace(); // 需要重新设置isInterrupted标志位为true monitorThread.interrupt(); } } }); // 启动线程 monitorThread.start(); } public void stop(){ // 设置isInterrupted标志位true monitorThread.interrupt(); } }

运行结果:

两阶段关闭线程:

二、要点

为什么需要在catch代码块中重新执行monitorThread.interrupt()?因为Thread.sleep()执行过程中被打断,isInterrupted标志位会清空,下一次进入while循环就会忽略这次打断,继续运行线程。

演示一下把monitorThread.interrupt()注释掉的结果:

可以看到,会忽略这次的isInterrupted信号,继续运行线程。

到此这篇关于java 两阶段终止线程的正确做法的文章就介绍到这了,更多相关java 两阶段终止线程内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是悲凉鞋子最近收集整理的关于java 两阶段终止线程的正确做法的全部内容,更多相关java 两阶段终止线程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(142)

评论列表共有 0 条评论

立即
投稿
返回
顶部