全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Java多线程中关于join方法的使用实例解析

先上代码

新建一个Thread,代码如下:

package com.thread.test;
public class MyThread extends Thread {
  private String name;
  public MyThread(String name) {
    this.name = name;
  }
  @Override
  public void run() {
    for (int i = 0; i < 100; i++) {
      System.out.println(name+"["+i+"]");
    }
    super.run();
  }
}

之后新建测试类,代码如下:

package com.thread.test;
/*
 * 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
 */
public class ThreadDemo{
  public static void main(String[] args) {
    MyThread t = new MyThread("A");
    t.start();
    for (int i = 0; i < 100; i++) {
      if (i>50) {
        try {
          t.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("主线程"+"["+i+"]");
    }
  }
}

下面是Java Platform SE8 API中对Thread中Join方法的解释:

public final void join(long millis)
        throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever. 
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
Parameters: 
millis - the time to wait in milliseconds 
Throws: 
IllegalArgumentException - if the value of millis is negative 
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

先上代码

新建一个Thread,代码如下:

package com.thread.test;
public class MyThread extends Thread {
  private String name;
  public MyThread(String name) {
    this.name = name;
  }
  @Override
  public void run() {
    for (int i = 0; i < 100; i++) {
      System.out.println(name+"["+i+"]");
    }
    super.run();
  }
}

之后新建测试类,代码如下:

package com.thread.test;
/*
 * 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
 */
public class ThreadDemo{
  public static void main(String[] args) {
    MyThread t = new MyThread("A");
    t.start();
    for (int i = 0; i < 100; i++) {
      if (i>50) {
        try {
          t.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("主线程"+"["+i+"]");
    }
  }
}

下面是Java Platform SE8 API中对Thread中Join方法的解释:

public final void join(long millis)
        throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever. 
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
Parameters: 
millis - the time to wait in milliseconds 
Throws: 
IllegalArgumentException - if the value of millis is negative 
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

 我自己的理解就是会强行进入使用join方法的线程,其他线程等待该线程完全执行完后才会进来。


# java多线程join方法  # java基本教程之join方法详解 java多线程教程  # java 中Thread.join()的使用方法  # Java之Thread的join方法实例  # java线程之join方法的使用介绍  # java多线程编程之join方法的使用示例  # Java使用join方法暂停当前线程  # JAVA多线程之方法 JOIN详解及实例代码  # Java字符串拼接新方法 StringJoiner用法详解  # 浅谈Java线程Thread.join方法解析  # java中join方法的理解与说明详解  # 的是  # 完后  # 中对  # 新建一个  # 自己的  # 才会  # nbsp  # ThreadDemo  # thrown  # super  # lt  # println  # System  # catch  # join  # printStackTrace  # InterruptedException  # gt  # main  # static 


相关文章: 佛山企业网站制作公司有哪些,沟通100网上服务官网?  网站代码制作软件有哪些,如何生成自己网站的代码?  Android滚轮选择时间控件使用详解  如何通过VPS建站无需域名直接访问?  建站之星云端配置指南:模板选择与SEO优化一键生成  如何在自有机房高效搭建专业网站?  网站制作新手教程,新手建设一个网站需要注意些什么?  如何通过建站之星自助学习解决操作问题?  建站之星IIS配置教程:代码生成技巧与站点搭建指南  专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?  建站之星微信建站一键生成小程序+多端营销系统  定制建站如何定义?其核心优势是什么?  大型企业网站制作流程,做网站需要注册公司吗?  如何通过商城免费建站系统源码自定义网站主题?  如何通过可视化优化提升建站效果?  江苏网站制作公司有哪些,江苏书法考级官方网站?  建站之星上传入口如何快速找到?  网站设计制作企业有哪些,抖音官网主页怎么设置?  定制建站是什么?如何实现个性化需求?  香港服务器网站卡顿?如何解决网络延迟与负载问题?  建站主机与虚拟主机有何区别?如何选择最优方案?  山东网站制作公司有哪些,山东大源集团官网?  C++用Dijkstra(迪杰斯特拉)算法求最短路径  网站制作服务平台,有什么网站可以发布本地服务信息?  mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?  如何通过商城自助建站源码实现零基础高效建站?  北京网站制作的公司有哪些,北京白云观官方网站?  如何在Ubuntu系统下快速搭建WordPress个人网站?  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?  东莞专业网站制作公司有哪些,东莞招聘网站哪个好?  如何快速搭建高效可靠的建站解决方案?  如何在云虚拟主机上快速搭建个人网站?  阿里云高弹*务器配置方案|支持分布式架构与多节点部署  网站制作网站,深圳做网站哪家比较好?  头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换  如何通过宝塔面板实现本地网站访问?  官网建站费用明细查询_企业建站套餐价格及收费标准指南  青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?  建站之星后台管理:高效配置与模板优化提升用户体验  如何在云主机上快速搭建多站点网站?  太原网站制作公司有哪些,网约车营运证查询官网?  建站之星如何实现网站加密操作?  建站之星导航菜单设置与功能模块配置全攻略  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)  如何快速搭建高效香港服务器网站?  广州顶尖建站服务:企业官网建设与SEO优化一体化方案  已有域名和空间如何快速搭建网站?  jQuery 常见小例汇总 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。