了解join()方法示例 [英] Understanding join() method example
本文介绍了了解join()方法示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Java thread join()方法让我感到困惑。我有以下示例
The Java thread join() method confuses me a bit. I have following example
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();
}
}
线程终止的顺序是什么?
In which order are the threads terminated?
推荐答案
实际上让您对 Thread.join()
感到困惑?你没有提到具体的任何内容。
What actually confuses you about Thread.join()
? You haven't mentioned anything specific.
鉴于 Thread.join()
(如文档说明),等待这个线程要死
,然后 t4
将等待 t3
完成,这将等待要 t2
完成,等待 t1
完成。
Given that Thread.join()
(as the documentation states), Waits for this thread to die
, then t4
will wait for t3
to complete, which will wait for t2
to complete, which will wait for t1
to complete.
因此 t1
将首先完成,然后是 t2
, t3
和 t4
。
Therefore t1
will complete first, followed by t2
, t3
, and t4
.
这篇关于了解join()方法示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文