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

2019-08-08  本文已影响0人  icebreakeros

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");
    }
}
上一篇下一篇

猜你喜欢

热点阅读