当前位置:网站首页>【面试:并发篇23:多线程:join】join再理解
【面试:并发篇23:多线程:join】join再理解
2022-07-25 21:27:00 【I cream】
【面试:并发篇23:多线程:join】join再理解
00.前言
如果有任何问题请指出,感谢。
01.介绍
昨天我突然想到一个问题 join方法的底层实现是wait,执行wait的线程等待,那么它是怎么唤醒的呢?抱着这个问题我找了找博客最后发现了这篇文章https://blog.csdn.net/x541211190/article/details/109322537,这里面介绍了notifyAll的解锁时机,我发现了这么一段话在java中,Thread类线程执行完run()方法后,一定会自动执行notifyAll()方法 这句话如同醍醐灌顶,我立刻就理解了join方法里的wait是如何唤醒的了。
接下来我来模拟一下join的过程。
02.模拟
join源码

我们关注两个地方,第一是while循环条件 isAlive()它的作用是 判断调用join的线程是否存活 如果存活则执行wait()这里wait作用的线程是执行join的线程,注意这里 调用和执行的区别。举一个例子:我们在t2线程中执行了t1.join(),那么isAlive()判断的是t1线程是否存活,wait是让t2线程陷入等待
join例子
@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();
}
}
结果
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
解释
我们创建了两个线程 t1 t2,我们在t2线程中执行t1.join使得t1线程同步到t2线程,结果也能证明这个结论
用wait模拟join例子
@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();
}
}
结果
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
解释
我们发现我们在t2线程创建了一个以 t1线程对象为监视器的锁,并执行了wait()方法导致t2线程陷入等待,此时如果我们没有notify或notifyAll方法的话 是不能唤醒t2线程的,但这里神奇的事情出现了 我们发现最终t2线程还是执行了。原因还是这句话在java中,Thread类线程执行完run()方法后,一定会自动执行notifyAll()方法,因为我们是把t1线程当成锁对象,但随着t1线程的运行结束 即run方法运行结束 t1线程执行了t1.notifyAll(),所以此时唤醒了 所有以t1为锁的线程,所以t2线程被唤醒了。这就是join的实现原理。
边栏推荐
- 牛客-TOP101-BM37
- Explain in detail the principle of MySQL master-slave replication "suggestions collection"
- NVIDIA has opened source a comprehensive library of 3D deep learning based on pytorch
- How to evaluate hardware resources (number of CPUs, memory size) when Oracle migrates from small computers to x86 architecture? Is there a measurement index or company?
- yuv422转rgb(422sp转420p)
- 浅谈web性能优化(一)
- An interview question about concurrent reading and writing of map in golang
- Research on the scheme of MySQL advanced (VIII) sorting problem
- 2022-07-18: what is the output of the following go language code? A:Groutine; B:Main; C:Goroutine; D:GoroutineMain。 package m
- Pychart automatically enters the test mode when running the program
猜你喜欢

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)

Opencv learning Fourier transform experience and line direction Fourier transform code

Jmeter分布式压测

Leetcode skimming -- guess the size of numbers II 375 medium

作为测试,如何理解线程同步异步

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

Basic method of black box (function) test

My heart's broken! After being cheated by 30000, a 16-year-old girl was unconvinced and cheated by 50000

Apple estimates that iPhone will give up the Chinese market, and the Chinese industrial chain needs to consider living a hard life
![[ManageEngine] value brought by Siem to enterprises](/img/1e/56d64d193e0428523418bef5e98866.png)
[ManageEngine] value brought by Siem to enterprises
随机推荐
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (7) - Fiddler status panel -quickexec command line
The onnx model is exported as a TRT model
字节一面:TCP 和 UDP 可以使用同一个端口吗?
[fiddlertx plug-in] use Fiddler to capture the package Tencent classroom video download (unable to capture the package solution)
Basic method of black box (function) test
The international summit osdi included Taobao system papers for the first time, and end cloud collaborative intelligence was recommended by the keynote speech of the conference
Explain the principle of MySQL master-slave replication in detail
黑盒(功能)测试基本方法
Stm3 (cubeide) lighting experiment
Sqlx library usage
In June 2021, the interview suffered a Waterloo. Is it so convoluted now
NPM module removal_ [solved] after NPM uninstalls the module, the module is not removed from package.json [easy to understand]
Detailed explanation of JVM memory model and structure (five model diagrams)
Add startup software items when the win system starts up
Cesium polygon gradient texture (canvas)
[online tutorial] iptables official tutorial -- learning notes 2
Autojs learning - file depth search
ag 搜索工具参数详解
Vivo official website app full model UI adaptation scheme
Achieve accurate positioning based on Tencent map, and realize the attendance punch function of wechat applet