聊聊高并发(二十六)解析java.util.concurrent各个组件(八) 理解CountDownLatch闭锁

CountDownLatch闭锁也是基于AQS实现的一种同步器,它表示了“所有线程都等待,直到锁打开才继续执行”的含义。它和Semaphore的语意不同, Semaphore的获取和释放操作都会修改状态,都可能让自己或者其他线程立刻拿到锁。而闭锁的获取操作只判断状态是否为0,不修改状态本身,闭锁的释放操作会修改状态,每次递减1,直到状态为0。

所以正常情况下,闭锁的获取操作只是等待,不会立刻让自己获得锁,直到释放操作把状态变为0。

闭锁可以用来实现很多场景,比如:

1. 某个服务依赖于其他服务的启动才能启动,就可以让这个服务在其他服务状态的闭锁上等待

2. 某个游戏,必须等所有就绪者都到达才能开始游戏

3. 启动一组相关的线程

4. 等待一组相关线程结束

 

来看看CountDownLatch的代码。它也提供了一个内部类Sync来继承AQS

1. CountDownLatch可以让多个线程同时进入临界区,所以也是共享模式的AQS

2. 获取操作只是判断状态是否为0,即是否可以结束等待,进入临界区

3. 释放操作是对状态递减1,所以叫CountDown,类似报数的意思

 

 
  1. private static final class Sync extends AbstractQueuedSynchronizer {

  2. private static final long serialVersionUID = 4982264981922014374L;

  3.  
  4. Sync(int count) {

  5. setState(count);

  6. }

  7.  
  8. int getCount() {

  9. return getState();

  10. }

  11.  
  12. protected int tryAcquireShared(int acquires) {

  13. return (getState() == 0) ? 1 : -1;

  14. }

  15.  
  16. protected boolean tryReleaseShared(int releases) {

  17. // Decrement count; signal when transition to zero

  18. for (;;) {

  19. int c = getState();

  20. if (c == 0)

  21. return false;

  22. int nextc = c-1;

  23. if (compareAndSetState(c, nextc))

  24. return nextc == 0;

  25. }

  26. }

  27. }

 

 

CountDownLatch维护了一个状态表示Count的总数,释放一次对这个总数减1直到为0,它的tryXXX方法传递的参数没有实际意义,只是为了适应接口。

如果获取失败,就进入AQS等待,直到等待结束后,以共享的方式在AQS队列中释放线程。

CountDownLatch常用的方法就两个: await()和countDown()

 

 
  1. public CountDownLatch(int count) {

  2. if (count < 0) throw new IllegalArgumentException("count < 0");

  3. this.sync = new Sync(count);

  4. }

  5.  
  6. // 等待就相当于获取操作

  7. public void await() throws InterruptedException {

  8.         sync.acquireSharedInterruptibly(1);

  9.     }

  10. // 限时等待

  11. public boolean await(long timeout, TimeUnit unit)

  12.         throws InterruptedException {

  13.         return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));

  14.     }

  15.  
  16. public void countDown() {

  17.         sync.releaseShared(1);

  18.     }

  19.  


设计1个测试用例来测试CountDownLatch闭锁的功能

 

1.  创建1个二元闭锁startLatch,只有1和0两种状态,也就是说只执行一次countDown()就可以打开闭锁。这个startLatch用来阻塞线程,直到主线程说可以开始了

2.  创建1个状态为n的endLatch,线程执行完就调用一次countDown,主线程在endLatch阻塞,直到n个线程都执行了countDown()报数,主线程才打印结束

 

 

 
  1. package com.zc.lock;

  2.  
  3. import java.util.concurrent.CountDownLatch;

  4.  
  5. public class CountDownLatchUsecase {

  6. private int nThreads;

  7.  
  8. private CountDownLatch startLatch;

  9.  
  10. private CountDownLatch endLatch;

  11.  
  12. public CountDownLatchUsecase(int n){

  13. this.nThreads = n;

  14. startLatch = new CountDownLatch(1);

  15. endLatch = new CountDownLatch(nThreads);

  16. }

  17.  
  18. public void race() throws InterruptedException{

  19. System.out.println("Thread " + Thread.currentThread().getName() + " is waiting the resource");

  20. startLatch.await();

  21. System.out.println("Thread " + Thread.currentThread().getName() + " got the resource");

  22. endLatch.countDown();

  23. }

  24.  
  25. public void start(){

  26. startLatch.countDown();

  27. }

  28.  
  29. public void end() throws InterruptedException{

  30. endLatch.await();

  31. }

  32.  
  33. public static void main(String[] args) throws Exception{

  34. final CountDownLatchUsecase usecase = new CountDownLatchUsecase(10);

  35. for(int i = 0; i < 10; i++){

  36. Thread t = new Thread(new Runnable(){

  37.  
  38. @Override

  39. public void run() {

  40. try {

  41. usecase.race();

  42. } catch (InterruptedException e) {

  43. e.printStackTrace();

  44. }

  45. }

  46.  
  47. }, String.valueOf(i));

  48. t.start();

  49. }

  50.  
  51. Thread.sleep(3000);

  52.  
  53. System.out.println("Now start!!!");

  54. usecase.start();

  55. usecase.end();

  56. System.out.println("All Thread finished");

  57.  
  58. }

  59. }


测试结果: 所有线程都等待,直到主线程说开始。所有线程都执行了countDown()之后,主线程才说结束

 

 

 
  1. Thread 0 is waiting the resource

  2. Thread 2 is waiting the resource

  3. Thread 3 is waiting the resource

  4. Thread 1 is waiting the resource

  5. Thread 4 is waiting the resource

  6. Thread 5 is waiting the resource

  7. Thread 6 is waiting the resource

  8. Thread 7 is waiting the resource

  9. Thread 8 is waiting the resource

  10. Thread 9 is waiting the resource

  11. Now start!!!

  12. Thread 0 got the resource

  13. Thread 2 got the resource

  14. Thread 3 got the resource

  15. Thread 8 got the resource

  16. Thread 7 got the resource

  17. Thread 6 got the resource

  18. Thread 5 got the resource

  19. Thread 1 got the resource

  20. Thread 4 got the resource

  21. Thread 9 got the resource

  22. All Thread finished

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值