一、前言
通过一个简单的例子引入join()方法
public class Thread01 extendsThread{
@Overridepublic voidrun() {for(int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+ "=" +i);
}
}
}
main线程中起两个线程
public classTest {public static void main(String[] args) throwsInterruptedException {
Thread01 thread01= newThread01();
Thread01 thread02= newThread01();
thread01.start();
thread02.start();
}
}
结果:
Thread-1=0Thread-0=0Thread-0=1Thread-0=2Thread-1=1Thread-0=3Thread-1=2Thread-0=4Thread-1=3Thread-1=4
说明:可以看到,thread01和thread02并发执行,没有先后顺序,现在在thread01.start()之后加入join()方法
public classTest {public static void main(String[] args) throwsInterruptedException {
Thread01 thread01= newThread01();
Thread01 thread02= newThread01();
thread01.start();
thread01.join();
System.out.println("main thread after join");
thread02.start();
}
}
其他不变,看一下结果:
Thread-0=0Thread-0=1Thread-0=2Thread-0=3Thread-0=4main thread after join
Thread-1=0Thread-1=1Thread-1=2Thread-1=3Thread-1=4
说明:可以看到,thread01在调用join()后,会优先执行,等它执行完了,才会执行thread02。在分析main线程中的执行过程之前,先看一下join()方法的源码。
二、join()/join(long millis)
/*** Waits for this thread to die.
*
*
An invocation of this method behaves in exactly the same
* way as the invocation
*
*
* {@linkplain#join(long) join}{@code(0)}
*
*
*@throwsInterruptedException
* if any thread has interrupted the current thread. The
* interrupted status of the current thread is
* cleared when this exception is thrown.*/
public final void join() throwsInterruptedException {
join(0);
}
join()方法调用了join(long millis)方法,注意下面这个注释:意思是最多等待几ms线程(比如thread01)结束,但若是在指定的时间内没有结束,那么调用该方法的线程(calling thread 比如main)会被唤醒,和thread01并发执行。
Waits at most {@code millis} milliseconds for this thread to die
/*** Waits at most {@codemillis} milliseconds for this thread to
* die. A timeout of {@code0} means to wait forever.
*
*
This implementation uses a loop of {@codethis.wait} calls
* conditioned on {@codethis.isAlive}. As a thread terminates the
* {@codethis.notifyAll} method is invoked. It is recommended that
* applications not use {@codewait}, {@codenotify}, or
* {@codenotifyAll} on {@codeThread} instances.
*
*@parammillis
* the time to wait in milliseconds
*
*@throwsIllegalArgumentException
* if the value of {@codemillis} is negative
*
*@throwsInterruptedException
* if any thread has interrupted the current thread. The
* interrupted status of the current thread is
* cleared when this exception is thrown.*/
public final synchronized void join(longmillis)throwsInterruptedException {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;
}
}
}
join(long millis)是同步方法,该方法是通过循环判断线程的isAlive的值(调用join方法的线程)来调用wait方法。当有线程执行完了,notifyAll方法会被调用,使其他处于等待的线程开始执行。
分析一下main方法中的执行过程。在main线程中,首先thread01依次调用start()-->join()-->join(0)方法,因为join(long millis)是同步方法,main线程会去获取与thread01对象关联的monitor的所有权(The thread that executes monitorenter attempts to gain ownership of the monitor associated with objectref),thread01对象关联的monitor还没有被别的线程所有,所以main线程进入该同步方法,传入的参数millis为0,接着判断thread01线程isAlive(Tests if this thread is alive. A thread is alive if it has been started and has not yet died),为true,调用wait()方法,main线程处于等待状态,而thread01线程继续执行,当thread01线程执行完毕,join(long millis)方法结束,notifyAll()方法会被调用,main线程被唤醒,继续执行,打印出"main thread after join"语句,执行thread02线程。