CountDownLatch

參考官方文檔:https://developer.android.google.cn/reference/java/util/concurrent/CountDownLatch

CountDownLatch介紹

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

一種同步幫助工具類,它允許一個或多個線程等待,直到在其他線程中執行的一組操作完成爲止。

A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of 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 CyclicBarrier.

CountDownLatch用給定的計數初始化。 await方法將阻塞線程直到調用countDown方法使當前計數達到零爲止。count爲0後所有釋放的線程將被釋放,並且所有隨後的await()調用將立即返回。 這是一種一次性現象-無法重置計數。 如果需要用於重置計數的版本,請考慮使用CyclicBarrier。

A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown(). A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.

CountDownLatch是一種多功能的同步工具,可以用於多種目的。 以1計數初始化的CountDownLatch用作簡單的開/關閂鎖或門:所有調用await的線程在門處等待,直到被調用countDown()的線程打開爲止。 初始化爲N的CountDownLatch可以用於使一個線程等待,直到N個線程完成某個動作或某個動作已經完成N次。

A useful property of a CountDownLatch is that it doesn't require that threads calling countDown wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads could pass.

CountDownLatch的比較有用的屬性是它不需要調用countDown的線程在繼續進行操作之前就等待計數達到零,它只是阻塞調用了await()的線程,直到所有線程都可以通過。

Sample usage: Here is a pair of classes in which a group of worker threads use two countdown latches

用法示例

這是一對類,其中一組工作線程使用兩個CountDownLatch

    The first is a start signal that prevents any worker from proceeding until the driver is ready for them to proceed;
    The second is a completion signal that allows the driver to wait until all workers have completed.

第一個是啓動信號,可防止任何工人繼續前進,直到駕駛員爲他們做好準備爲止。
第二個是完成信號,允許駕駛員等到所有工人都完成後。

 class Driver { // ...
   void main() throws InterruptedException {

    //啓動信號
     CountDownLatch startSignal = new CountDownLatch(1);

   //完成信號 (需要所有工人都完成)
     CountDownLatch doneSignal = new CountDownLatch(N);

  //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;
   }

   void doWork() { ... }
 }

用法示例2

另一個典型用法是將問題分爲N個部分,用Runnable描述每個部分,該Runnable執行該部分並在閂鎖上遞減計數,然後將所有Runnable排隊給執行程序。 當所有子部分都完成時,協調線程將能夠通過等待。 (當線程必須以此方式反覆遞減計數時,請使用CyclicBarrier。)

 class Driver2 { // ...
   void main() throws InterruptedException {
     CountDownLatch doneSignal = new CountDownLatch(N);
     Executor e = ...

     for (int i = 0; i < N; ++i) // create and start threads
       e.execute(new WorkerRunnable(doneSignal, i));

      //駕駛員等待所有工人完成工作

     doneSignal.await();           // wait for all to finish
   }
 }

 class WorkerRunnable implements Runnable {
   private final CountDownLatch doneSignal;
   private final int i;
   WorkerRunnable(CountDownLatch doneSignal, int i) {
     this.doneSignal = doneSignal;
     this.i = i;
   }
   public void run() {
     try {
       doWork(i);
       doneSignal.countDown();
     } catch (InterruptedException ex) {} // return;
   }

   void doWork() { ... }
 }

.

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章