CountDownLatch in Java

The CountDownLatch class is another important class for concurrent execution. It is a synchronization aid that allows one or more than one thread to wait until a set of operations being performed in another thread is completed.

It initializes with the count, which we pass to the constructor. The invocation of the countDown() method causes the await methods to block until the current count reaches zero, after which all waiting threads are released, and any subsequent invocations of await immediately return. CountDownLatch doesn't require that thread countdown() to wait for the count to reach zero before proceeding. It prevents any thread from proceeding past and await until all threads can pass.

CountDownLatch in Java

The CountDownLatch is used for multiple purposes based on the count value:

  1. If we initialize the CountDownlatch with count value 1, it will work as a simple on/off latch or gate.
  2. If we initialize the CountDownLatch with count value N, it will be used to make one thread wait until N threads have completed some action or some action has been completed N times.

Constructor of CountDownLatch

CountDownLatch provides a parameterized constructor. It accepts an integer value for the count. It constructs the CountDownLatch and initializes it with the given count value.

Note: The constructor throws an IllegalArgumentException when the value of count is negative.

Syntax

Parameters

count -> it is the number of times countdown() must be invoked before the thread can pass through await().

Methods of CountDownLatch

The CountDownLatch class has several methods which we can use for concurrency control. The CountDownLatch class also provides methods of java.lang.Object class because it is inherited by the CountDownLatch class.

The methods of the java.lang.Object class are as follows:

  1. clone
  2. equals
  3. finalize
  4. getClass
  5. hashCode
  6. notify
  7. notifyAll
  8. wait
  9. wait
  10. wait

If you don't have knowledge of the Object class methods, go through the following link: https://www.javatpoint.com/object-class

The CountDownLatch provides the following methods:

1. await()

The await() method cause the current thread to wait until one of the following is not done:

  1. The latch has counted down to zero.
  2. Interruption of the thread is not done.

The await() method immediately returns when the value of the current count is set to zero.

The await() method disabled the current thread for thread scheduling purposes when the current count is neither zero nor a negative number, and the current thread remains dormant until one of the following things happens:

  • The value of the count reaches zero because of the invocations of the countDown()
  • The current thread is interrupted by some other thread.

Syntax:

The await() method has the following syntax:

It does not accept any value and does not return any value.

Throws

The await() method throws interruptedException when the current thread is interrupted while waiting.

2. await()

It is another variation of the await() method that causes the current thread to wait until the following is not done:

  1. The latch has counted down to zero.
  2. The specified waiting time elapses.
  3. The thread is interrupted.

The await() method immediately returns the value true when the value of the current count is set to zero.

The await() method disabled the current thread for thread scheduling purposes when the current count is neither zero nor a negative number, and the current thread remains dormant until one of the following things happens:

  • The value of the count reaches zero because of the invocations of the countDown()
  • The specified waiting time elapses.
  • The current thread is interrupted by some other thread.

Syntax:

Parameters:

timeout: The timeout parameter is of type long, which defines the maximum time to wait.

unit: The unit parameter is of type TimeUnit, which defines the time unit of the timeout argument.

Returns

It returns true when the count reaches zero and returns false when the waiting time elapsed before the count reached zero.

Throws

The await() method throws interruptedException when the current thread is interrupted while waiting.

3. countdown

The countdown() method is another important method provided by the CountDownLatch class. It documents the count of the latch and releases all the waiting threads when the count reaches zero. It has done the following things:

  1. The count is decremented when the current count is greater than zero.
  2. All waiting threads will be re-enabled for thread scheduling purposes when the new count is zero.
  3. Nothing will happen in the case when the current count equals zero.

4. getCount

It is another important method provided by the CoutDownLatch class. The getCount() method is used to get the count of the latch which we currently use.

Syntax:

It does not accept any parameter. It returns the current count of the latch.

5. toString

The toString() is the last method provided by the CountDownLatch class. It is used to get a string that will identify the latch and its state.

Syntax:

Overrides

It overrides the toString in class Object.

Returns

It returns the string that will identify the latch and its state.

Let's take an example of the CountDownLatch and understand how it works and can implement in java.

CountDownLatchExample.java

Output:

CountDownLatch in Java