• Java CountDownLatch 使用与案例测试


    一、CountDownLatch介绍;

    * CountDownLatch是一种java.util.concurrent包下一个同步工具类;

    * CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行。
    * public void countDown() 锁存器的计数减1;
    * public boolean await(long timeout,TimeUnit unit) 使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断或超出了指定的等待时间。

    二、应用场景介绍;

    应用场景1:某一线程在开始运行前等待n个线程执行完毕;比如启动一个服务时,主线程需要等待多个组件加载完毕,之后再继续执行。


    应用场景2:实现多个线程开始执行任务的最大并行性。注意是并行性,不是并发,强调的是多个线程在某一时刻同时开始执行。类似于赛跑,将多个线程放到起点,等待发令枪响,然后同时开跑。做法是初始化一个共享的CountDownLatch(1),将其计算器初始化为1,多个线程在开始执行任务前首先countdownlatch.await(),当主线程调用countDown()时,
    计数器变为0,多个线程同时被唤醒。

    三、样例代码;

     1 public class CountDownLatchTest {
     2 
     3     public static void main(String[] args) throws Exception{
     4         // TODO Auto-generated method stub
     5         //场景1测试代码块开始
     6         CountDownLatch latch = new CountDownLatch(2);
     7         
     8         for(int i=0;i<2;i++){
     9             Thread thread = new Thread(new ComponentService(latch));
    10             thread.start();
    11         }
    12         
    13         System.out.println("main thread begin...");
    14         latch.await(); //使当前线程在锁存器倒计数至零之前一直等待
    15         System.out.println("main thread end....");
    16         //场景1测试代码块结束
    17         System.out.println("===============长长的分割线================");
    18         //场景2测试代码块开始
    19         CountDownLatch begin = new CountDownLatch(1);
    20         for(int i=0;i<2;i++){
    21             Thread thread = new Thread(new Player(begin));
    22             thread.start();
    23         }
    24         System.out.println("main thread begin...");
    25         begin.countDown();
    26         System.out.println("main thread end....");
    27         //场景2测试代码块结束
    28     }
    29 }
     1 class ComponentService implements Runnable{
     2     private CountDownLatch latch;
     3     public ComponentService(CountDownLatch latch) {
     4         super();
     5         this.latch = latch;
     6     }
     7 
     8     @Override
     9     public void run() {
    10         // TODO Auto-generated method stub
    11             System.out.println(Thread.currentThread().getName()+" 服务启动成功 !");
    12             latch.countDown();
    13     }
    14 }
     1 class Player implements Runnable{
     2     private CountDownLatch begin;
     3     public Player(CountDownLatch begin) {
     4         super();
     5         this.begin = begin;
     6     }
     7 
     8     @Override
     9     public void run(){
    10         // TODO Auto-generated method stub
    11         try {
    12             begin.await();
    13             System.out.println(Thread.currentThread().getName()+" 起跑成功!");
    14         } catch (InterruptedException e) {
    15             // TODO Auto-generated catch block
    16             e.printStackTrace();
    17         }
    18     }
    19 }

    执行结果:

    四、部分内容转载说明;

    部分来自网络整理:

    https://www.cnblogs.com/Lee_xy_z/p/10470181.html

  • 相关阅读:
    使用golang访问kubebernetes
    使用 Rancher 管理现有 Kubernetes 集群
    Running powershell scripts during nuget package installation and removal
    How to Create, Use, and Debug .NET application Crash Dumps in 2019
    寻找写代码感觉(一)之使用 Spring Boot 快速搭建项目
    Selenium+Java之解决org.openqa.selenium.InvalidArgumentException: invalid argument报错问题
    Selenium环境搭建
    关于Xpath定位方法知道这些基本够用
    Web自动化之浏览器启动
    【翻译】编写代码注释的最佳实践
  • 原文地址:https://www.cnblogs.com/crazytrip/p/11697595.html
Copyright © 2020-2023  润新知