当前位置:网站首页>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 .
边栏推荐
- Create gradle project
- Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
- Embedded development: seven reasons to avoid open source software
- 请求头不同国家和语言的表示
- Brush questions -- sword finger offer
- [proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
- App移动端测试【4】apk的操纵
- 《微服务设计》读书笔记(上)
- Problems of CString in multithreading
- Go语言自学系列 | golang switch语句
猜你喜欢

Download and install common programs using AUR

From the 18th line to the first line, the new story of the network security industry
![App mobile terminal test [4] APK operation](/img/f1/4bff6e66b77d0f867bf7237019e982.png)
App mobile terminal test [4] APK operation

探索Cassandra的去中心化分布式架构

CString getbuffer and releasebuffer instructions

Wechat payment -jsapi: code implementation (payment asynchronous callback, Chinese parameter solution)

Microservices Seata distributed transactions

Nifi from introduction to practice (nanny level tutorial) - flow
![[web security] - [SQL injection] - error detection injection](/img/18/5c511871dab0e5c684b6b4c081c061.jpg)
[web security] - [SQL injection] - error detection injection

Myopia: take off or match glasses? These problems must be understood clearly first
随机推荐
【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
[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)
Automatic generation of client code from flask server code -- Introduction to flask native stubs Library
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
Colab works with Google cloud disk
Principles of several common IO models
2022年Q2加密市场投融资报告:GameFi成为投资关键词
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
Backtracking method to solve batch job scheduling problem
Microservice API gateway
一些事情的反思
pyinstaller不是内部或外部命令,也不是可运行的程序 或批处理文件
"Remake Apple product UI with Android" (3) - elegant statistical chart
How idea starts run dashboard
Redis installation under windows and Linux systems
The wonderful use of do{}while()
Initial test of scikit learn Library
Microservice sentinel flow control degradation
无心剑中译泰戈尔《漂鸟集(1~10)》
Semi supervised learning