首页 > 代码库 > 聊聊高并发(二十六)解析java.util.concurrent各个组件(八) 理解CountDownLatch闭锁
聊聊高并发(二十六)解析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,类似报数的意思
private static final class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 4982264981922014374L; Sync(int count) { setState(count); } int getCount() { return getState(); } protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1; } protected boolean tryReleaseShared(int releases) { // Decrement count; signal when transition to zero for (;;) { int c = getState(); if (c == 0) return false; int nextc = c-1; if (compareAndSetState(c, nextc)) return nextc == 0; } } }
CountDownLatch维护了一个状态表示Count的总数,释放一次对这个总数减1直到为0,它的tryXXX方法传递的参数没有实际意义,只是为了适应接口。
如果获取失败,就进入AQS等待,直到等待结束后,以共享的方式在AQS队列中释放线程。
CountDownLatch常用的方法就两个: await()和countDown()
public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } // 等待就相当于获取操作 public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } // 限时等待 public boolean await(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } public void countDown() { sync.releaseShared(1); }
设计1个测试用例来测试CountDownLatch闭锁的功能
1. 创建1个二元闭锁startLatch,只有1和0两种状态,也就是说只执行一次countDown()就可以打开闭锁。这个startLatch用来阻塞线程,直到主线程说可以开始了
2. 创建1个状态为n的endLatch,线程执行完就调用一次countDown,主线程在endLatch阻塞,直到n个线程都执行了countDown()报数,主线程才打印结束
package com.zc.lock; import java.util.concurrent.CountDownLatch; public class CountDownLatchUsecase { private int nThreads; private CountDownLatch startLatch; private CountDownLatch endLatch; public CountDownLatchUsecase(int n){ this.nThreads = n; startLatch = new CountDownLatch(1); endLatch = new CountDownLatch(nThreads); } public void race() throws InterruptedException{ System.out.println("Thread " + Thread.currentThread().getName() + " is waiting the resource"); startLatch.await(); System.out.println("Thread " + Thread.currentThread().getName() + " got the resource"); endLatch.countDown(); } public void start(){ startLatch.countDown(); } public void end() throws InterruptedException{ endLatch.await(); } public static void main(String[] args) throws Exception{ final CountDownLatchUsecase usecase = new CountDownLatchUsecase(10); for(int i = 0; i < 10; i++){ Thread t = new Thread(new Runnable(){ @Override public void run() { try { usecase.race(); } catch (InterruptedException e) { e.printStackTrace(); } } }, String.valueOf(i)); t.start(); } Thread.sleep(3000); System.out.println("Now start!!!"); usecase.start(); usecase.end(); System.out.println("All Thread finished"); } }
测试结果: 所有线程都等待,直到主线程说开始。所有线程都执行了countDown()之后,主线程才说结束
Thread 0 is waiting the resource Thread 2 is waiting the resource Thread 3 is waiting the resource Thread 1 is waiting the resource Thread 4 is waiting the resource Thread 5 is waiting the resource Thread 6 is waiting the resource Thread 7 is waiting the resource Thread 8 is waiting the resource Thread 9 is waiting the resource Now start!!! Thread 0 got the resource Thread 2 got the resource Thread 3 got the resource Thread 8 got the resource Thread 7 got the resource Thread 6 got the resource Thread 5 got the resource Thread 1 got the resource Thread 4 got the resource Thread 9 got the resource All Thread finished
聊聊高并发(二十六)解析java.util.concurrent各个组件(八) 理解CountDownLatch闭锁