当前位置:网站首页>【面试:并发篇26:多线程:两阶段终止模式】volatile版本
【面试:并发篇26:多线程:两阶段终止模式】volatile版本
2022-07-26 22:31:00 【I cream】
【面试:并发篇26:多线程:两阶段终止模式】volatile版本
00.前言
如果有任何问题请指出,感谢。
01.介绍
可以与这个文章中的两阶段终止模式进行对比https://blog.csdn.net/m0_71229547/article/details/125656363?spm=1001.2014.3001.5501
02.代码
@Slf4j(topic = "c.TwoPhaseTermination")
public class Test13 {
public static void main(String[] args) throws InterruptedException {
TwoPhaseTermination tpt = new TwoPhaseTermination();
tpt.start();
Thread.sleep(3500);
log.debug("停止监控");
tpt.stop();
}
}
@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination {
// 监控线程
private Thread monitorThread;
// 停止标记
private volatile boolean stop = false;
// 启动监控线程
public void start() {
monitorThread = new Thread(() -> {
while (true) {
if (stop) {
log.debug("料理后事");
break;
}
try {
Thread.sleep(1000);
log.debug("执行监控记录");
} catch (Exception e) {
}
}
}, "monitor");
monitorThread.start();
}
// 停止监控线程
public void stop() {
stop = true;
monitorThread.interrupt();
}
}
结果
17:32:07.094 c.TwoPhaseTermination [monitor] - 执行监控记录
17:32:08.108 c.TwoPhaseTermination [monitor] - 执行监控记录
17:32:09.109 c.TwoPhaseTermination [monitor] - 执行监控记录
17:32:09.585 c.TwoPhaseTermination [main] - 停止监控
17:32:09.585 c.TwoPhaseTermination [monitor] - 料理后事
解释
与之前的不同点是:这里我们利用的是静态变量stop进行是否还监控的判断,但是一个线程一直读 另一个线程写,势必会导致可见性问题,所以我们用了volatile修饰stop,另外如果我们在sleep期间 主线程把stop改为了true 那么我们还是需要等待sleep完才可以进行是否退出的判断,所以我们主线程的stop方法里面不仅改变了stop的值 还顺便把监控线程打断 避免了上述sleep的问题。
边栏推荐
- What is the reason for oom during redis shake synchronization in shake database?
- 会议OA之我的会议
- Kalibr calibration realsensed435i -- multi camera calibration
- Hcia-r & s self use notes (19) VLAN configuration and experiment, routing between VLANs
- np. transpose & np.expand_ dims
- 2022.7.18-----leetcode.749
- org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
- Three effective strategies for the transformation of data supply chain to be coordinated and successful
- Kingbasees database administrator's Guide -- 11 manage data files and temporary files
- Import of MySQL data
猜你喜欢

Part II - C language improvement_ 13. Recursive function

第二部分—C语言提高篇_13. 递归函数

Vector execution engine framework gluten announced the official open source and appeared at spark technology summit

Part II - C language improvement_ 11. Pretreatment

第二部分—C语言提高篇_6. 多维数组

Programmer growth chapter 29: how to motivate employees?
![[2016] [paper notes] differential frequency tunable THz technology——](/img/7e/71126950250997fc436a4ee730aee7.png)
[2016] [paper notes] differential frequency tunable THz technology——

第二部分—C语言提高篇_11. 预处理

【2016】【论文笔记】差频可调谐THz技术——

Disk expansion process and problems encountered in the virtual machine created by VMWare
随机推荐
力扣155题,最小栈
什么是 Base64 ?
Part II - C language improvement_ 10. Function pointer and callback function
MySQL random paging to get non duplicate data
Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support
2022.7.26-----leetcode.1206
第二部分—C语言提高篇_6. 多维数组
Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)
华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析
Public cloud security and compliance considerations
Vit:vision transformer super detailed with code
18. Opening and saving file dialog box usage notes
Go uses flag package to parse command line parameters
Dao:op token and non transferable NFT are committed to building a new digital democracy
Machine learning notes - building recommendation system (3) six research directions of deep recommendation system
Sign up now | frontier technology exploration: how to make spark stronger and more flexible
[shader realizes shine effect _shader effect Chapter 3]
Six challenges facing enterprise data governance!
C.Net timestamp and time conversion support time zone
第二部分—C语言提高篇_7. 结构体