我有一个例子对我来说似乎很陌生。Java join()方法
public class Join {
public static void main(String[] args) {
Thread t1 = new Thread(
new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
);
Thread t2 = new Thread(t1);
t1.setName("t1");
t2.setName("t2");
t1.start();
try {t1.join();} catch (InterruptedException ie) {}
t2.start();
}
}
我们只会看到只打印t1。 如果我们将评论“t1.join”,我们将选择预期的输出(t1 t2)。 为什么?
非常感谢,我知道这是一个非常糟糕的做法,当然,join()在这里没有任何理由,它只是出于好奇。 – Bax 2012-03-21 08:38:22