StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Tuesday, September 9, 2014

Java CountDownLatch

 September 09, 2014     Java     No comments   

CountDownLatch Introduction

CountDownLatch is a synchronization object that allows a thread to wait till certain events occur in other threads. We can make the current thread to wait until few dependent threads have completed their operation. CountDownLatch starts with a initial count. Thread that needs to wait, blocks until the count reaches to zero. Dependent threads on completing their task start to count down after which all the blocked threads are released.

Java CountDownLatch Class

Java supports CountDownLatch synchronization object. Some of the key APIs of CountDownLatch class are listed below.
  • CountDownLatch(int count) - Constructor that creates CountDownLatch with a specified count.
  • void await() - Current threads waits till the latch has counted down to zero
  • boolean await(long timeout, TimeUnit unit) - Causes the current thread to wait till the latch has counted down to zero or the specified timeout has occurred.
  • void countDown() - Decrements the latch count. Releases waiting threads on count reaching zero.

Java CountDownLatch Example

In this example, we create a CountDownLatch object with initial count of two. The main thread creates pre-processing threads and awaits for them to be completed. Once the pre-processing threads complete their tasks they decrement the latch count. Once the latch reaches count of zero then the main thread is released and proceeds to completion.
package cdl;

import java.util.concurrent.CountDownLatch;

public class CDLExample {

  private static class PreProcess1 implements Runnable {

    CountDownLatch cdl = null;
    PreProcess1(CountDownLatch cdl) {
      this.cdl = cdl;
    }
    
    @Override
    public void run() {
      System.out.println("Preprocess1 done");
      cdl.countDown();
    }
  }

  private static class PreProcess2 implements Runnable {

    CountDownLatch cdl = null;
    PreProcess2(CountDownLatch cdl) {
      this.cdl = cdl;
    }
    
    @Override
    public void run() {     
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("Preprocess2 done");
      cdl.countDown();
    }
  }
  
  public static void main(String[] args) {
    CountDownLatch cdl = new CountDownLatch(2);
    try {
      new Thread(new PreProcess1(cdl)).start();
      new Thread(new PreProcess2(cdl)).start();
      System.out.println("Waiting ...");
      cdl.await();
      System.out.println("Completed ...");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
This program produces the following output.
Waiting ...
Preprocess1 done
Preprocess2 done
Completed ...
Read other concurrency tutorials from Java Tutorials page.
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk