t.join()方法阻塞调用此方法的线程(calling thread),直到线程t完成,此线程再继续;通常用于在main()主线程内,等待其它线程完成再结束main()主线程。
package thread;
/**
* T1执行之后T2 之后T3
* @author zhoushiwen
*
*/
public class Thread1 {
public static void main(String[] args) throws Exception{
Thread t1 = new Thread("t1"){
@Override
public void run() {
System.out.println("t1.....");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread("t2"){
@Override
public void run() {
try {
t1.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("t2.....");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t3 = new Thread("t3"){
@Override
public void run() {
try {
t2.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("t3.....");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t3.....over");
}
};
t1.start();
t2.start();
t3.start();
}
}