Home » Core Java » util » concurrent » Java CountDownLatch Example

About Konstantina Dimtsa

Konstantina Dimtsa
Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.

Java CountDownLatch Example

In this example we will discuss about the class java.util.concurrent.CountDownLatch. java.util.concurrent.CountDownLatch is a synchronization mechanism that allows one or more threads to wait until a set of operations being performed in other threads completes.

1. How CountDownLatch works

A java.util.concurrent.CountDownLatch is initialized with a given count. The constructor of java.util.concurrent.CountDownLatch is defined as follows:

  • CountDownLatch(int count)
    Constructs a CountDownLatch initialized with the given count.

Then, the await() method blocks the main thread until the current count reaches zero. This will happen after continuous invocations of the countDown() method, which decrements the latch counter. When this latch counter reaches zero, all waiting threads are released and any subsequent invocations of await return immediately.

2. CountDownLatch example

In this section we will show a simple example of using java.util.concurrent.CountDownLatch.

First, create a java class named CountDownLatchExample.java with the following code:


package com.javacodegeeks.java.util.concurrent.countdownlatch;

import java.util.concurrent.*;

public class CountDownLatchExample {

	public static void main(String args[]) {

		final CountDownLatch latch = new CountDownLatch(3);

		Thread testThread1 = new Thread(new TestThread("FirstThread", latch));
		Thread testThread2 = new Thread(new TestThread("SecondThread", latch));
		Thread testThread3 = new Thread(new TestThread("ThirdThread", latch));


		try {
		} catch (InterruptedException ie) {

		System.out.println("All threads have started");



In the main thread, we initialize the CountDownLatch with count 3 and we start three other threads. The main thread will be blocked until the count reaches zero.

Also, create a java class named TestThread.java with the following code:


package com.javacodegeeks.java.util.concurrent.countdownlatch;

import java.util.concurrent.CountDownLatch;

public class TestThread extends Thread {

	private String name;
	private CountDownLatch latch;

	public TestThread(String name, CountDownLatch latch) {
		this.name = name;
		this.latch = latch;

	public void run() {

		System.out.println(name + " : started");
		try {
		} catch (Exception e) {



Εach of the other threads decrement the CountDownLatch.

If we run the above code, we will have the following results:

  • Output:
FirstThread : started
SecondThread : started
ThirdThread : started
All threads have started

Download the source code

This was an example of how to use java.util.concurrent.CountDownLatch class.

You can download the full source code of this example here : CountDownLatchExample

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....



Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.