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:技术有限,如有不对之处,欢迎大家指出。

猜你喜欢

转载自blog.csdn.net/bj_ameng/article/details/53349223