Posts Java Multithreading - Countdown Latches
Post
Cancel

Java Multithreading - Countdown Latches

This following post is based on the Udemy Course: Java Multithreading


A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. A CountDownLatch is initialized with a given count.The await methods block until the current count reaches zero due to invocations of the countDown method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon– the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier. A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as asimple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown. A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times. A useful property of a CountDownLatch is that it doesn’t require that threads calling countDown wait for the count to reach zero before proceeding, it simply prevents anythread from proceeding past an await until allthreads could pass.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.threads;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Processor implements Runnable{

 CountDownLatch latch;
 
 Processor(CountDownLatch latch){
  this.latch = latch;
 }
 
 @Override
 public void run() {
  
  System.out.println("Started.");
  
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  
  //reduce the count of latch by 1
  latch.countDown();
  
 }
 
}

public class App {
 
 public static void main(String[] args) {
  
  //create a latch
  CountDownLatch countDownLatch = new CountDownLatch(3);
  
  //use an executor service to create 3 worker threads
  ExecutorService executorService = Executors.newFixedThreadPool(3);
  
  //assign 3 tasks to the threads
  //the threads will reduce the value of latch by 1
  for(int i=0; i<3; i++) {
   executorService.submit(new Processor(countDownLatch));
  }
  
  //wait until the value of latch becomes 0
  try {
   countDownLatch.await();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  
  System.out.println(Thread.currentThread().getName() + " has finished");
  
 }

}

OUTPUT:

1
2
3
4
Started.
Started.
Started.
main has finished
This post is licensed under CC BY 4.0 by the author.