Java Concurrency Interview

Live coding puzzles question 177 of 180 senior

Implement a simple CountDownLatch yourself.

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.

puzzle wait-notify aqs

Quiz yourself on Live coding puzzles Suggest a correction