当前位置:网站首页>[interview: concurrent Article 23: multithreading: Join] re understanding of join
[interview: concurrent Article 23: multithreading: Join] re understanding of join
2022-07-25 21:29:00 【I cream】
【 interview : Concurrent articles 23: Multithreading :join】join Understand again
00. Preface
If you have any questions, please point out , thank .
01. Introduce
Yesterday I suddenly thought of a question join The underlying implementation of the method is wait, perform wait Thread waiting for , So how does it wake up ? With this problem in mind, I looked for a blog and finally found this article https://blog.csdn.net/x541211190/article/details/109322537, It introduces notifyAll Unlocking time of , I found such a passage stay java in ,Thread Class thread finished executing run() After the method , It will be automatically executed notifyAll() Method This sentence is like an epiphany , I immediately understood join In the method wait How to wake up .
Next, let me simulate join The process of .
02. simulation
join Source code

We focus on two places , The first is the while The loop condition isAlive() Its function is Judgment call join Is the thread alive or not If alive, execute wait() here wait The role of the thread is to execute join The thread of , Note that there The difference between call and execution . For example : We are t2 Executed in thread t1.join(), that isAlive() The judgment is t1 Is the thread alive ,wait It's Jean t2 Thread caught waiting
join Example
@Slf4j(topic = "c.TestInterJoin")
public class TestInterJoin {
public static void main(String[] args) {
Thread t1 = new Thread(()->{
for (int i=0;i<5;i++){
log.debug("t1");
}
},"t1");
Thread t2 = new Thread(()->{
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i=0;i<5;i++){
log.debug("t2");
}
},"t2");
t2.start();
t1.start();
}
}
result
15:08:50.125 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2
explain
We created two threads t1 t2, We are t2 Execute in thread t1.join bring t1 The thread is synchronized to t2 Threads , The result can also prove this conclusion
use wait simulation join Example
@Slf4j(topic = "c.TestInterJoinMN")
public class TestInterJoinMN {
public static void main(String[] args) {
Thread t1 = new Thread(()->{
Sleeper.sleep(2);
for (int i=0;i<5;i++){
log.debug("t1");
}
},"t1");
Thread t2 = new Thread(()->{
synchronized (t1){
try {
t1.wait();
} catch (InterruptedException e) {
e.pri(ntStackTrace();
}
}
for (int i=0;i<5;i++){
log.debug("t2");
}
},"t2");
t2.start();
t1.start();
}
}
result
15:12:16.361 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2
explain
We found that we were t2 The thread creates a with t1 The thread object is the lock of the monitor , And perform the wait() Methods lead to t2 Thread caught waiting , At this time, if we don't notify or notifyAll Method words Can't wake up t2 Thread , But something magical happened here We found that eventually t2 The thread still executes . The reason is still this sentence stay java in ,Thread Class thread finished executing run() After the method , It will be automatically executed notifyAll() Method , Because we put t1 Threads are treated as lock objects , But as the t1 The running of the thread ends namely run End of method run t1 The thread executes t1.notifyAll(), So I woke up at this time All with t1 Thread for lock , therefore t2 The thread is awakened . This is it. join Implementation principle of .
边栏推荐
- H5 realize the animation effect of a scratch card
- CTS test steps (Casio cts200 test)
- day04_ array
- Temperature and humidity environment monitoring system based on stm32
- Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
- The noise reduction effect is increased by more than 6 times! With the microphone inside the headset, this wireless noise reduction headset is unusual!
- Trusted and controllable way of Tencent cloud database
- Experience sharing of system architecture designers preparing for the exam: from point to surface
- Research: more than 70% of doctors are still prescribing unsafe antibiotic drugs
- 我也是醉了,Eureka 延迟注册还有这个坑!
猜你喜欢

2022 latest examination questions and answers of eight members (standard staff) of Shanghai Architecture

NVIDIA has opened source a comprehensive library of 3D deep learning based on pytorch

【FiddlerTX插件】使用Fiddler抓包腾讯课堂视频下载(抓不到包解决方案)

Canvas fill gradient

Decompile app
![[ManageEngine] value brought by Siem to enterprises](/img/1e/56d64d193e0428523418bef5e98866.png)
[ManageEngine] value brought by Siem to enterprises

Reading the pointpillar code of openpcdet -- Part 3: Calculation of loss function

Vivo official website app full model UI adaptation scheme

Airtest solves the problem that a password needs to be entered in the process of "automatic packaging" (the same applies to random bullet frame processing)

Test cases and defect report templates
随机推荐
[ManageEngine] value brought by Siem to enterprises
Research on the scheme of MySQL advanced (VIII) sorting problem
ES6---4个强大运算符(??、??=、?.、?:)
函数栈帧的创建和销毁
Please give an example of how to optimize MySQL index (sqlserver index optimization)
Beisen Holdings' IPO: a total loss of 4.115 billion yuan in three years, and a total of 2.84 billion yuan in the previous nine rounds of financing
[online tutorial] iptables official tutorial -- learning notes 2
How to solve the problem of high concurrency and large traffic with PHP
如何自动生成短链?如何在线批量生成带UTM参数的链接?
How to choose sentinel vs. hystrix current limiting?
When MySQL imports data, it has been changed to CSV utf8 file and the file name is English. Why does it still fail to import
cuda_ error_ out_ of_ Memory (out of memory)
Detailed explanation of Ag search tool parameters
H5 realize the animation effect of a scratch card
An interview question about recover in golang
Focus on data | Haitai Fangyuan directly hits the construction idea of data security governance in the securities industry
3阶有向完全图的所有非同构的子图(不同钩子图个数)
An interview question about concurrent reading and writing of map in golang
Mysql8.0 MHA to achieve high availability "MHA"
2022 latest examination questions and answers of eight members (standard staff) of Shanghai Architecture