美文网首页
Java并发 | join实现顺序执行线程

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

作者: icebreakeros | 来源:发表于2019-08-08 16:11 被阅读0次

    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实现顺序执行线程

          本文链接:https://www.haomeiwen.com/subject/auwxjctx.html