• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

浅谈Java线程Thread.join方法解析

java 搞代码 4年前 (2022-01-05) 22次浏览 已收录 0个评论

本篇文章主要介绍了浅谈Java线程Thread.join方法解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

join字面上是加入的意思,我们先看看join方法的解释和实现。

 /** * Waits for this thread to die. * 调用方线程(调用join方法的线程)执行等待操作,直到被调用的线程(join方法所属的线程)结束,再被唤醒 * <p> An invocation of this method behaves in exactly the same * way as the invocation * * * @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是调用的

 /** * Waits at most {@code millis} milliseconds for this thread to * die. A timeout of {@code 0} means to wait forever. * 等待线程执行结束,或者指定的最大等待时间到了,调用方线程再次被唤醒,如果最大等待时间为0,则只能等线程执行结束,才能被唤醒。 * <p> Th<i style="color:transparent">来源gaodai$ma#com搞$$代**码网</i>is 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. * * */ 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; } } } 

可以看到,join方法本身是通过wait方法来实现等待的,这里判断如果线程还在运行中的话,则继续等待,如果指定时间到了,或者线程运行完成了,则代码继续向下执行,调用线程就可以执行后面的逻辑了。

但是在这里没有看到哪里调用notify或者notifyAll方法,如果没有调用的话,那调用方线程会一直等待下去,那是哪里调用了唤醒它的方法呢?通过查证得知,原来在线程结束时,java虚拟机会执行该线程的本地exit方法,

 //线程退出函数: void JavaThread::exit(bool destroy_vm, ExitType exit_type) { ... //这里会处理join相关的销毁逻辑 ensure_join(this); ... } //处理join相关的销毁逻辑 static void ensure_join(JavaThread* thread) { Handle threadObj(thread, thread->threadObj()); ObjectLocker lock(threadObj, thread); thread->clear_pending_exception(); java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED); java_lang_Thread::set_thread(threadObj(), NULL); //这里就调用notifyAll方法,唤醒等待的线程 lock.notify_all(thread); thread->clear_pending_exception(); } 

这样线程什么时候被唤醒就明白了。下面写个例子看下效果。

 public class JoinTest { public static void main(String[] args) { ThreadBoy boy = new ThreadBoy(); boy.start(); } static class ThreadBoy extends Thread{ @Override public void run() { System.out.println("男孩和女孩准备出去逛街"); ThreadGirl girl = new ThreadGirl(); girl.start(); try { girl.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("男孩和女孩开始去逛街了"); } } static class ThreadGirl extends Thread{ @Override public void run() { int time = 5000; System.out.println("女孩开始化妆,男孩在等待。。。"); try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("女孩化妆完成!,耗时" + time); } } } 

执行结果为:

男孩和女孩准备出去逛街
女孩开始化妆,男孩在等待。。。
女孩化妆完成!,耗时5000
男孩和女孩开始去逛街了

就是男孩和女孩准备去逛街,女孩要化妆先,等女孩化妆完成了,再一起去逛街。

那join(time)的用法是怎么样的呢?

 public class JoinTest { public static void main(String[] args) { ThreadBoy boy = new ThreadBoy(); boy.start(); } static class ThreadBoy extends Thread{ @Override public void run() { System.out.println("男孩和女孩准备出去逛街"); ThreadGirl girl = new ThreadGirl(); girl.start(); int time = 2000; try { girl.join(time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("男孩等了" + time + ", 不想再等了,去逛街了"); } } static class ThreadGirl extends Thread{ @Override public void run() { int time = 5000; System.out.println("女孩开始化妆,男孩在等待。。。"); try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("女孩化妆完成!,耗时" + time); } } } 

这里仅仅把join方法换成了join(time)方法,描述改了点,打印的结果是:

男孩和女孩准备出去逛街
女孩开始化妆,男孩在等待。。。
男孩等了2000, 不想再等了,去逛街了
女孩化妆完成!,耗时5000

男孩等了join(time)中的time时间,如果这个time时间到达之后,女孩所在的线程还没执行完,则不等待了,继续执行后面的逻辑,就是不等女孩了,自己去逛街。

由此看出,join方法是为了比较方便的实现两个线程的同步执行,线程1执行,碰到线程2后,等待线程2执行后,再继续执行线程1的执行,加入的意思现在就比较形象化了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是浅谈Java线程Thread.join方法解析的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:浅谈Java线程Thread.join方法解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址