10. java中join的使用

join()应该是我们在java中经常会用到的一个方法,它主要是将当前线程置为WAITTING状态,然后等待调用的线程执行完毕或被interrupted。

join()是Thread中定义的方法,我们看下他的定义:

   /**
     * Waits for this thread to die.
     *
     * <p> An invocation of this method behaves in exactly the same
     * way as the invocation
     *
     * <blockquote>
     * {@linkplain #join(long) join}{@code (0)}
     * </blockquote>
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public final void join() throws InterruptedException {
        join(0);
    }

我们看下join是怎么使用的,通常我们需要在线程A中调用线程B.join():

我们在主线程中调用了t2.join(),则主线程将会等待t2执行完毕,我们看下输出结果:

当线程已经执行完毕或者还没开始执行的时候,join()将会立即返回:

join还有两个带时间参数的方法:

如果在给定的时间内调用的线程没有返回,则主线程将会继续执行:

上面的例子将会输出:

Join()还有个happen-before的特性,这就是如果thread t1调用 t2.join(), 那么当t2返回时,所有t2的变动都会t1可见。

之前我们讲volatile关键词的时候也提到了这个happen-before规则。 我们看下例子:

我们运行下,可以看到while循环一直在进行中,即使t4中的变量已经变成了0。

所以如果我们需要在这种情况下使用的话,我们需要用到join(),或者其他的同步机制。

本文的例子可以参考https://github.com/ddean2009/learn-java-concurrency/tree/master/join

更多教程请参考 flydean的博客

最后更新于

这有帮助吗?