public class JoinTest2 {
// 现在有T1、T2、T3三个线程,如何保证T2在T1执行完后执行,T3在T2执行完后执行
public static void main(String[] args) {
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1 run");
}
});
final Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
// 引用T1线程,等待T1线程执行完
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T2 run");
}
});
final Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
// 引用t2线程,等待t2线程执行完成
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t3 run");
}
});
t1.start();
t2.start();
t3.start();
}
该你找到的 参考一下 |