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方法可以处理更为复杂的业务场景。
  • 相关阅读:
    [bzoj1500][luogu2042][cogs339][codevs1758]维修数列(维护数列)
    无旋treap的简单思想以及模板
    [hdu2036]改革春风吹满地
    (treap)[bzoj3224][洛谷3369][cogs1829]Tyvj 1728 普通平衡树
    [bzoj3875][Ahoi2014]骑士游戏
    [bzoj1433][ZJOI2009]假期的宿舍
    <struct、union、enum>差异
    LeetCode(50) Pow(x,n)
    LeetCode(49)Group Anagrams
    LeetCode(48)Rotate Image
  • 原文地址:https://www.cnblogs.com/beanbag/p/10406229.html
Copyright © 2011-2022 走看看