简介
在前文AbstractQueuedSynchronizer使用和源码分析中讲解了AQS的原理和实现,理解了AQS的基础上,去看CountDownLatch等Synchronizer就非常简单了。
CountDownLatch从字面上理解,CountDown是倒计时的意思,Latch是锁、门栓,那么CountDownLatch就是倒计时的门栓了:),在开发时,我们可能会有类似下面的这种场景,有三个并发任务同时去执行,然后在下一步需要等待它们都执行完成才能继续执行,实现这个现在想想大概有几种方式,Thread.join等待线程,使用wait()notify(),Executor.invokeAll、然后对各个Future调用get等,这些都可以实现,但是有没有一种专门的工具类完成这个工作呢,有的,在JDK1.5中提供了java.util.concurrent.CountDownLatch类,用于一个或多个类等待一直到其他线程完成一系列操作。
CountDownLatch创建时设置一个count值,表示倒计时的次数,然后等待状态的线程调用CountDownLatch的await()方法(注意不要和Object.wait()混淆)进行等待,倒计时的方法是countDown(),
每次countDown都会减少count的值,直到count为0,则所有的await()的线程都会从等待中返回。
使用示例
下面使用CountDownLatch实现一个并发任务执行时间统计,为了防止一部分任务先执行,要让它们都等待一个开始指令,类似起跑抢指令,然后在最后等所有线程执行结束输出整体的执行时间。
public class CountDownTest {
private static class ProcessJob implements Runnable {
@Override
public void run() {
System.out.println("Some Job");
}
}
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
int jobSize = 20;
CountDownLatch startLatch = new CountDownLatch(1);
CountDownLatch stopLatch = new CountDownLatch(jobSize);
for (int i = 0; i < jobSize; i++) {
executorService.submit(() -> {
try {
// 线程等待开始的Latch
startLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
new ProcessJob().run();
} finally {
// 调用结束的Latch的countDown
stopLatch.countDown();
}
});
}
long start = System.currentTimeMillis();
startLatch.countDown();
// 等待所有线程执行完任务
stopLatch.await();
long end = System.currentTimeMillis();
System.out.println("Total cost time: " + (end - start));
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
}
}
CountDownLatch源码分析
像AbstractQueuedSynchronizer使用和源码分析中提到,CountDownLatch是基于AQS实现的,CountDownLatch由于可以多个线程同时通过,所以是共享模式,那么就可以考虑用state表示count,count方法则调用compareAndSet通过CAS进行减1操作,如果到0则调用releaseShared唤醒等待线程, 通过覆盖tryAcquireShared方法,只需判断当前state是否等于0,如果不为0则等待,然后await()方法就可以调用AQS的acquireShared方法了,并且可以根据是否中断、有超时调用不同的方法。CountDownLatch同样按照标准的AQS使用方式,创建一个静态内部类实现AQS相关的acquire和release方法
public class CountDownLatch { 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) { // 只有当当前state为0即count为0的时候能够返回 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)) // 成功CAS将count变为0的线程负责唤醒等待线程 return nextc == 0; } } } private final Sync sync; /** * Constructs a {@code CountDownLatch} initialized with the given count. * * @param count the number of times {@link #countDown} must be invoked * before threads can pass through {@link #await} * @throws IllegalArgumentException if {@code count} is negative */ public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } /** * await()调用acquireShared的不可中断的方法acquireSharedInterruptibly,acquireSharedInterruptibly又会调用到上面Sync实现的tryAcquire方法判断是否能够返回还是继续等待。 * Causes the current thread to wait until the latch has counted down to * zero, unless the thread is {@linkplain Thread#interrupt interrupted}. * *If the current count is zero then this method returns immediately. * *
If the current count is greater than zero then the current * thread becomes disabled for thread scheduling purposes and lies * dormant until one of two things happen: *
*
* *- The count reaches zero due to invocations of the * {@link #countDown} method; or *
- Some other thread {@linkplain Thread#interrupt interrupts} * the current thread. *
If the current thread: *
*
* then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * * @throws InterruptedException if the current thread is interrupted * while waiting */ public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } /** * 这是acquire的具有超时、中断的版本 * Causes the current thread to wait until the latch has counted down to * zero, unless the thread is {@linkplain Thread#interrupt interrupted}, * or the specified waiting time elapses. * *- has its interrupted status set on entry to this method; or *
- is {@linkplain Thread#interrupt interrupted} while waiting, *
If the current count is zero then this method returns immediately * with the value {@code true}. * *
If the current count is greater than zero then the current * thread becomes disabled for thread scheduling purposes and lies * dormant until one of three things happen: *
*
* *- The count reaches zero due to invocations of the * {@link #countDown} method; or *
- Some other thread {@linkplain Thread#interrupt interrupts} * the current thread; or *
- The specified waiting time elapses. *
If the count reaches zero then the method returns with the * value {@code true}. * *
If the current thread: *
*
* then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * *- has its interrupted status set on entry to this method; or *
- is {@linkplain Thread#interrupt interrupted} while waiting, *
If the specified waiting time elapses then the value {@code false} * is returned. If the time is less than or equal to zero, the method * will not wait at all. * * @param timeout the maximum time to wait * @param unit the time unit of the {@code timeout} argument * @return {@code true} if the count reached zero and {@code false} * if the waiting time elapsed before the count reached zero * @throws InterruptedException if the current thread is interrupted * while waiting */ public boolean await(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } /** * countDown调用sync的releaseShared方法,releaseShared方法又会调用上面Sync实现的tryReleaseShared方法根据返回结果判定是否唤醒线程 * Decrements the count of the latch, releasing all waiting threads if * the count reaches zero. * *
If the current count is greater than zero then it is decremented. * If the new count is zero then all waiting threads are re-enabled for * thread scheduling purposes. * *
If the current count equals zero then nothing happens. */ public void countDown() { sync.releaseShared(1); } ... }
坚持原创技术分享,您的支持将鼓励我继续创作分享!
赏
微信打赏
支付宝打赏