Thread.join()

Thread.join()方法用于让当前线程等待指定线程结束后再继续执行。它有三种形式,包括无限等待、指定时间等待和超时等待。当调用join()时,当前线程将进入等待状态,直到被join的线程执行完毕。通过示例代码展示了join()方法的使用,演示了如何控制线程的执行顺序和同步。
摘要由CSDN通过智能技术生成

Thread.join():join()的作用是“等待该进程终止”,也就是在子线程调用了join()方法后,主线程后面的代码要等到子线程结束了才能执行。一般应用于一个线程的输入可能依赖于另一个或者多个线程的输出,此时这个线程就需要等待依赖线程执行完毕才能继续执行。

public final void join() throws InterruptedException 
public final synchronized void join(long millis) throws InterruptedException 
public final synchronized void join(long millis, int nanos) throws InterruptedException

从源码分析join方法:

(1)join():表示无限等待,它会一直阻塞当前线程,至到目标线程执行完毕

public final void join() throws InterruptedException { 
   join(0); 
}

(2)join(long millis, int nanos):表示等待目标线程终止时间最长为millis毫秒+nanos纳秒

public final synchronized void join(long millis, int nanos) throws InterruptedException { 
      if (millis < 0) { 
          throw new IllegalArgumentException("timeout value i
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值