当前位置:网站首页>【面试:并发篇27:多线程:犹豫模式】
【面试:并发篇27:多线程:犹豫模式】
2022-07-26 22:31:00 【I cream】
【面试:并发篇27:多线程:犹豫模式】
00.前言
如果有任何问题请指出,感谢。
01.介绍
什么是犹豫模式,指的是一个线程发现另一个线程或者线程已经做了某一件相同的事,那么本线程就无需再做了,直接结束返回。
通俗来说就是 一个对象只能有效调用一次方法,之后再调用直接返回结束。
02.两阶段终止模式-犹豫模式优化
@Slf4j(topic = "c.TwoPhaseTermination")
public class Test13 {
public static void main(String[] args) throws InterruptedException {
TwoPhaseTermination tpt = new TwoPhaseTermination();
tpt.start();
tpt.start();
tpt.start();
Thread.sleep(3500);
log.debug("停止监控");
tpt.stop();
}
}
@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination {
// 监控线程
private Thread monitorThread;
// 停止标记
private volatile boolean stop = false;
// 判断是否执行过 start 方法
private boolean starting = false;
// 启动监控线程
public void start() {
synchronized (this) {
//犹豫模式主要部分代码
if (starting) {
// false
return;
}
starting = true;
}
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();
}
}
结果
19:53:25.992 c.TwoPhaseTermination [monitor] - 执行监控记录
19:53:27.004 c.TwoPhaseTermination [monitor] - 执行监控记录
19:53:28.009 c.TwoPhaseTermination [monitor] - 执行监控记录
19:53:28.487 c.TwoPhaseTermination [main] - 停止监控
19:53:28.487 c.TwoPhaseTermination [monitor] - 料理后事
解释
我们创建了一个starting变量 作用是判断这个方法是否已经被调用过 如果没有则执行之后的代码 如果有则直接返回,这里我们执行了三次start方法 但结果只运行了一次。
不过还要注意一点 判断是否执行的if语句需要加锁,目的是为了防止 多线程情况下 对同一个方法反复执行
边栏推荐
- Hcia-r & s self use notes (20) VLAN comprehensive experiment, GVRP
- Three person management of system design
- New employees of black maredge takeout
- Part II - C language improvement_ 5. Bit operation
- json格式化小工具--pyqt5实例
- Counter attack dark horse: devdbops training, give you the best courses!
- 【flask高级】结合源码分析flask中的线程隔离机制
- P5469 [NOI2019] 机器人(拉格朗日插值、区间dp)
- Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
- Dynamic memory management and related topics
猜你喜欢

Hcia-r & s self use notes (21) STP technical background, STP foundation and data package structure, STP election rules and cases

Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)

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

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

Ribbon load balancing

18、打开、保存文件对话框使用小记

为什么我还在CSDN写文章?一段陪伴学习的历程。

Thousands of tiles' tilt model browsing speeds up, saying goodbye to the embarrassment of jumping out one by one

How to use data pipeline to realize test modernization

DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
随机推荐
上千Tile的倾斜模型浏览提速,告别一块一块往外蹦的尴尬
What is Base64?
Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)
MySQL syntax uses detailed code version
TypeScript阶段学习
Distributed lock and its implementation
[Luogu] p2709 little B's inquiry
Hcia-r & s self use notes (20) VLAN comprehensive experiment, GVRP
Signal debugging document developed by car
Dajiang Zhitu and CC have produced multiple copies of data. How to combine them into one and load them in the new earth map
The nature and proof of the center of gravity of [mathematics] tree
[2016] [paper notes] differential frequency tunable THz technology——
The most classic Nature paper on Alzheimer's disease is suspected of fraud
Problems and solutions encountered in using nextline(), nextint() and next() in scanner
C language dynamic memory management
Thousands of tiles' tilt model browsing speeds up, saying goodbye to the embarrassment of jumping out one by one
Science | University of Washington uses AI and structural prediction to design new proteins
大疆智图、CC生产了多份数据,如何合并为一份在图新地球进行加载
pgsql -> flink cdc -> flink -> Mysql, if a PgSQL CDC
Download win10 system image and create virtual machine on VMware virtual machine