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

 更新时间:2017年01月08日 14:32:48   作者:52Hz  
本文通过实例代码给大家实例介绍了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方法的线程,其他线程等待该线程完全执行完后才会进来。

相关文章

  • Spring boot2.0 实现日志集成的方法(3)

    Spring boot2.0 实现日志集成的方法(3)

    这篇文章主要介绍了Spring boot2.0 实现日志集成的方法,基于上一篇将日志信息根据类别输出到不同的文件中,这篇文章将通过日志来监控用户的操作行为、请求的耗时情况,针对耗时久的请求进行性能分析,提升系统性能,需要的小伙伴可以参考一下
    2022-04-04
  • java中File类的构造函数及其方法

    java中File类的构造函数及其方法

    这篇文章主要介绍了java中File类的构造函数及其方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-06-06
  • 如何解决Eclipse找不到或无法加载主类问题

    如何解决Eclipse找不到或无法加载主类问题

    这篇文章主要介绍了如何解决Eclipse找不到或无法加载主类问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • Java编程倒计时实现方法示例

    Java编程倒计时实现方法示例

    这篇文章主要介绍了Java编程倒计时实现的三个示例,三种实现方法,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09
  • SpringBoot集成Mybatis的实现步骤

    SpringBoot集成Mybatis的实现步骤

    这篇文章主要介绍了SpringBoot集成Mybatis的实现步骤,本文通过SpringBoot +MyBatis 实现对数据库学生表的查询操作,需要的朋友可以参考下
    2020-12-12
  • 源码分析Java中ThreadPoolExecutor的底层原理

    源码分析Java中ThreadPoolExecutor的底层原理

    这篇文章主要带大家从源码分析一下Java中ThreadPoolExecutor的底层原理,文中的示例代码讲解详细,具有一定的学习价值,需要的可以参考一下
    2023-05-05
  • Java中的内存泄露问题和解决办法

    Java中的内存泄露问题和解决办法

    大家好,本篇文章主要讲的是Java中的内存泄露问题和解决办法,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Java 将PDF转为HTML时保存到流的方法和步骤

    Java 将PDF转为HTML时保存到流的方法和步骤

    本文介绍如何通过Java后端程序代码将PDF文件转为HTML,并将转换后的HTML文件保存到流,下面是实现转换的方法和步骤,感兴趣的朋友一起看看吧
    2022-01-01
  • springboot项目配置logback日志系统的实现

    springboot项目配置logback日志系统的实现

    这篇文章主要介绍了springboot项目配置logback日志系统的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Java如何基于DOM解析xml文件

    Java如何基于DOM解析xml文件

    这篇文章主要介绍了Java如何基于DOM解析xml文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09

最新评论