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方法可以处理更为复杂的业务场景。
  • 相关阅读:
    Study Plan The Twelfth Day
    Study Plan The Fifteenth Day
    Study Plan The Seventeenth Day
    Study Plan The Tenth Day
    Study Plan The Eighth Day
    Study Plan The Eleventh Day
    Study Plan The Sixteenth Day
    Study Plan The Thirteenth Day
    Study Plan The Fourteenth Day
    Study Plan The Ninth Day
  • 原文地址:https://www.cnblogs.com/beanbag/p/10406229.html
Copyright © 2011-2022 走看看