Konstantina Dimtsa

About 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:

CountDownLatchExample.java

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));

		testThread1.start();
		testThread2.start();
		testThread3.start();

		try {
			latch.await();
		} 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:

TestThread.java

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 {
			Thread.sleep(2000);
		} catch (Exception e) {
		}

		latch.countDown();
	}

}

Ε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.

Download
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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close