当前位置:网站首页>【面试:并发篇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语句需要加锁,目的是为了防止 多线程情况下 对同一个方法反复执行
边栏推荐
- 2. Realize the map of navigation bar and battle page
- The memory occupation of the computer is too high after it is turned on (more than 50%)
- The interviewer asked: this point of JS
- np.transpose & np.expand_dims
- Kingbasees database administrator's Guide -- 11 manage data files and temporary files
- What is Base64?
- 第二部分—C语言提高篇_13. 递归函数
- Kingbasees SQL language reference manual of Jincang database (3.1.1.14. scope type)
- 第二部分—C语言提高篇_7. 结构体
- 公有云安全性和合规性方面的考虑事项
猜你喜欢

数据供应链的转型 协调一致走向成功的三大有效策略

SQL Basics

PostgreSQL and Navicat: the backbone of the database industry

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

【flask高级】结合源码分析flask中的线程隔离机制

Lesson 2 of Silicon Valley classroom - building project environment and developing lecturer management interface

Public cloud security and compliance considerations

Vit:vision transformer super detailed with code

HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理

关于 StatefulWidget,你不得不知道的原理和要点!
随机推荐
Three effective strategies for the transformation of data supply chain to be coordinated and successful
C.Net timestamp and time conversion support time zone
Basic use of gateway
HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
Part II - C language improvement_ 7. Structure
第二部分—C语言提高篇_13. 递归函数
MySQL 数据的导入
力扣155题,最小栈
百度网址收录
Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
使用AW9523B芯片驱动16路LED时,LED出现误点亮的问题
8 other programming languages -- Recording
Science | University of Washington uses AI and structural prediction to design new proteins
DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
MySQL random paging to get non duplicate data
Esmfold: a new breakthrough in protein structure prediction after alphafold2
Part II - C language improvement_ 9. Linked list
SQL Basics
Learn various details and thoughts of chatroom implementation in Muduo
2022.7.18-----leetcode.749