当前位置:网站首页>【面试:并发篇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语句需要加锁,目的是为了防止 多线程情况下 对同一个方法反复执行
边栏推荐
- Related functions of strings
- 数据供应链的转型 协调一致走向成功的三大有效策略
- 电脑开机后内存占用过高(50%以上)
- Boss; Can flick CDC Oracle finish reading the full amount of data, just like directly fetching data from the database
- TypeScript阶段学习
- np. transpose & np.expand_ dims
- Kt6368a Bluetooth chip development precautions and problem collection - long term update
- 2022年物联网行业有哪些用例?
- [Luogu] p2709 little B's inquiry
- 公有云安全性和合规性方面的考虑事项
猜你喜欢

MySQL 数据的导入

Part II - C language improvement_ 7. Structure

How to use data pipeline to realize test modernization

Problems and solutions encountered in using nextline(), nextint() and next() in scanner

基本的SELECT语句

黑马瑞吉外卖之新增员工

HCIA-R&S自用笔记(23)DHCP

2. Realize the map of navigation bar and battle page

Dao:op token and non transferable NFT are committed to building a new digital democracy

Science | University of Washington uses AI and structural prediction to design new proteins
随机推荐
Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective
What is Base64?
1. Configuration environment and project creation
Pyqt5 how to set pushbutton click event to obtain file address
第二部分—C语言提高篇_7. 结构体
Disk expansion process and problems encountered in the virtual machine created by VMWare
菜鸟网络面试【杭州多测师】【杭州多测师_王sir】
The interviewer asked: this point of JS
Lesson 2 of Silicon Valley classroom - building project environment and developing lecturer management interface
华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析
np.transpose & np.expand_dims
Eureka basic use
Kingbasees database administrator's Guide -- 11 manage data files and temporary files
Basic select statement
HCIA-R&S自用笔记(21)STP技术背景、STP基础和数据包结构、STP选举规则及案例
第二部分—C语言提高篇_12. 动/精态库的封装和使用
P5469 [noi2019] robot (Lagrange interpolation, interval DP)
The most classic Nature paper on Alzheimer's disease is suspected of fraud
Novice online interview [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
HCIA-R&S自用笔记(20)VLAN综合实验、GVRP