(四)Thread.join的做用和原理

文章简介

不少人对Thread.join的做用以及实现了解得不多,毕竟这个api咱们不多使用。这篇文章仍然会结合使用及原理进行深度分析java

内容导航

  1. Thread.join的做用
  2. Thread.join的实现原理
  3. 何时会使用Thread.join

Thread.join的做用

以前有人问过我一个这样的面试题面试

Java中如何让多线程按照本身指定的顺序执行?

这个问题最简单的回答是经过Thread.join来实现,长此以往就让不少人误觉得Thread.join是用来保证线程的顺序性的。
下面这段代码演示了Thread.join的做用api

public class JoinDemo extends Thread{
    int i;
    Thread previousThread; //上一个线程
    public JoinDemo(Thread previousThread,int i){
        this.previousThread=previousThread;
        this.i=i;
    }
    @Override
    public void run() {
        try {
          //调用上一个线程的join方法,你们能够本身演示的时候能够把这行代码注释掉
            previousThread.join(); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("num:"+i);
    }
    public static void main(String[] args) {
        Thread previousThread=Thread.currentThread();
        for(int i=0;i<10;i++){
            JoinDemo joinDemo=new JoinDemo(previousThread,i);
            joinDemo.start();
            previousThread=joinDemo;
        }
    }
}

上面的代码,注意 previousThread.join部分,你们能够把这行代码注释之后看看运行效果,在没有加join的时候运行的结果是不肯定的。加了join之后,运行结果按照递增的顺序展现出来。多线程

thread.join的含义是当前线程须要等待previousThread线程终止以后才从thread.join返回。简单来讲,就是线程没有执行完以前,会一直阻塞在join方法处。

下面的图表现了join对于线程的做用
图片描述ide

Thread.join的实现原理

线程是如何被阻塞的?又是经过什么方法唤醒的呢?先来看看Thread.join方法作了什么事情
public class Thread implements Runnable {
    ...
    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) { //判断是否携带阻塞的超时时间,等于0表示没有设置超时时间
            while (isAlive()) {//isAlive获取线程状态,无线等待直到previousThread线程结束
                wait(0); //调用Object中的wait方法实现线程的阻塞
            }
        } else { //阻塞直到超时
            while (isAlive()) { 
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }
    ...

从join方法的源码来看,join方法的本质调用的是Object中的wait方法实现线程的阻塞,wait方法的实现原理咱们在后续的文章再说详细阐述。可是咱们须要知道的是,调用wait方法必需要获取锁,因此join方法是被synchronized修饰的,synchronized修饰在方法层面至关于synchronized(this),this就是previousThread自己的实例。this

有不少人不理解join为何阻塞的是主线程呢? 不理解的缘由是阻塞主线程的方法是放在previousThread这个实例做用,让你们误觉得应该阻塞previousThread线程。实际上主线程会持有previousThread这个对象的锁,而后调用wait方法去阻塞,而这个方法的调用者是在主线程中的。因此形成主线程阻塞。spa

第二个问题,为何previousThread线程执行完毕就可以唤醒住线程呢?或者说是在何时唤醒的?

要了解这个问题,咱们又得翻jdk的源码,可是若是你们对线程有必定的基本了解的话,经过wait方法阻塞的线程,须要经过notify或者notifyall来唤醒。因此在线程执行完毕之后会有一个唤醒的操做,只是咱们不须要关心。
接下来在hotspot的源码中找到 thread.cpp,看看线程退出之后有没有作相关的事情来证实咱们的猜测.线程

void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
  assert(this == JavaThread::current(),  "thread consistency check");
  ...
  // Notify waiters on thread object. This has to be done after exit() is called
  // on the thread (if the thread is the last thread in a daemon ThreadGroup the
  // group should have the destroyed bit set before waiters are notified).
  ensure_join(this); 
  assert(!this->has_pending_exception(), "ensure_join should have cleared");
  ...

观察一下 ensure_join(this)这行代码上的注释,唤醒处于等待的线程对象,这个是在线程终止以后作的清理工做,这个方法的定义代码片断以下code

static void ensure_join(JavaThread* thread) {
  // We do not need to grap the Threads_lock, since we are operating on ourself.
  Handle threadObj(thread, thread->threadObj());
  assert(threadObj.not_null(), "java thread object must exist");
  ObjectLocker lock(threadObj, thread);
  // Ignore pending exception (ThreadDeath), since we are exiting anyway
  thread->clear_pending_exception();
  // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
  java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
  // Clear the native thread instance - this makes isAlive return false and allows the join()
  // to complete once we've done the notify_all below
  //这里是清除native线程,这个操做会致使isAlive()方法返回false
  java_lang_Thread::set_thread(threadObj(), NULL);
  lock.notify_all(thread);//注意这里
  // Ignore pending exception (ThreadDeath), since we are exiting anyway
  thread->clear_pending_exception();
}

ensure_join方法中,调用 lock.notify_all(thread); 唤醒全部等待thread锁的线程,意味着调用了join方法被阻塞的主线程会被唤醒; 到目前为止,咱们基本上对join的原理作了一个比较详细的分析对象

总结,Thread.join其实底层是经过wait/notifyall来实现线程的通讯达到线程阻塞的目的;当线程执行结束之后,会触发两个事情,第一个是设置native线程对象为null、第二个是经过notifyall方法,让等待在previousThread对象锁上的wait方法被唤醒。

何时会使用Thread.join

在实际应用开发中,咱们不多会使用thread.join。在实际使用过程当中,咱们能够经过join方法来等待线程执行的结果,其实有点相似future/callable的功能。
咱们经过如下伪代码来讲明join的使用场景

public void joinDemo(){
   //....
   Thread t=new Thread(payService);
   t.start();
   //.... 
   //其余业务逻辑处理,不须要肯定t线程是否执行完
   insertData();
   //后续的处理,须要依赖t线程的执行结果,能够在这里调用join方法等待t线程执行结束
   t.join();
}

扫码关注公众号

相关文章
相关标签/搜索