多线程编程之join()方法

编程技术  /  houtizong 发布于 3年前   140

现实生活中,有些工作是需要团队中成员依次完成的,这就涉及到了一个顺序问题。现在有T1、T2、T3三个工人,如何保证T2在T1执行完后执行,T3在T2执行完后执行?

问题分析:首先问题中有三个实体,T1、T2、T3, 因为是多线程编程,所以都要设计成线程类。关键是怎么保证线程能依次执行完呢?

 

Java实现过程如下:

public class T1 implements Runnable{@Overridepublic void run() {    try {System.out.println("T1开始工作.....");Thread.sleep(RandomUtils.nextInt(300));System.out.println("T1结束工作>>>>>");} catch (InterruptedException e) {e.printStackTrace();}}}

 

package thread.join.demo1;import org.apache.commons.lang.math.RandomUtils;public class T2 implements Runnable{@Overridepublic void run() {try{System.out.println("T2开始工作.....");Thread.sleep(RandomUtils.nextInt(300));System.out.println("T2结束工作>>>>>");} catch (InterruptedException e) {e.printStackTrace();}}}

 

public class T3 implements Runnable{@Overridepublic void run() {try{System.out.println("T3开始工作.....");Thread.sleep(RandomUtils.nextInt(300));System.out.println("T3结束工作>>>>>");} catch (InterruptedException e) {e.printStackTrace();}}}

 

public class Main {public static void main(String[] args){Thread t1 = new Thread(new T1());Thread t2 = new Thread(new T2());Thread t3 = new Thread(new T3());t1.start();t2.start();t3.start();System.out.println("T1、T2、T3依次工作结束.");}}
 

运行结果:

T1开始工作.....

T2开始工作.....

T3开始工作.....

T1T2T3依次工作结束.

T3结束工作>>>>>

T2结束工作>>>>>

T1结束工作>>>>>

 

说明:从结果来看,T1T2T3并没有依次执行。查看JDK文档,java.lang.Thread 类有三个join()方法,其解释为:等待该线程终止。试用它来解决该问题……

 

Main.java修改如下:

public class Main {public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(new T1());Thread t2 = new Thread(new T2());Thread t3 = new Thread(new T3());t1.start();t1.join();t2.start();t2.join();t3.start();t3.join();System.out.println("T1、T2、T3依次工作结束.");}}

 

 

运行结果:

T1开始工作.....

T1结束工作>>>>>

T2开始工作.....

T2结束工作>>>>>

T3开始工作.....

T3结束工作>>>>>

T1T2T3依次工作结束.

 

 

哈,解决了!

 

 

查看jdk源码,其中join方法代码片断如下:

 

/**     * Waits at most <code>millis</code> milliseconds for this thread to      * die. A timeout of <code>0</code> means to wait forever.      *     * @param      millis   the time to wait in milliseconds.     * @exception  InterruptedException if any thread has interrupted     *             the current thread.  The <i>interrupted status</i> of the     *             current thread is cleared when this exception is thrown.     */    public final synchronized void join(long millis)     throws InterruptedException {long base = System.currentTimeMillis();long now = 0;if (millis < 0) {            throw new IllegalArgumentException("timeout value is negative");}if (millis == 0) {    while (isAlive()) {wait(0);    }} else {    while (isAlive()) {long delay = millis - now;if (delay <= 0) {    break;}wait(delay);now = System.currentTimeMillis() - base;    }}    } 

 

 

单纯从代码上看如果线程被生成了,但还未被起动,isAlive()将返回false,调用它的join()方法是没有作用的,将直接继续向下执行。Main.java类中,t1.join()是判断t1线程的状态,如果t1isActive()方法返回false,在 t1.join(),这一点就不用阻塞了,可以继续向下进行了。从源码里看,wait方法中有参数,也就是不用唤醒谁,只是不再执行wait,向下继续执行而已。join()方法中,对于isAlive()wait()方法的作用对象是个比较让人困惑的问题:

isAlive()方法的签名是:public final native boolean isAlive(),也就是说isAlive()是判断当前线程的状态,也就是t1的状态。

 

 

 

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客