• 控制并发-CountDownLatch


    概述

    本文详细介绍CountDownLatch的两种使用场景,分别是倒数(等待所有的线程处理完成)和唤醒所有线程同时运行(适用于性能测试中触发所有并发同时运行)。

    下文从这个两个方面进行介绍

    CountDownLatch倒数实例

      实例代码利用CountDownLatch的downLatch方法让子线程在运行结束了减2;利用await方法,让主线程阻塞,见下实例代码。 

      

    package com.yang.concurrent;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class CountDownLatchDemo01 {
        public static void main(String[] args)   {
            final CountDownLatch countDownLatch=new CountDownLatch(5);
            ExecutorService executorService = Executors.newFixedThreadPool(5);
            for (int i = 0; i < 5; i++) {
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println(Thread.currentThread().getName() + "等待运行中");
                            Thread.sleep(1000);
                            System.out.println(Thread.currentThread().getName() + "运行结束");
                            countDownLatch.countDown();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
                executorService.submit(runnable);
            }
            System.out.println("主线程等待子线程运行结束");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("主线程运行结束");
        }
    }
    

        运行结果如下图所示:

        

    CountDownLatch同时运行实例

      本实例利用CountDownLatch的await方法让所以的子线程都等待,待相关资源准备好后,主线程通知线程运行。如下实例代码所示:

       

    package com.yang.concurrent;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 本实例演示子线程都等待
     */
    public class CountDownLatchDemo02 {
        public static void main(String[] args)   {
            final CountDownLatch countDownLatch=new CountDownLatch(1);
            ExecutorService executorService = Executors.newFixedThreadPool(5);
            for (int i = 0; i < 5; i++) {
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println(Thread.currentThread().getName() + "等待运行中");
                            countDownLatch.await();
                            System.out.println(Thread.currentThread().getName() + "运行结束");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
                executorService.submit(runnable);
            }
            System.out.println("主线程准备相关资源");
    
            try {
                Thread.sleep(1000);
                System.out.println("主线程资源准备就绪,子线程可以运行了");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

      运行结果如下图所示:

      

    CountDownLatch赛跑实例

      下实例代码我们模拟百米赛跑的故事,赛跑先要进行相关的准备工作,待准备工作完成后,明枪起跑,待所有人都到终点后,起跑结束,相关实例代码如下图所示。

      

    package com.yang.concurrent;
    
    import java.util.Random;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 本实例演示子线程都等待
     */
    public class CountDownLatchDemo03 {
        public static void main(String[] args)   {
            final CountDownLatch beginCountDownLatch=new CountDownLatch(1);
            final CountDownLatch endCountDownLatch=new CountDownLatch(5);
    
            ExecutorService executorService = Executors.newFixedThreadPool(5);
            System.out.println("百米赛跑准备中");
    
            for (int i = 0; i < 5; i++) {
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        try {
    
                            System.out.println(Thread.currentThread().getName() + "准备起跑!");
                            beginCountDownLatch.await();
                            Thread.sleep((long)new Random(1000).nextLong());
                            System.out.println(Thread.currentThread().getName() + "到达终点");
                            endCountDownLatch.countDown();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
                executorService.submit(runnable);
            }
    
            try {
                Thread.sleep(1000);
    
                System.out.println("名枪起跑");
                beginCountDownLatch.countDown();
                endCountDownLatch.await();
                System.out.println("所有人到达终点,赛跑结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

      运行结果如下:

      

  • 相关阅读:
    <LinkedList> 61
    <LinkedList> (hard + 高)25
    <DP> (高频)322
    <BackTracking> (dfs hard) 291
    <Tree> (高频)236
    <Math> 29 365
    <String> 161 358
    <Array> 309 (高)334
    <Array> 54 (高频+hard )45
    <Design> 359 346
  • 原文地址:https://www.cnblogs.com/cnxieyang/p/12767953.html
Copyright © 2020-2023  润新知