zoukankan      html  css  js  c++  java
  • Java主线程在子线程执行完毕后再执行

    一、join()

    Thread中的join()方法就是同步,它使得线程之间由并行执行变为串行执行。

    public class MyJoinTest {
        public static void main(String[] args) {
            Vector<Thread> threadVector = new Vector<Thread>();
    
            for (int i = 0;i<5;i++){
                Thread childThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("子线程执行...");
                    }
                });
                threadVector.add(childThread);
                childThread.start();
    
            }
            for (Thread t : threadVector){
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            System.out.println("主线程执行......");
        }
    }

    我们使用循环创建了5个子线程,把它们放到Vector对象中,并启动这个线程。遍历Vector,获取每一个子线程。在main线程中调用子线程的join方法,那么main线程放弃cpu的使用权,直到所有的子线程执行完毕,才会执行main线程。执行结果如下:

    子线程执行...
    子线程执行...
    子线程执行...
    子线程执行...
    子线程执行...
    主线程执行......

    二、CountDownLatch

    public class CountDownLatchTest {
    
        public static void main(String[] args) {
    
            final CountDownLatch latch = new CountDownLatch(5);
    
            for (int i = 0; i < 5; i++) {
    
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("子线程执行...");
                        latch.countDown();//让latch中的值减1
                    }
                }).start();
    
            }
            try {
                latch.await();//阻塞当前线程,直到latch的值为0
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            System.out.println("主线程执行......");
    
        }
    
    }

    三、CyclicBarrier

    public class CyclicBarrierTest {
    
        public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
    
            final CyclicBarrier barrier = new CyclicBarrier(5);
    
            for (int i = 0; i < 4; i++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println("子线程执行...");
                        try {
                            barrier.await();//到达屏障
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
            barrier.await();
            System.out.println("主线程执行......");
        }
    }
    CountDownLatchCyclicBarrier有什么区别呢?
    他们的区别:
    CountDownLatch只能使用一次,而CyclicBarrier方法可以使用reset()方法重置,所以CyclicBarrier方法可以处理更为复杂的业务场景。
  • 相关阅读:
    iOS,Android,WP, .NET通用AES加密算法
    iOS开发笔记-图标和图片大小官方最新标准
    因为对 Docker 不熟悉建了 N 多个 Nginx
    Docker 学习笔记 2019-05-27
    Linux Mint 19.1 安装 Docker 过程笔记
    W600 一块新的 KiCad PCB
    KiCad Mark 点名称
    一次乙型流感记录(2019-05-24)
    为什么不喜欢在 QQ 群里回答问题?
    Git 的两种忽略文件方式 gitignore 和 exclude
  • 原文地址:https://www.cnblogs.com/beanbag/p/10406229.html
Copyright © 2011-2022 走看看