Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of await() in CountDownLatch?

I have the following program, where I am using java.util.concurrent.CountDownLatch and without using await() method it's working fine.

I am new to concurrency and want to know the purpose of await(). In CyclicBarrier I can understand why await() is needed, but why in CountDownLatch?

Class CountDownLatchSimple:

public static void main(String args[]) {
  CountDownLatch latch = new CountDownLatch(3);
  Thread one = new Thread(new Runner(latch),"one");
  Thread two = new Thread(new Runner(latch), "two");
  Thread three = new Thread(new Runner(latch), "three");

  // Starting all the threads
  one.start(); two.start(); three.start();
  
}

Class Runner implements Runnable:

CountDownLatch latch;

public Runner(CountDownLatch latch) {
    this.latch = latch;
}

@Override
public void run() {
    System.out.println(Thread.currentThread().getName()+" is Waiting.");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    latch.countDown();
    System.out.println(Thread.currentThread().getName()+" is Completed.");
}

OUTPUT

two is Waiting.
three is Waiting.
one is Waiting.
one is Completed.
two is Completed.
three is Completed.

like image 575
NewToJava Avatar asked Jan 26 '17 04:01

NewToJava


People also ask

What is CountDownLatch await?

Class declaration CountDownLatch. await() method block the main thread execution until the current count reaches to zero. the count is decremented using countDown() method by executing threads when their task is completed. Any call to await returns immediately once the count is 0.

What is the use of CountDownLatch in Java?

CountDownLatch is used to make sure that a task waits for other threads before it starts. To understand its application, let us consider a server where the main task can only start when all the required services have started.

What is the difference between CyclicBarrier and CountDownLatch in Java?

As stated in the definitions, CyclicBarrier allows a number of threads to wait on each other, whereas CountDownLatch allows one or more threads to wait for a number of tasks to complete. In short, CyclicBarrier maintains a count of threads whereas CountDownLatch maintains a count of tasks.

Does CountDownLatch block thread?

Simply put, a CountDownLatch has a counter field, which you can decrement as we require. We can then use it to block a calling thread until it's been counted down to zero.


Video Answer


1 Answers

CountDownLatch is the synchronization primitive which is used to wait for all threads completing some action.

Each of the thread is supposed to mark the work done by calling countDown() method. The one who waits for the action to be completed should call await() method. This will wait indefinitely until all threads mark the work as processed, by calling the countDown(). The main thread can then continue by processing the worker's results for example.

So in your example it would make sense to call await() at the end of main() method:

latch.await();

Note: there are many other use cases of course, they don't need to be threads but whatever that runs usually asynchronously, the same latch can be decremented several times by the same task etc. The above describes just one common use case for CountDownLatch.

like image 57
Zbynek Vyskovsky - kvr000 Avatar answered Oct 11 '22 21:10

Zbynek Vyskovsky - kvr000