EDU.oswego.cs.dl.util.concurrent
Class CountDown
- 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. ]
CountDown(int count) - Create a new CountDown with given count value *
|
count_
protected int count_
initialCount_
protected final int initialCount_
CountDown
public CountDown(int count)
Create a new CountDown with given count value *
acquire
public void acquire()
throws InterruptedException
- acquire in interface Sync
attempt
public boolean attempt(long msecs)
throws InterruptedException
- 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
- release in interface Sync