Live coding puzzles question 177 of 180 senior
Implement a simple CountDownLatch yourself.
- Spin on a volatile counter
- Correct: A guarded wait: decrement under a lock and
notifyAllat zero, withawaitlooping while the count is above zero - A
Semaphorewith zero permits Thread.joinon every thread
class Latch {
private int count;
Latch(int count) {
this.count = count;
}
synchronized void countDown() {
if (count > 0 && --count == 0) {
notifyAll(); // once, when it actually reaches zero
}
}
synchronized void await() throws InterruptedException {
while (count > 0) {
wait();
}
}
}
The points being checked: while around wait, notifyAll rather than notify (every waiter must be
released, not one), not counting below zero, and the fact that a latch is one-shot, which is what
distinguishes it from a CyclicBarrier that resets.
The real one is an AQS subclass using the state as the count, which is worth naming as the follow-up.