Thread.join()的使用

如果一个线程A执行了thread.join()方法,表示,当前线程A等待thread线程终止之后才从thread.join()返回。线程Thread除了提供join()方法之外,还提供了join(long millis)和join(long millis,int nanos)两个具备超时特性的方法。如果线程thread在给定的超时时间里没有终止,那么将会从该超时方法中返回。

 

public class JoinExample { 
static class Domino implements Runnable{
 private Thread thread; 
public Domino(Thread thread) {
 this.thread=thread; 
} 
@Override public void run() {       
 try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"执行结束!");

    }

}
public static void main(String[] args) throws InterruptedException{
    Thread currentThread = Thread.currentThread();
    for (int i = 0; i <10; i++) {
        //每个线程拥有前一个线程的引用,需要等待前一个线程终止,才能从等待中返回
        Thread thread = new Thread(new Domino(currentThread),String.valueOf(i));
        thread.start();
        currentThread=thread;
    }
    TimeUnit.SECONDS.sleep(5);
    System.out.println(Thread.currentThread().getName()+",执行结束!");
}

} 执行结果如下:

main,

执行结束!

0执行结束!

1执行结束!

2执行结束!

3执行结束!

4执行结束!

5执行结束!

6执行结束!

7执行结束!

8执行结束!

9执行结束!

每个线程终止的前提是前驱线程的终止,每个线程等待前驱线程终止后,才从join()方法返回。如果把调用Thread.join的代码注释后,再次执行,结果如下:

0执行结束!

3执行结束!

2执行结束!

1执行结束!

5执行结束!

4执行结束!

6执行结束!

7执行结束!

8执行结束!

9执行结束!

main,执行结束!

Thread.join的源代码:

//加锁当前线程对象
  public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;
        //millis不得小于0
        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }
        //如果millis等于0,即没有超时设置
        if (millis == 0) {
            //条件不满足,就继续等待
            while (isAlive()) {
                wait(0);
            }
        } else {
            //如果条件不满足,并且在延迟时间内,就继续等待
            while (isAlive()) {
                //计算延迟时间
                long delay = millis - now;
                //判断是否到了延迟时间,如果到了,直接返回
                if (delay <= 0) {
                    break;
                }
                //继续等待
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }
 

Thread.join()实际上调用的是Thread.join(long millis)方法,只是millis的值为0

当前线程终止时,会调用线程自身的notifyAll()方法,会通知所有等待在该线程对象上的线程。可以看到join方法的逻辑结果与等待/通知经典范式一致。

转载于:https://my.oschina.net/u/4122764/blog/3045548