EDU.oswego.cs.dl.util.concurrent

Class CountDown

public class CountDown extends Object implements Sync

A CountDown can serve as a simple one-shot barrier. A Countdown is initialized with a given count value. Each release decrements the count. All acquires block until the count reaches zero. Upon reaching zero all current acquires are unblocked and all subsequent acquires pass without blocking. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a Barrier.

Sample usage. Here are a set of classes in which a group of worker threads use a countdown to notify a driver when all threads are complete.

 class Worker implements Runnable { 
   private final CountDown done;
   Worker(CountDown d) { done = d; }
   public void run() {
     doWork();
    done.release();
   }
 }
 
 class Driver { // ...
   void main() {
     CountDown done = new CountDown(N);
     for (int i = 0; i < N; ++i) 
       new Thread(new Worker(done)).start();
     doSomethingElse(); 
     done.acquire(); // wait for all to finish
   } 
 }
 

[ Introduction to this package. ]

Field Summary
protected intcount_
protected intinitialCount_
Constructor Summary
CountDown(int count)
Create a new CountDown with given count value *
Method Summary
voidacquire()
booleanattempt(long msecs)
intcurrentCount()
Return the current count value.
intinitialCount()
Return the initial count value *
voidrelease()
Decrement the count.

Field Detail

count_

protected int count_

initialCount_

protected final int initialCount_

Constructor Detail

CountDown

public CountDown(int count)
Create a new CountDown with given count value *

Method Detail

acquire

public void acquire()

attempt

public boolean attempt(long msecs)

currentCount

public int currentCount()
Return the current count value. This is just a snapshot value, that may change immediately after returning.

initialCount

public int initialCount()
Return the initial count value *

release

public void release()
Decrement the count. After the initialCount'th release, all current and future acquires will pass