EDU.oswego.cs.dl.util.concurrent

Class CountDown

Implemented Interfaces:
Sync

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 <32N; ++i) 
       new Thread(new Worker(done)).start();
     doSomethingElse(); 
     done.acquire(); // wait for all to finish
   } 
 }
 

[ Introduction to this package. ]

Field Summary

protected int
count_
protected int
initialCount_

Fields inherited from interface EDU.oswego.cs.dl.util.concurrent.Sync

ONE_CENTURY, ONE_DAY, ONE_HOUR, ONE_MINUTE, ONE_SECOND, ONE_WEEK, ONE_YEAR

Constructor Summary

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

Method Summary

void
acquire()
boolean
attempt(long msecs)
int
currentCount()
Return the current count value.
int
initialCount()
Return the initial count value *
void
release()
Decrement the count.

Field Details

count_

protected int count_


initialCount_

protected final int initialCount_

Constructor Details

CountDown

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

Method Details

acquire

public void acquire()
            throws InterruptedException
Specified by:
acquire in interface Sync


attempt

public boolean attempt(long msecs)
            throws InterruptedException
Specified by:
attempt in interface Sync


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
Specified by:
release in interface Sync