CyclicBarrier in Java

In this article, we will discuss about CyclicBarrier in Java.
 

CyclicBarrier

 
The CyclicBarrier class allows synchronization of multiple threads at a common point.

CyclicBarrier can be used when a set of two or more threads must wait at some point until all threads have reached that point.
 
CyclicBarrier
 

Creating CyclicBarrier

 
CyclicBarrier class provides following constructors :
 


CyclicBarrier(int numberOfThreads) 

This creates a new CyclicBarrier that waits until numberOfThreads reach a common barrier point.
 


CyclicBarrier(int numberOfThreads, Runnable action) 

This constructor creates a CyclicBarrier with an action that specifies a thread that will be executed until the barrier is reached.

When threads reach the barrier point, we call its await() method. Once the specified number of threads reach the barrier point, execution can continue.

 

await() method

 
The threads call await() to wait at the barrier point until the specified number is reached.


public int await() 
                throws InterruptedException,BrokenBarrierException

This form makes the execution wait until all threads have invoked the await() method on this barrier.
 


public int await(long timeout,TimeUnit unit) 
                throws InterruptedException,BrokenBarrierException,TimeoutException

This form makes the execution wait until all threads have invoked the await() method on this barrier, or the specified waiting time elapses.
 

CyclicBarrier Example

 
In this example, we create a cyclic barrier with count 3. We then create 3 threads that must wait for each other until all of them reach the barrier point before proceeding with execution.
 

package com.topjavatutorial;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {

  public static void main(String[] args) {

    CyclicBarrier cb = new CyclicBarrier(3,new FinalAction());
    
    MyThread_CB thread1 = new MyThread_CB(cb, "First Thread");
    MyThread_CB thread2 = new MyThread_CB(cb, "Second Thread");
    MyThread_CB thread3 = new MyThread_CB(cb, "Third Thread");
  }

}

package com.topjavatutorial;

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

public class MyThread_CB implements Runnable {
  
  CyclicBarrier cb;
  String name;
  
  MyThread_CB(CyclicBarrier cb,String name){
    this.name=name;
    this.cb = cb;
    new Thread(this).start();
  }

  @Override
  public void run() {
      try {
        System.out.println(name + " waiting");
        cb.await();
      } catch (InterruptedException | BrokenBarrierException e) {
        e.printStackTrace();
      }
      System.out.println(name + " completed");
  }

}

package com.topjavatutorial;

public class FinalAction implements Runnable {

  @Override
  public void run() {
    System.out.println("All threads reached common point");
  }

}


 

Output

 

First Thread waiting
Second Thread waiting
Third Thread waiting
All threads reached common point
First Thread completed
Third Thread completed
Second Thread completed

Note: The order in which these threads execute may vary.
 

Summary

 
A cyclic barrier lets a set of threads wait for each other to reach a common barrier point.

The barrier is cyclic because it can be reused after the waiting threads are released.

CyclicBarrier helps with the parallelization of tasks using the divide and conquer approach.
 
 

You may also like reading

CountDownLatch in Java

Java 8 new features

Top 10 Tricky Java Puzzles

Top 10 Recursion Coding Interview Questions

Top Java Tutorial Articles : March 2016

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags