Design a thread-safe one-shot latch that opens after a fixed number of signals.
Implement the CountDownLatch class:
CountDownLatch(initialCount) creates a latch with the given non-negative count.countDown() decreases the count by one when it is positive. Once the count reaches zero, the latch opens permanently. Additional calls are no-ops.await() waits until the count reaches zero and then returns. If the latch is already open, it returns immediately.getCount() returns the current count without waiting.
All methods may be called concurrently. When the count reaches zero, every thread blocked in await must be released. Writes performed by signaler threads before countDown must be visible to a thread after its await returns.
The latch is one-shot: it never resets and its count never becomes negative.
The judge creates all signaler and waiter threads. Your implementation should contain only the synchronization inside CountDownLatch. Standard concurrency and thread APIs are preloaded, so you do not need import, include, or using statements.
Example 1:
Input:
Output:
Explanation: The waiting thread remains blocked after the first signal and is released by the second.
Example 2:
Input:
Output:
Explanation: A zero-count latch starts open and stays open.
Constraints
0 <= initialCount <= 1_000- At most
100 threads access one latch. - At most
2_000 total method calls are made on one latch. - Every latch on which the judge calls
await eventually reaches zero. - Judge threads are not interrupted while blocked in
await.