当前位置:网站首页>[interview: concurrent Article 27: multithreading: hesitation mode]
[interview: concurrent Article 27: multithreading: hesitation mode]
2022-07-26 23:35:00 【I cream】
【 interview : Concurrent articles 27: Multithreading : Hesitation mode 】
00. Preface
If you have any questions, please point out , thank .
01. Introduce
What is hesitation mode , It means that a thread finds another thread or thread has done something the same , Then this thread does not need to do any more , Straight back to .
Generally speaking, it's An object can only effectively call a method once , Then call directly to return to the end .
02. Two stage termination mode - Hesitation mode optimization
@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(" Stop monitoring ");
tpt.stop();
}
}
@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination {
// Monitor threads
private Thread monitorThread;
// Stop mark
private volatile boolean stop = false;
// To judge whether or not start Method
private boolean starting = false;
// Start the monitoring thread
public void start() {
synchronized (this) {
// Hesitation mode main part code
if (starting) {
// false
return;
}
starting = true;
}
monitorThread = new Thread(() -> {
while (true) {
if (stop) {
log.debug(" Take care of future affairs ");
break;
}
try {
Thread.sleep(1000);
log.debug(" Execution monitoring record ");
} catch (Exception e) {
}
}
}, "monitor");
monitorThread.start();
}
// Stop monitoring thread
public void stop() {
stop = true;
monitorThread.interrupt();
}
}
result
19:53:25.992 c.TwoPhaseTermination [monitor] - Execution monitoring record
19:53:27.004 c.TwoPhaseTermination [monitor] - Execution monitoring record
19:53:28.009 c.TwoPhaseTermination [monitor] - Execution monitoring record
19:53:28.487 c.TwoPhaseTermination [main] - Stop monitoring
19:53:28.487 c.TwoPhaseTermination [monitor] - Take care of future affairs
explain
We created one starting Variable The function is to judge whether this method has been called If not, execute the following code Direct return if any , Here we have implemented it three times start Method But it only ran once .
But one more thing to note Judge whether it is implemented if Statement needs to be locked , The purpose is to prevent multithreading Execute the same method repeatedly
边栏推荐
- Easily implement seckill system with redis! (including code)
- 大疆智图、CC生产了多份数据,如何合并为一份在图新地球进行加载
- 会议OA之我的会议
- Part II - C language improvement_ 13. Recursive function
- HCIA-R&S自用笔记(23)DHCP
- The memory occupation of the computer is too high after it is turned on (more than 50%)
- Download win10 system image and create virtual machine on VMware virtual machine
- 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
- C.Net timestamp and time conversion support time zone
- Silicon Valley class lesson 6 - Tencent cloud on demand management module (I)
猜你喜欢

Eureka basic use
![[shader realizes shine effect _shader effect Chapter 3]](/img/ea/6c14f682e6157a96c1877d99c9f7d3.png)
[shader realizes shine effect _shader effect Chapter 3]

JSON formatting gadget -- pyqt5 instance

Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support

研究阿尔茨海默病最经典的Nature论文涉嫌造假

科研太忙无法顾家?陈婷:人生不能只有一个支点

Basic select statement

公有云安全性和合规性方面的考虑事项

Easily implement seckill system with redis! (including code)

【2016】【论文笔记】差频可调谐THz技术——
随机推荐
How to transfer the GPX data collected by CTI RTK out of KML and SHP with attributes for subsequent management and analysis
New employees of black maredge takeout
Application of workflow engine in vivo marketing automation | engine 03
Science | University of Washington uses AI and structural prediction to design new proteins
Real time voice quality monitoring
Sign up now | frontier technology exploration: how to make spark stronger and more flexible
告别宽表,用 DQL 成就新一代 BI
菜鸟网络面试【杭州多测师】【杭州多测师_王sir】
研究阿尔茨海默病最经典的Nature论文涉嫌造假
科研太忙无法顾家?陈婷:人生不能只有一个支点
Related functions of strings
MySQL random paging to get non duplicate data
Hcia-r & s self use notes (18) campus network architecture foundation, switch working principle, VLAN principle
百度网址收录
[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code
29、 Implementation of xv6 file system (GDB tracks mkfs, buffer cache and log)
Part II - C language improvement_ 9. Linked list
Six challenges facing enterprise data governance!
Dao:op token and non transferable NFT are committed to building a new digital democracy
json格式化小工具--pyqt5实例