全网整合营销服务商

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

免费咨询热线: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 


相关文章: 在线制作视频的网站有哪些,电脑如何制作视频短片?  如何通过老薛主机一键快速建站?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?  如何用PHP工具快速搭建高效网站?  建站之星如何防范黑客攻击与数据泄露?  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  如何用搬瓦工VPS快速搭建个人网站?  韩国服务器如何优化跨境访问实现高效连接?  武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?  在线教育网站制作平台,山西立德教育官网?  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  网站图片在线制作软件,怎么在图片上做链接?  小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建  定制建站平台哪家好?企业官网搭建与快速建站方案推荐  合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?  建站之星备案流程有哪些注意事项?  简易网站制作视频教程,使用记事本编写一个简单的网页html文件?  兔展官网 在线制作,怎样制作微信请帖?  成都网站制作公司哪家好,四川省职工服务网是做什么用?  平台云上自主建站:模板化设计与智能工具打造高效网站  如何生成腾讯云建站专用兑换码?  全景视频制作网站有哪些,全景图怎么做成网页?  小建面朝正北,A点实际方位是否存在偏差?  建站主机系统SEO优化与智能配置核心关键词操作指南  建站之星会员如何解锁更多建站功能?  详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)  浅谈Javascript中的Label语句  Android自定义控件实现温度旋转按钮效果  天津个人网站制作公司,天津网约车驾驶员从业资格证官网?  建站VPS选购需注意哪些关键参数?  建站之星体验版:智能建站系统+响应式设计,多端适配快速建站  建站之星后台管理系统如何操作?  怎么将XML数据可视化 D3.js加载XML  建站之星与建站宝盒如何选择最佳方案?  外汇网站制作流程,如何在工商银行网站上做外汇买卖?  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  如何获取免费开源的自助建站系统源码?  如何快速搭建高效简练网站?  如何通过FTP空间快速搭建安全高效网站?  建站之星如何取消后台验证码生成?  广州网站建站公司选择指南:建站流程与SEO优化关键词解析  家庭服务器如何搭建个人网站?  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  C++如何使用std::optional?(处理可选值)  建站之星安装失败:服务器环境不兼容?  网站制作公司,橙子建站是合法的吗?  如何快速搭建高效服务器建站系统?  制作企业网站建设方案,怎样建设一个公司网站?  网站建设制作、微信公众号,公明人民医院怎么在网上预约? 

您的项目需求

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