当前位置:网站首页>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 .
边栏推荐
- 工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
- Redis installation under windows and Linux systems
- Nifi from introduction to practice (nanny level tutorial) - flow
- [redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
- 探索Cassandra的去中心化分布式架构
- Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
- Detailed explanation of four modes of distributed transaction (Seata)
- TCP拥塞控制详解 | 3. 设计空间
- Is it safe to open an account with tongdaxin?
- Under VC, Unicode and ANSI are converted to each other, cstringw and std:: string are converted to each other
猜你喜欢

近视:摘镜or配镜?这些问题必须先了解清楚

Microservice API gateway zuul

Record a jar package conflict resolution process

“用Android复刻Apple产品UI”(3)—优雅的数据统计图表

Download and install common programs using AUR

Redis在Windows以及Linux系统下的安装

WinDbg analysis dump file

First knowledge of database
![App mobile terminal test [3] ADB command](/img/f1/4bff6e66b77d0f867bf7237019e982.png)
App mobile terminal test [3] ADB command

The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
随机推荐
Find mapping relationship
Redis high availability and persistence
Calibre LVL
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
Principles of several common IO models
相同切入点的抽取
Colab works with Google cloud disk
App移动端测试【3】ADB命令
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
App mobile terminal test [4] APK operation
The wonderful use of do{}while()
关于网页中的文本选择以及统计选中文本长度
记一次jar包冲突解决过程
[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
Download and install common programs using AUR
pycharm错Error updating package list: connect timed out
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
Three dimensional reconstruction of deep learning
Semi supervised learning