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

内容导航

  • Thread.join的作用

  • Thread.join的实现原理

  • 什么时候会使用Thread.join

Thread.join的作用

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

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

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;

       }

   }

}

  

这个问题最简单的回答是通过Thread.join来实现,久而久之就让很多人误以为Thread.join是用来保证线程的顺序性的。 下面这段代码演示了Thread.join的作用

上面的代码,注意 previousThread.join部分,大家可以把这行代码注释以后看看运行效果,在没有加join的时候运行的结果是不确定的。加了join以后,运行结果按照递增的顺序展示出来。

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

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本身的实例。

有很多人不理解join为什么阻塞的是主线程呢? 不理解的原因是阻塞主线程的方法是放在previousThread这个实例作用,让大家误以为应该阻塞previousThread线程。实际上主线程会持有previousThread这个对象的锁,然后调用wait方法去阻塞,而这个方法的调用者是在主线程中的。所以造成主线程阻塞。

第二个问题,为什么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)这行代码上的注释,唤醒处于等待的线程对象,这个是在线程终止之后做的清理工作,这个方法的定义代码片段如下

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();

}

  

Java Thread.join的作用和原理的更多相关文章

  1. [译]Java Thread join示例与详解

    Java Thread join示例与详解 Java Thread join方法用来暂停当前线程直到join操作上的线程结束.java中有三个重载的join方法: public final void ...

  2. java.lang.ThreadLocal的作用和原理?列举在哪些程序中见过ThreadLocal的使用?

    java.lang.ThreadLocal的作用和原理?列举在哪些程序中见过ThreadLocal的使用? 说明类java.lang.ThreadLocal的作用和原理.列举在哪些程序中见过Threa ...

  3. Java Thread.join()方法

    一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...

  4. Java Thread join() 的用法

    Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...

  5. 【转】Java Thread.join()详解

    http://www.open-open.com/lib/view/open1371741636171.html 一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: ? 1 ...

  6. Java Thread.join()详解(转)

    (1)join方法是可以中断的(2)在线程joiner在另一个线程t上调用t.join(),线程joiner将被挂起,直到线程t结束(即t.isAlive()返回为false)才恢复 package ...

  7. JAVA THREAD.JOIN方法详解

    一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...

  8. Java Thread.join()详解

    一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法   一.使用方式. join是Thread类的 ...

  9. 浅析 Java Thread.join()

    转自:http://blog.csdn.net/bzwm/article/details/3881392 一.在研究join的用法之前,先明确两件事情. 1.join方法定义在Thread类中,则调用 ...

随机推荐

  1. sql server 高可用故障转移(上)

    群集准备工作 个人电脑 内存12G,处理器 AMD A6-3650CPU主频2.6GHz 虚拟机 VMware Workstation 12 数据库  sql server 2008 r2 三台虚拟服 ...

  2. 第二章——机器学习项目完整案例(End-to-End Machine Learning Project)

    本章通过一个例子,介绍机器学习的整个流程. 2.1 使用真实数据集练手(Working with Real Data) 国外一些获取数据的网站: Popular open data repositor ...

  3. Java Persistence/ManyToMany

    A ManyToMany relationship in Java is where the source object has an attribute that stores a collecti ...

  4. 关于js中对象和函数的一道问题

    今天在写代码的时候遇到一个问题,没有解决,又在sf上提问了..冏..,最后讨论良久终于解决,和往常一样,做一个备忘,最近打算把印象笔记里的东西迁来.具体可访问http://segmentfault.c ...

  5. ORA-12737: Instant Client Light: unsupported server character set CHS16GBK

    当使用Navicat Premiun 英文版连接oracl时可能会报ORA-12737: Instant Client Light: unsupported server character set ...

  6. version.go

    package blog4go const ( // 版本号 VERSION = "0.5.6" )

  7. Java借助CountDownLatch完成异步回调

    public class AsyncDemo { private static void doSomeTask() { System.out.println("Hello World&quo ...

  8. 【线段树】Bzoj1230 [Usaco2008 Nov]lites 开关灯

    Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每 ...

  9. BZOJ_2788_[Poi2012]Festival_差分约束+tarjan+floyed

    BZOJ_2788_[Poi2012]Festival_差分约束+tarjan+floyed Description 有n个正整数X1,X2,...,Xn,再给出m1+m2个限制条件,限制分为两类: ...

  10. BZOJ_3144_[Hnoi2013]切糕_最小割

    BZOJ_3144_[Hnoi2013]切糕_最小割 Description Input 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R ...