Java并发 | join实现顺序执行线程

join实现顺序执行线程

join()等待线程摧毁

join和synchronized区别

join在内部使用wait()方法进行等待
synchronized关键字使用的是“对象监视器”原理做为同步

实例

class A implements Runnable {
    @Override
    public void run() {
        System.out.println("A");
    }
}

class B implements Runnable {
    @Override
    public void run() {
        System.out.println("B");
    }
}

class C implements Runnable {
    @Override
    public void run() {
        System.out.println("C");
    }
}

public class Run {

    public static void main(String[] args) throws InterruptedException {
        Thread aThread = new Thread(new A());
        Thread bThread = new Thread(new B());
        Thread cThread = new Thread(new C());
        aThread.start();
        aThread.join();

        bThread.start();
        bThread.join();

        cThread.start();
        cThread.join();
        System.out.println("main");
    }
}

你可能感兴趣的:(Java并发 | join实现顺序执行线程)