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

Saturday, September 13, 2014

Java CyclicBarrier

 September 13, 2014     Java     No comments   

CyclicBarrier Introduction

CyclicBarrier is a synchronization object that will release when a given number of threads are waiting on it. CyclicBarrier is initialized with a count that indicates the number of threads that must wait on this barrier. CyclicBarrier is useful in applications where threads needs to wait for each other.

Java CyclicBarrier Class

Java supports CyclicBarrier synchronization object. Some of the key APIs of CyclicBarrier class are listed below.
  • CycliBarrier(int count) - Constructor that creates CyclicBarrier with a specified count. Indicates the number of threads that must invoke await before the barrier is released.
  • int await() - Waits till all threads have invoked wait on this barrier.
  • int await(long timeout, TimeUnit unit) - Waits until all threadshave invoked await on this barrier, or the specified waiting time elapses.

Java CyclicBarrier Example

In this example we have a short task and a long task. We use a CyclicBarrier object to make the short task to wait till the long task has completed. 
package com.sourcetricks.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierExample {

  private static class MyTask1 implements Runnable {
    CyclicBarrier barrier;
    
    MyTask1(CyclicBarrier barrier) {
      this.barrier = barrier;
    }
    
    @Override
    public void run() {
      System.out.println("In MyTask1 ...");
      try {
        Thread.sleep(2000);
        barrier.await();
      } catch (InterruptedException | BrokenBarrierException e) {
        e.printStackTrace();
      }
      System.out.println("Completing MyTask1 ...");
    }
  }
  
  private static class MyTask2 implements Runnable {
    CyclicBarrier barrier;
    
    MyTask2(CyclicBarrier barrier) {
      this.barrier = barrier;
    }
    
    @Override
    public void run() {
      System.out.println("In MyTask2 ...");
      try {
        Thread.sleep(10000);
        barrier.await();
      } catch (InterruptedException | BrokenBarrierException e) {
        e.printStackTrace();
      }
      System.out.println("Completing MyTask2 ...");
    }
  }
  
  public static void main(String[] args) {
    CyclicBarrier barrier = new CyclicBarrier(2);
    Thread t1 = new Thread(new MyTask1(barrier));
    Thread t2 = new Thread(new MyTask2(barrier));
    t1.start();
    t2.start();
  }
}
This program produces the following output.
In MyTask1 ...
In MyTask2 ...
Completing MyTask2 ...
Completing MyTask1 ...
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