How it works...

The CountDownLatch class has three basic elements:

  • The initialization value that determines how many events the CountDownLatch object waits for
  • The await() method, called by the threads that wait for the finalization of all the events
  • The countDown() method, called by the events when they finish their execution

When you create a CountDownLatch object, it uses the constructor's parameter to initialize an internal counter. Every time a thread calls the countDown() method, the CountDownLatch object decrements the internal counter in one unit. When the internal counter reaches 0, the CountDownLatch object wakes up all the threads that were waiting in the await() method.

There's no way to re-initialize the internal counter of ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.