我数 10 下大家一起上

开发 后端
CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间通信的作用(非互斥)。

 [[423912]]

在日常编码中,Java 并发编程可是少不了,试试下面这些并发编程工具类:

今天先带领大家重温学习 CountDownLatch 这个牛叉的工具类,肝起。

认识 CountDownLatch

CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间通信的作用(非互斥)。

CountDownLatch 能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行。使用一个计数器进行实现。计数器初始值为线程的数量。当每一个线程完成自己任务后,计数器的值就会减一。当计数器的值为0时,表示所有的线程都已经完成一些任务,然后在CountDownLatch上等待的线程就可以恢复执行接下来的任务。

CountDownLatch 的使用

CountDownLatch类使用起来非常简单。

Class 位于:java.util.concurrent.CountDownLatch

下面简单介绍它的构造方法和常用方法。

构造方法

CountDownLatch只提供了一个构造方法:

  1. // count 为初始计数值 
  2. public CountDownLatch(int count) { 
  3.   // …… 

常用方法

//常用方法1:调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行

  1. //常用方法1:调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行 
  2. public void await() throws InterruptedException { 
  3.   // …… 
  4. }    
  5.  
  6. // 常用方法2:和await()类似,只不过等待超时后count值还没变为0的话就会继续执行 
  7. public boolean await(long timeout, TimeUnit unit) throws InterruptedException {  
  8.   // …… 
  9.  
  10. // 常用方法3:将count值减1 
  11. public void countDown() { 
  12.   // …… 
  13. }   

CountDownLatch 的应用场景

我们考虑一个场景:用户购买一个商品下单成功后,我们会给用户发送各种消息提示用户『购买成功』,比如发送邮件、微信消息、短信等。所有的消息都发送成功后,我们在后台记录一条消息表示成功。

当然我们可以使用单线程去完成,逐个完成每个操作,如下图所示:

但是这样效率就会非常低。如何解决单线程效率低的问题?当然是通过多线程啦。

使用多线程也会遇到一个问题,子线程消息还没发送完,主线程可能就已经打出『所有的消息都已经发送完毕啦』,这在逻辑上肯定是不对的。我们期望所有子线程发完消息主线程才会打印消息,怎么实现呢?CountDownLatch就可以解决这一类问题。

我们使用代码实现上面的需求。

  1. import java.util.concurrent.*; 
  2.  
  3. public class OrderServiceDemo { 
  4.  
  5.     public static void main(String[] args) throws InterruptedException { 
  6.         System.out.println("main thread: Success to place an order"); 
  7.  
  8.         int count = 3; 
  9.         CountDownLatch countDownLatch = new CountDownLatch(count); 
  10.  
  11.         Executor executor = Executors.newFixedThreadPool(count); 
  12.         executor.execute(new MessageTask("email", countDownLatch)); 
  13.         executor.execute(new MessageTask("wechat", countDownLatch)); 
  14.         executor.execute(new MessageTask("sms", countDownLatch)); 
  15.  
  16.         // 主线程阻塞,等待所有子线程发完消息 
  17.         countDownLatch.await(); 
  18.         // 所有子线程已经发完消息,计数器为0,主线程恢复 
  19.         System.out.println("main thread: all message has been sent"); 
  20.     } 
  21.  
  22.     static class MessageTask implements Runnable { 
  23.         private String messageName; 
  24.         private CountDownLatch countDownLatch; 
  25.  
  26.         public MessageTask(String messageName, CountDownLatch countDownLatch) { 
  27.             this.messageName = messageName; 
  28.             this.countDownLatch = countDownLatch; 
  29.         } 
  30.  
  31.         @Override 
  32.         public void run() { 
  33.             try { 
  34.                 // 线程发送消息 
  35.                 System.out.println("Send " + messageName); 
  36.                 try { 
  37.                     TimeUnit.SECONDS.sleep(1); 
  38.                 } catch (InterruptedException e) { 
  39.                     e.printStackTrace(); 
  40.                 } 
  41.             } finally { 
  42.                 // 发完消息计数器减 1 
  43.                 countDownLatch.countDown(); 
  44.             } 
  45.         } 
  46.     } 

程序运行结果:

  1. main thread: Success to place an order 
  2. Send email 
  3. Send wechat 
  4. Send sms 
  5. main thread: all message has been sent 

从运行结果可以看到主线程是在所有的子线程发送完消息后才打印,这符合我们的预期。

CountDownLatch 的限制

CountDownLatch是一次性的,计算器的值只能在构造方法中初始化一次,之后没有任何机制再次对其设置值,当CountDownLatch使用完毕后,它不能再次被使用。

 

责任编辑:武晓燕 来源: 爱笑的架构师
相关推荐

2022-11-29 16:35:02

Tetris鸿蒙

2022-12-02 14:20:09

Tetris鸿蒙

2021-04-12 18:03:39

Nginx架构负载

2021-07-02 20:46:06

Go接口动态

2022-12-22 19:04:14

RabbitMQ消息中间件

2011-06-13 13:23:44

投影仪推荐

2021-02-03 09:59:02

鸿蒙HarmonyOS应用开发

2009-10-29 16:32:34

Oracle表空间

2021-09-11 19:02:34

Hook使用版本

2012-09-10 13:42:55

PHP项目管理

2009-08-10 18:16:20

C#面试题

2009-09-14 18:24:56

CCNA认证考试题库

2021-12-14 09:34:31

丑数顺序指针

2011-08-05 14:14:12

2021-03-10 12:43:06

LDR指令函数

2021-07-14 08:00:12

Numa架构Linux

2014-12-15 16:05:54

CocoaChina征文大赛

2018-07-30 16:04:09

ReactJS代码HTML

2023-03-30 09:32:27

2014-10-21 15:07:04

点赞
收藏

51CTO技术栈公众号