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

作者:52Hz 时间:2023-08-22 21:47:29 

先上代码

新建一个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方法的线程,其他线程等待该线程完全执行完后才会进来。


来源:http://www.cnblogs.com/Miracle-Maker/archive/2017/01/08/6261738.html

标签:java,多线程,join
0
投稿

猜你喜欢

  • 在C#中新手易犯的典型缺陷

    2023-08-26 01:24:20
  • winform获取当前名称实例汇总

    2023-03-22 17:03:18
  • jcrop 网页截图工具(插件)开发

    2022-10-21 22:30:19
  • Java运行时数据区域(内存划分)的深入讲解

    2023-03-16 02:36:50
  • eclipse中自动生成javadoc文档的方法

    2022-04-08 17:54:12
  • 详解Flutter中视频播放器插件的使用教程

    2023-06-15 23:47:31
  • Java jar打包工具使用方法步骤解析

    2023-07-01 12:26:47
  • C#根据年月日计算星期几的函数小例子

    2023-11-20 00:12:40
  • 解析C#中的私有构造函数和静态构造函数

    2021-11-27 07:13:15
  • Unity TextMeshPro实现富文本超链接默认字体追加字体

    2021-10-13 23:57:24
  • Android编程实现仿美团或淘宝的多级分类菜单效果示例【附demo源码下载】

    2022-09-09 01:38:50
  • 详解Springboot对多线程的支持

    2023-09-21 02:18:21
  • C#警惕匿名方法造成的变量共享实例分析

    2021-08-26 19:35:22
  • Java超详细讲解设计模式之一的工厂模式

    2023-03-26 21:08:04
  • Android日期选择器实现年月日三级联动

    2022-12-13 03:35:59
  • 基于JWT.NET的使用(详解)

    2021-07-09 22:15:25
  • Android自制精彩弹幕效果

    2023-12-27 04:57:43
  • 浅析JDK和Tomcat的安装与配置方法

    2022-02-02 07:49:43
  • JavaWeb工程中集成YMP框架快速上手

    2023-11-24 12:15:12
  • 解析后台进程对Android性能影响的详解

    2021-09-05 05:42:09
  • asp之家 软件编程 m.aspxhome.com