利用 join()方法,先讓某個執行緒完成結束後,自己才開始執行。
class Car2 extends Thread {
String name;
Car2 (String name){
this.name = name;
}
public void run(){
for(int i=0;i < 5;i++){
System.out.println("執行 "+ name+" 執行緒");
}
}
}
public class ThreadJoin {
public static void main(String[] args) throws InterruptedException {
Car2 car1 = new Car2("汽車 1");
car1.start();
car1.join();
Car2 car2 = new Car2("汽車 2");
car2.start();
}
}
執行結果

沒有留言:
張貼留言