一个线程可以在其他线程上调用join()方法,其效果是等待一段时间直到第二个线程结束才能继续执行。
JRE的注释中写道:Waits for this thread to die。
Join仅仅是等待当前线程执行结束,在等待过程中会释放锁资源。
/** * 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); }
示例代码:
/** * * @author zhangwei_david * @version $Id: One.java, v 0.1 2015年5月18日 上午10:40:37 zhangwei_david Exp $ */ public class One extends Thread { /** * @see java.lang.Thread#run() */ @Override public void run() { System.out.println(super.getName() + " is running...."); System.out.println(super.getName() + " end."); } public One(String name) { super(name); } }
/** * * @author zhangwei_david * @version $Id: Two.java, v 0.1 2015年5月18日 上午10:41:41 zhangwei_david Exp $ */ public class Two extends Thread { private Thread one; /** * @see java.lang.Thread#run() */ @Override public void run() { System.out.println(" two is running...."); one.start(); try { one.join(); } catch (InterruptedException e) { } System.out.println(" two end"); } public Two(Thread one) { super(); this.one = one; } }
/** * * @author zhangwei_david * @version $Id: JoinDemo.java, v 0.1 2015年5月17日 下午3:32:45 zhangwei_david Exp $ */ public class JoinDemo { /** * * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { Thread one = new One("One"); Thread two = new Two(one); two.start(); } }
从结果中可以发现在线程Two 被挂起,直到线程One执行完成后才继续执行。
two is running.... One is running.... One end. two end
那么join 是如何实现的呢?我们可以看看源码。
public final synchronized void join(long millis) throws InterruptedException { // 获取当前时间 long base = System.currentTimeMillis(); long now = 0; // 超时时间小于0 if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } // 超时时间等于0 if (millis == 0) { //判断当前线程是否是启动的,如果是启动的一直等待到线程的执行结束也就是没有超时时间 while (isAlive()) { // 如果是启动的则等待0毫秒 wait(0); } // 如果超时时间大于0 } else { // 当前线程是启动状态,则一直等待到线程执行结束或者超时 while (isAlive()) { // 获取延迟时间 long delay = millis - now; // 延迟时间小于或等于0 则不需要等待 if (delay <= 0) { break; } // 等待 delay 毫秒 wait(delay); // 设置now 值 now = System.currentTimeMillis() - base; } } }
/** *一直等待到其他线程notify(),notifyAll()或者是超时 * **/ public final native void wait(long timeout) throws InterruptedException;
相关推荐
是第一次在C#中接触Thread,自己研究了一下其中Thread.Join()这个方法,下面谈谈自己的理解。 Thread.Join()在MSDN中的解释很模糊:Blocks the calling thread until a thread terminates 有两个主要问题: ...
CountDownLatch与thread.join()的区别
线程创建,线程等待与清除,thread_clean.c,thread_create.c,thread_create.c,thread_exit_struct.c,thread_id.c,thread_int.c, thread_join.c, thread_join.c, thread_string.c, thread_struct.c
本文实例讲述了C#多线程之Thread中Thread.Join()函数用法。分享给大家供大家参考。具体分析如下: Thread.Join()在MSDN中的解释:Blocks the calling thread until a thread terminates 当NewThread调用Join方法的...
主要介绍了c++11中关于std::thread的join详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了java 中Thread.join()的使用方法的相关资料,需要的朋友可以参考下
主要介绍了Java中Thread.join()的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了JAVA CountDownLatch与thread-join()的区别解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
join() 定义在Thread.java中。 join() 的作用:让“主线程”等待“子线程”结束之后才能继续运行。这句话可能有点晦涩,我们还是通过例子去理解
主要介绍了Java之Thread的join方法,实例形式讲述了join方法的应用,需要的朋友可以参考下
1,简介 C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准中的一些表示线程的...缺省构造的thread对象、已经完成join的thread对象、已
本文对java Thread中join()方法进行介绍,join()的作用是让“主线程”等待“子线程”结束之后才能继续运行,大家参考使用吧
普通线程的创建,通过 委托ThreadStart对应的函数来执行相关操作; 通过线程池,可以直接从池中查找出空闲线程,让它执行委托WaitCallback对应函数来执行相关操作。使用时要与AutoResetEvent来并用,以在线程结束时...
本篇文章主要介绍了浅谈Java线程Thread.join方法解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
c++11多线程库的使用,主要介绍了thread类中的构造函数 join函数和detach函数的使用方式
Thread.join() 作用为阻塞主线程,即在子线程未返回的时候,主线程等待其返回然后再继续执行. join不能与start在循环里连用 以下为错误代码,代码创建了5个线程,然后用一个循环激活线程,激活之后令其阻塞主线程. ...
本文实例讲述了Python中threading模块join函数用法。分享给大家供大家参考。具体分析如下: ... thread_arr[i].join() 此处join的原理就是依次检验线程池中的线程是否结束,没有结束就阻塞直到线程结束
2 如果创建线程,并且设置了daemon为true,即thread.setDaemon(True), 则主线程执行完毕后自动退出,不会等待子线程的执行结果。而且随着主线程退出,子线程也消亡。 3 join方法的作用是阻塞,等待子线程结束,join...