Thread.join源码解析

/*
 *
 * Waits for this thread to die.
 *等待该线程终止的时间最长为 millis 毫秒。超时为 0 意味着要一直等下去。
 * 

An invocation of this method behaves in exactly the same * way as the invocation * *

* {@linkplain #join(long) join}{@code (0)} *
* @throws InterruptedException  * 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除 * 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() throws InterruptedException { join(0); }
/**
 * Waits at most {@code millis} milliseconds for this thread to
 * die. A timeout of {@code 0} means to wait forever.
 *
 * 

This implementation uses a loop of {@code this.wait} calls * conditioned on {@code this.isAlive}. As a thread terminates the * {@code this.notifyAll} method is invoked. It is recommended that * applications not use {@code wait}, {@code notify}, or * {@code notifyAll} on {@code Thread} instances. * * @param millis * the time to wait in milliseconds * * @throws IllegalArgumentException * if the value of {@code millis} is negative * * @throws InterruptedException * 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(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; } } }

isAlive()方法:判断当前线程是否处于活动状态.(“活动状态”是指线程处于运行或者准备开始运行的状态。)

我的理解是:通过while (isAlive())判断线程是不是start()了,其mills = 0,然后执行wait(0);

Thread.join源码解析_第1张图片

可以看到在没有start(),直接join()它的mills = 0,但是它的isAlive()= false。

你可能感兴趣的:(并发)