Java CyclicBarrier Class

Category: Java   Tags: Java, Java Thread, Java MultiThread, Thread Synchronization, Immutable Objects, Java Concurrent Collection

CyclicBarrier allows a set of threads to wait at a predetermined execution point until all threads in the set have reached that point. It allows you to define a synchronization object that suspends until the specified number of threads has reached the barrier point.

Constructors

public CyclicBarrier(int numThreads)
Creates a new CyclicBarrier. Here numThreads is the number of threads that must reach the barrier before execution continues.
public CyclicBarrier(int numThreads, Runnable action)
Creates a new CyclicBarrier. Here numThreads is the number of threads that must reach the barrier before execution continues. action is a thread that will be executed when the barrier is reached.

Methods

public int await() throws InterruptedException, BrokenBarrierException
Waits until all parties have invoked await on this barrier.
public int await(long timeout, TimeUnit unit) throws InterruptedException, BrokenBarrierException, TimeoutException
Waits until all parties have invoked await on this barrier or the specified waiting time elapses.
public void reset()
Resets the barrier to its initial state.
public boolean isBroken()
Queries if this barrier is in a broken state.
public int getParties()
Returns the number of parties required to trip this barrier.

JavaCyclicBarrierExample.java

                            package com.tutorial.thread;

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

                            class JavaCyclicBarrierExample {

                                public static void main(String args[]) {
                                    CyclicBarrier cb = new CyclicBarrier(3, new BarrierAction());

                                    new MyThread(cb, "Thread1");
                                    new MyThread(cb, "Thread2");
                                    new MyThread(cb, "Thread3");
                                }
                            }
                            // A thread of execution that uses a CyclicBarrier.

                            class MyThread implements Runnable {

                                CyclicBarrier cbar;
                                String name;

                                MyThread(CyclicBarrier c, String n) {
                                    cbar = c;
                                    name = n;
                                    new Thread(this).start();
                                }

                                public void run() {
                                    System.out.println(name);
                                    try {
                                        cbar.await();
                                    } catch (BrokenBarrierException exc) {
                                        System.out.println(exc);
                                    } catch (InterruptedException exc) {
                                        System.out.println(exc);
                                    }
                                }
                            }

                            // This thread will be executed once barrier is reached
                            //afte excutiing three thread
                            class BarrierAction implements Runnable {

                                public void run() {
                                    System.out.println("Barrier Reached..");
                                }
                            }
                        

Output:

                            Thread2
                            Thread1
                            Thread3
                            Barrier Reached..