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. ]
|