Thread.join():join()的作用是“等待该进程终止”,也就是在子线程调用了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
从源码分析join方法:
(1)join():表示无限等待,它会一直阻塞当前线程,至到目标线程执行完毕
public final void join() throws InterruptedException {
join(0);
}
(2)join(long millis, int nanos):表示等待目标线程终止时间最长为millis毫秒+nanos纳秒
public final synchronized void join(long millis, int nanos) throws InterruptedException {
if (millis < 0) {
throw new IllegalArgumentException("timeout value i