当前位置:网站首页>[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 .
边栏推荐
- 黑盒(功能)测试基本方法
- Test cases and defect report templates
- 【面试:并发篇23:多线程:join】join再理解
- 腾讯云数据库的可信可控之路
- QT | learn about QT creator by creating a simple project
- [FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
- In depth understanding of seven specific ways to enhance code scalability
- The onnx model is exported as a TRT model
- cuda_ error_ out_ of_ Memory (out of memory)
- MPI学习笔记(二):矩阵相乘的两种实现方法
猜你喜欢
![[ManageEngine] value brought by Siem to enterprises](/img/1e/56d64d193e0428523418bef5e98866.png)
[ManageEngine] value brought by Siem to enterprises

DDD的Go实战

Add startup software items when the win system starts up

C#Socket

Apple estimates that iPhone will give up the Chinese market, and the Chinese industrial chain needs to consider living a hard life

Byte side: can TCP and UDP use the same port?

pyqt5使用pyqtgraph绘制多个Y值散点图

Qixin Jushi cloud spectrum new chapter | Haitai Fangyuan and Sichuan Unicom reach ecological strategic cooperation

我也是醉了,Eureka 延迟注册还有这个坑!
What's special about Huawei's innovative solutions to consolidate the foundation of ERP for small and medium-sized enterprises?
随机推荐
Autojs learning - realize 3D perspective
mysql导入数据时已改成csv utf8文件且文件名为英文,为什么还是导入失败
919. 完全二叉树插入器 : 简单 BFS 运用题
How to solve the problem of high concurrency and large traffic with PHP
Zero basic learning canoe panel (17) -- panel CAPL function
如何自动生成短链?如何在线批量生成带UTM参数的链接?
工作面试总遇秒杀? 看了京东 T8 大咖私藏的秒杀系统笔记, 已献出膝盖
An interview question about interface and implementation in golang
Array of arm disassembly
[ManageEngine] value brought by Siem to enterprises
ZigBee IOT development platform (Industrial IOT)
ag 搜索工具参数详解
Apple estimates that iPhone will give up the Chinese market, and the Chinese industrial chain needs to consider living a hard life
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)
两数,三数之和
Six principles of C program design
resize函数的作用「建议收藏」
Test cases and defect report templates
Achieve accurate positioning based on Tencent map, and realize the attendance punch function of wechat applet
Cesium 多边形渐变色纹理(Canvas)