当前位置:网站首页>Multithread 02 thread join
Multithread 02 thread join
2022-07-03 16:09:00 【Cool Wang sledgehammer】
join Use
effect : Ensure the visibility of thread execution results
/**
* @Author: wy
*/
public class MyThread {
private static Integer i = 1;
private static Integer j = 2;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
i=j+1;
j=i;
});
Thread thread2 = new Thread(() -> {
i=3;
j=i;
});
thread1.start();
thread2.start();
Thread.sleep(100);
System.out.println("i:"+i);
System.out.println("j:"+j);
}
}In the above code , In the ideal state, if thread1 and thread2 If it is executed in sequence , The result that we should get is i=4,j=4.
But we know that , We just did start Method , Does not mean that the thread starts running , Not immediately run Method .
At this point, the thread just becomes ready , Which thread executes depends on the scheduling algorithm of the operating system .(window Medium is preemptive , That is, which thread grabs cpu Resources will not be released until the thread is completed .
linux Medium is time-sharing scheduling , That is, the execution time of a given thread , When the time comes, no matter whether the execution is completed or not, the resources must be released )
In order to ensure that thread1 Do it after the execution thread2 Words , have access to join Method .
/**
* @Author: wy
*/
public class MyThread {
private static Integer i = 1;
private static Integer j = 2;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
i=j+1;
j=i;
});
Thread thread2 = new Thread(() -> {
i=3;
j=i;
});
thread1.start();
thread1.join(); // Use join Method , Pause the current process , know thread1 Completion of execution
thread2.start();
Thread.sleep(100);
System.out.println("i:"+i);
System.out.println("j:"+j);
}
}In this way, we can guarantee thread2 really thread1 Executed after execution .
Why?
join End the current process , Until the specified execution is completed .

From this picture, we can see thread2 really thread1 After that .
Again join We can see in the code of
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
// call wait Method
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}Here it is. join It is through wait, and notify To operate the process to achieve this effect .
边栏推荐
- 用同花顺炒股开户安全吗?
- 记一次jar包冲突解决过程
- Detailed explanation of four modes of distributed transaction (Seata)
- From the 18th line to the first line, the new story of the network security industry
- Colab works with Google cloud disk
- Large CSV split and merge
- [combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
- Mb10m-asemi rectifier bridge mb10m
- 【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
- Microservice API gateway
猜你喜欢

突破100万,剑指200万!

面试官:JVM如何分配和回收堆外内存

Record a jar package conflict resolution process

Microservices Seata distributed transactions
![[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解](/img/cd/00954b9c592c253d42e6a3b8298999.jpg)
[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
![[list to map] collectors Tomap syntax sharing (case practice)](/img/ac/e02deb1cb237806d357a88fb812852.jpg)
[list to map] collectors Tomap syntax sharing (case practice)

Mongodb installation and basic operation

Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN

How to thicken the brush in the graphical interface

Break through 1million, sword finger 2million!
随机推荐
Introduction series of software reverse cracking (1) - common configurations and function windows of xdbg32/64
App mobile terminal test [5] file writing and reading
Record a jar package conflict resolution process
用通达信炒股开户安全吗?
Secsha system 1- login function
App移动端测试【3】ADB命令
[combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
相同切入点的抽取
Redis installation under windows and Linux systems
First knowledge of database
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
Microservice - declarative interface call openfeign
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
切入点表达式
QT use qzxing to generate QR code
How to thicken the brush in the graphical interface
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
EditText request focus - EditText request focus
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)