概要
本文分三个部分对Thread.join()进行分析:
1. join() 的示例和作用
2. join() 源码分析
3. 对网上其他分析 join() 的文章提出疑问
1. join() 的示例和作用
1.1 示例
// 父线程 public class Parent { public static void main(String[] args) { // 创建child对象,此时child表示的线程处于NEW状态 Child child = new Child(); // child表示的线程转换为RUNNABLE状态 child.start(); // 等待child线程运行完再继续运行 child.join(); } }
// 子线程 public class Child extends Thread { public void run() { // ... } }
上面代码展示了两个类:Parent(父线程类),Child(子线程类)。
Parent.main()方法是程序的入口,通过Child child = new Child(); 新建child子线程(此时 child子线程处于NEW状态);
然后调用child.start()(child子线程状态转换为RUNNABLE);
再调用child.join(),此时,Parent父线程会等待child子线程运行完再继续运行。
下图是我总结的 Java 线程状态转换图:
1.2 join() 的作用
让父线程等待子线程结束之后才能继续运行。
我们来看看在 Java 7 Concurrency Cookbook 中相关的描述(很清楚地说明了 join() 的作用):
Waiting for the finalization of a thread
In some situations, we will have to wait for the finalization of a thread. For example, we mayhave a program that will begin initializing the resources it needs before proceeding with therest of the execution. We can run the initialization tasks as threads and wait for its finalizationbefore continuing with the rest of the program.For this purpose, we can use the join() method of the Thread class. When we call thismethod using a thread object, it suspends the execution of the calling thread until the objectcalled finishes its execution.
当我们调用某个线程的这个方法时,这个方法会挂起调用线程,直到被调用线程结束执行,调用线程才会继续执行。
2. join() 源码分析
以下是 JDK 8 中 join() 的源码:
public final void join() throws InterruptedException { join(0); } public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } } public final synchronized void join(long millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis++; } join(millis); }
join() 一共有三个重载版本,分别是无参、一个参数、两个参数:
public final void join() throws InterruptedException; public final synchronized void join(long millis) throws InterruptedException; public final synchronized void join(long millis, int nanos) throws InterruptedException;