Java线程中的join方法

join是Thread类的一个方法,启动线程后直接调用,例如:

Thread t = new AThread();
t.start(); 
t.join();

为什么要用join()方法,它会为我们解决什么问题呢?
在很多情况下,有运行时间很长的逻辑与计算任务的时候,我们会启动一个或多个子线程去进行处理,这时我们有以下几种情况会出现:

  1. 后续的处理中不需要等待子线程处理的结束,也不关心子线程是否执行完成。这时我们可以直接运行我们主线程直到结束。
  2. 我们需要等待子线程全部执行结束,我们才可以往下执行主线程。
  3. 在主线程的后续逻辑处理中需要子线程中的处理结果。

我们先看第一种情况:


public class AThread extends Thread{

    @Override
    public void run() {
        System.out.println("This aThread is running ..." + this.getName());
        try {
            sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("This aThread is end ..." + this.getName());
    }
}

public class BThread extends Thread{

    @Override
    public void run() {
        System.out.println("This bThread is running ..." + this.getName());
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("This bThread is end ..." + this.getName());
    }

}
public class Demo {
    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        Thread aThread = new AThread();
        Thread bThread = new BThread();
        aThread.start();
        bThread.start();
        Long endTime = System.currentTimeMillis();
        System.out.println("Main mothed is end,use time :" 
        + (endTime - startTime));
    }
}

运行结果:主线程直接运行结果,这时在主线程的运行过程中我们不关心子线程是否运行成功,也不关心子线程的执行结果。
这里写图片描述

第二种情况:

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        Thread aThread = new AThread();
        Thread bThread = new BThread();
        aThread.start();
        bThread.start();
        //我们需要等待子线程结束后,再进行主线程
        aThread.join();
        bThread.join();
        Long endTime = System.currentTimeMillis();
        System.out.println("Main mothed is end,use time :" 
        + (endTime - startTime));
    }
}

运行结果:这时,我们不关心子线程的运行结果,但是我们需要等待子线程运行结束后,主线程才可以运行接下来的逻辑。
这里写图片描述

第三种情况:

public class AThread extends Thread{

    private String resultA;

    @Override
    public void run() {
        System.out.println("This aThread is running ..." + this.getName());
        try {
            sleep(1000);
            this.resultA = "AThread rsult is a";
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("This aThread is end ..." + this.getName());
    }

    public String getResultA() {
        return resultA;
    }

    public void setResultA(String resultA) {
        this.resultA = resultA;
    }

}
public class BThread extends Thread{

    private String resultB;

    @Override
    public void run() {
        System.out.println("This bThread is running ..." + this.getName());
        try {
            sleep(2000);
            this.resultB = "BThread rsult is b";
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("This bThread is end ..." + this.getName());
    }

    public String getResultB() {
        return resultB;
    }

    public void setResultB(String resultB) {
        this.resultB = resultB;
    }

}
public class Demo {
    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        AThread aThread = new AThread();
        BThread bThread = new BThread();
        aThread.start();
        bThread.start();
        //我们需要等待子线程结束后,再进行主线程
        aThread.join();
        System.out.println(aThread.getResultA());
        bThread.join();
        System.out.println(bThread.getResultB());
        Long endTime = System.currentTimeMillis();
        System.out.println("Main mothed is end,use time :" 
        + (endTime - startTime));
    }
}

运行结果:这时我们需要等待子线程运程结束,并将运行结果返回,如果在主线程需要使用,我们就可以直接使用了。
这里写图片描述


PS:技术有限,如有不对之处,欢迎大家指出。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值