Name

CountDownLatch

Synopsis

This class synchronizes threads. All threads that call await( ) block until the countDown( ) method is invoked a specified number of times. The required number of calls is specified when the CountDownLatch is created. Once countDown( ) has been called the required number of times, all threads blocked in await( ) are allowed to resume, and any subsequent calls to await( ) do not block. getCount( ) returns the number of calls to countDown( ) that must still be made before the threads blocked in await( ) can resume. Note that there is no way to reset the count. Once a CountDownLatch has “latched,” it remains in that state forever. Create a new CountDownLatch if you need to synchronize another group of threads. Contrast this class with CyclicBarrier.

public class CountDownLatch {
// Public Constructors
     public CountDownLatch(int count);  
// Public Instance Methods
     public void await( ) throws InterruptedException;  
     public boolean await(long timeout, TimeUnit unit) throws InterruptedException;  
     public void countDown( );  
     public long getCount( );  
// Public Methods Overriding Object
     public String toString( );  
}

Get Java in a Nutshell, 5th 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.