JDK1.8中CountDownLatch 源码(注释已经翻译成中文)
以下是countdownlatch源码。注释已经翻译成中文:package java.util.concurrent;import java.util.concurrent.locks.AbstractQueuedSynchronizer;/** * A synchronization aid that allows&
以下是countdownlatch源码。注释已经翻译成中文:
package java.util.concurrent;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
/**
* A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
*一种同步辅助程序,允许一个或多个线程等待在其它线程中执行的一组操作完成。
*
* <p>A {@code CountDownLatch} is initialized with a given <em>count</em>.
* The {@link #await await} methods block until the current count reaches
* zero due to invocations of the {@link #countDown} method, after which
* all waiting threads are released and any subsequent invocations of
* {@link #await await} return immediately. This is a one-shot phenomenon
* -- the count cannot be reset. If you need a version that resets the
* count, consider using a {@link CyclicBarrier}.
* 用给定的count之来初始化countDownLatch对象。调用 countDown方法直到当前count减少到0的时候,await方法就会被释放掉(不在阻塞)
* 这是一次性的倒计时器,无法重置(reset)计数器.如果你需要重置count的话,可以使用CyclicBarrier这个对象
*
* <p>A {@code CountDownLatch} is a versatile synchronization tool
* and can be used for a number of purposes. A
* {@code CountDownLatch} initialized with a count of one serves as a
* simple on/off latch, or gate: all threads invoking {@link #await await}
* wait at the gate until it is opened by a thread invoking {@link
* #countDown}. A {@code CountDownLatch} initialized to <em>N</em>
* can be used to make one thread wait until <em>N</em> threads have
* completed some action, or some action has been completed N times.
*CountDownLatch是一个通用的同步工具,适用于很多场景。
*CountDownLatch在初始化的时候,计数器设置为1的时候,可以作为一个简单的开关。
*所有调用await方法的线程都在等待着。直到调用countDown方法的时候,线程才不会继续等待。
*初始化一个计数器为N的CountDownLatch对象的含义:有可能是一个线程在等待其他N个线程完成某一个操作
*或者是某个操作完成N次
*
* <p>A useful property of a {@code CountDownLatch} is that it
* doesn't require that threads calling {@code countDown} wait for
* the count to reach zero before proceeding, it simply prevents any
* thread from proceeding past an {@link #await await} until all
* threads could pass.
*
* <p><b>Sample usage:</b> Here is a pair of classes in which a group
* of worker threads use two countdown latches:
* <ul>
* <li>The first is a start signal that prevents any worker from proceeding
* until the driver is ready for them to proceed;
* <li>The second is a completion signal that allows the driver to wait
* until all workers have completed.
* </ul>
*
* <pre> {@code
* class Driver { // ...
* void main() throws InterruptedException {
* CountDownLatch startSignal = new CountDownLatch(1);
* CountDownLatch doneSignal = new CountDownLatch(N);
*
* for (int i = 0; i < N; ++i) // create and start threads
* new Thread(new Worker(startSignal, doneSignal)).start();
*
* doSomethingElse(); // don't let run yet
* startSignal.countDown(); // let all threads proceed
* doSomethingElse();
* doneSignal.await(); // wait for all to finish
* }
* }
*
* class Worker implements Runnable {
* private final CountDownLatch startSignal;
* private final CountDownLatch doneSignal;
* Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
* this.startSignal = startSignal;
* this.doneSignal = doneSignal;
* }
* public void run() {
* try {
* startSignal.await();
* doWork();
* doneSignal.countDown();
* } catch (InterruptedException ex) {} // return;
* }
*
* v原创不易,完成人机校验,阅读全文