1.現在有T1、T2、T3三個線程,你怎樣保證T2在T1執行完後執行,T3在T2執行完後執行
解決思路:
(1)、線程按順序啓動,
(2)、for (;;) {
if (!t1.isAlive()){
System.out.println(t1.isInterrupted());
t2.start();
break;
}
}
循環判斷線程狀態
(3)、當前一個線程完成後再start下一個線程
public static void main(String[] args) {
Thread t1 = new Thread(new Sequence());
Thread t2 = new Thread(new Sequence());
Thread t3 = new Thread(new Sequence());
t1.start();
//isAlive() 的功能是判斷當前的線程是否處於活動狀態
for (;;) {
if (!t1.isAlive()){
t2.start();
break; //這裏要記得跳出循環,否則會報錯!
}
}
for (;;) {
if (!t2.isAlive()){
t3.start();
break;//這裏要記得跳出循環,否則會報錯!
}
}
}