Thread.join解析

Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies.
@throws InterruptedException if interrupt() was called for the receiver while it was in the join() call
@see Object#notifyAll
@see java.lang.ThreadDeath

join方法的注释上写着:阻塞当前线程,直到收到结束执行或者死亡。当接收者的interrupt方法被调用,并且它被调用了join方法的时候,就会抛出InteruptedException。
举个例子:线程A的run方法中调用了线程B的join,此时线程A处于阻塞状态,直到线程B执行完毕或者死亡的时候,线程A才会继续执行。如果此时线程A处于阻塞状态,而线程B调用线程A的interrupt的时候,就会抛出InterruptedException。
下面举两个例子
示例1:
有两个线程A,B,在A的run方法中,会调用ThreadB.join,也就是说,在ThreadA启动之后,打印出来"ThreadA Started"之后,就会被线程B阻塞。然后线程B开始执行,打印出来"ThreadB started"之后,会休眠5S,之后再打印出"ThreadB Sleep End",之后线程B执行结束,于是线程A阻塞解除,打印出"ThreadA End"。

public class Test {
public static void main(String[] args) {
    ThreadB threadB = new ThreadB();
    ThreadA threadA = new ThreadA(threadB);
    threadA.start();
    threadB.start();
}

private static class ThreadA extends Thread {
    ThreadB joinThread;

    ThreadA(ThreadB threadB) {
        joinThread = threadB;
    }

    @Override
    public void run() {
        super.run();
        System.out.println("ThreadA Started");
        try {
            joinThread.join();
        } catch (InterruptedException e) {
        }
        System.out.println("ThreadA End");
    }
}

private static class ThreadB extends Thread {
    @Override
    public void run() {
        super.run();
        System.out.println("ThreadB started");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("ThreadB Sleep End");
    }
}
}

运行结果:

Thread.join解析_第1张图片
示例1结果

示例2
和上例不一样的地方是,在ThreadB的Run方法中,Sleep完后,调用ThreadA.interrupt,试一下官方文档上注释对不对。

public class Test {
public static void main(String[] args) {
    ThreadB threadB = new ThreadB();
    ThreadA threadA = new ThreadA(threadB);
    threadB.setThreadA(threadA);
    threadA.start();
    threadB.start();
}

private static class ThreadA extends Thread {
    ThreadB joinThread;

    ThreadA(ThreadB threadB) {
        joinThread = threadB;
    }

    @Override
    public void run() {
        super.run();
        System.out.println("ThreadA Started");
        try {
            joinThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("ThreadA End");
    }
}

private static class ThreadB extends Thread {
    ThreadA threadA;

    public void setThreadA(ThreadA threadA) {
        this.threadA = threadA;
    }

    @Override
    public void run() {
        super.run();
        System.out.println("ThreadB started");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("ThreadB Call ThreadA.interrupt");
        threadA.interrupt();
        System.out.println("ThreadB Sleep End");
    }
}
}

运行结果:
打印出InterruptedException

Thread.join解析_第2张图片
示例2运行结果

你可能感兴趣的:(Thread.join解析)