全网整合营销服务商

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

免费咨询热线: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优化双核驱动策略  义乌企业网站制作公司,请问义乌比较好的批发小商品的网站是什么?  保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?  如何在IIS7中新建站点?详细步骤解析  北京的网站制作公司有哪些,哪个视频网站最好?  如何通过建站之星自助学习解决操作问题?  建站之星导航配置指南:自助建站与SEO优化全解析  建站之星logo尺寸如何设置最合适?  枣阳网站制作,阳新火车站打的到仙岛湖多少钱?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  网站制作需要会哪些技术,建立一个网站要花费多少?  深圳 网站制作,深圳招聘网站哪个比较好一点啊?  如何在IIS中配置站点IP、端口及主机头?  如何用5美元大硬盘VPS安全高效搭建个人网站?  建站org新手必看:2024最新搭建流程与模板选择技巧  网站制作费用多少钱,一个网站的运营,需要哪些费用?  如何快速搭建FTP站点实现文件共享?  Python路径拼接规范_跨平台处理说明【指导】  如何快速生成高效建站系统源代码?  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?  北京网站制作公司哪家好一点,北京租房网站有哪些?  制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?  建设网站制作价格,怎样建立自己的公司网站?  广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?  建站主机核心功能解析:服务器选择与网站搭建流程指南  如何快速搭建高效简练网站?  如何选择域名并搭建高效网站?  网站图片在线制作软件,怎么在图片上做链接?  电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?  Bpmn 2.0的XML文件怎么画流程图  如何自定义建站之星模板颜色并下载新样式?  制作网站外包平台,自动化接单网站有哪些?  专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?  电商平台网站制作流程,电商网站如何制作?  公众号网站制作网页,微信公众号怎么制作?  无锡营销型网站制作公司,无锡网选车牌流程?  北京企业网站设计制作公司,北京铁路集团官方网站?  行程制作网站有哪些,第三方机票电子行程单怎么开?  如何通过VPS建站实现广告与增值服务盈利?  宝塔新建站点为何无法访问?如何排查?  定制建站平台哪家好?企业官网搭建与快速建站方案推荐  早安海报制作网站推荐大全,企业早安海报怎么每天更换?  企业微网站怎么做,公司网站和公众号有什么区别?  网站制作说明怎么写,简述网页设计的流程并说明原因?  宝塔面板创建网站无法访问?如何快速排查修复?  如何在Golang中使用replace替换模块_指定本地或远程路径  建站之星CMS建站配置指南:模板选择与SEO优化技巧  免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程? 

您的项目需求

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