标签:err 使用 计数 ati join 条件 strong 一个 入门
join():正在执行的线程需要等待调用join()的线程执行完之后,才可以继续执行。
使用场景:当一个线程必须等待另一个线程执行完毕才能执行时可以使用join方法。
/**
* 功能描述:顺序执行步骤1,2,3,
* @since 2020-06-08
*/
class TestJoin {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("第1步");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
t1.join();
System.out.println("第2步");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
t2.join();
System.out.println("第3步");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
System.out.println("第2步准备...");
t2.start();
System.out.println("第3步准备...");
t3.start();
System.out.println("第1步准备...");
t1.start();
}
}
|
第2步准备... 第3步准备... 第1步准备... 第1步 第2步 第3步 |
标签:err 使用 计数 ati join 条件 strong 一个 入门
原文地址:https://www.cnblogs.com/nxf-rabbit75/p/13067605.html