Java線程的join()方法讓我困惑了一下。我有下面的例子瞭解join()方法示例
class MyThread extends Thread {
private String name;
private int sleepTime;
private Thread waitsFor;
MyThread(String name, int stime, Thread wa) { … }
public void run() {
System.out.print("["+name+" ");
try { Thread.sleep(sleepTime); }
catch(InterruptedException ie) { }
System.out.print(name+"? ");
if (!(waitsFor == null))
try { waitsFor.join(); }
catch(InterruptedException ie) { }
System.out.print(name+"] ");
而且
public class JoinTest2 {
public static void main (String [] args) {
Thread t1 = new MyThread("1",1000,null);
Thread t2 = new MyThread("2",4000,t1);
Thread t3 = new MyThread("3",600,t2);
Thread t4 = new MyThread("4",500,t3);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
在其順序線程終止?
這是一個詭計問題嗎?或作業? :) – bzlm 2011-05-03 16:45:36
剛剛讀了一些電子書,發現這個例子,實際上執行線程等待線程在哪裏加入被稱爲完成,也許我今天就退出:D – 2011-05-03 16:50:27
等待線程死亡。 http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#join() – 2011-05-03 16:51:05