Java-CyclicBarrier

CyclicBarrier

A situation not uncommon in concurrent programming occurs when a set of two or more threads must wait at a predetermined execution point until all threads in the set have reached that point. To handle such a situation, the concurrent API supplies the CyclicBarrier class. It enables you to define asynchronization object that suspends until the specified number of threads has reached the barrier point. CyclicBarrier has two constructors :

CyclicBarrier

Here, numThreads specifies the number of threads that must reach the barrier before execution continues. In the second form, action specifies a thread that will be executed when the barrier is reached, but before any threads are released. This barrier action is useful for updating shared-state before the numThreads continue.

CyclicBarrier 2 3

Here is the general procedure that you will follow to use CyclicBarrier. First, create a CyclicBarrier object, specifying the number of threads that you will be waiting for. Next, when each thread reaches the barrier, have it call await( ) on that object. This will pause execution of the thread until all of the other threads also call await( ). Once the specified number of threads has reached the barrier, await( ) will return and execution will resume. Also, if you have specified an action, then that thread is executed. The barrier is called cyclic because it can be re-used after the waiting threads are released. The await( ) method has two forms:

CyclicBarrier

The first form waits until all the threads have reached the barrier point. The second form waits only for the period of time specified by wait. The units represented by wait are specified by tu. Both forms return a value that indicates the order that the threads arrive at the barrier point. The first thread returns a value equal to the number of threads waited upon minus one. The last thread returns zero.

CyclicBarrier

If any of the waiting threads is interrupted or times out (using the timed wait version of the await() method) the barrier is said to be “broken” and all of the waiting threads receive a BrokenBarrierException. In theory, the barrier can be “fixed” by calling its reset() method. It is necessary when BrokenBarrierException occurs. Without reset the barrier can cause the BrokenBarrierException again by the call of the await() method.
Program

CyclicBarrier CyclicBarrier


Program Source

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

class MyClass extends Thread {

    CyclicBarrier cb;

    MyClass(CyclicBarrier c) {

        cb = c;
    }

    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + "--> Waiting");
            cb.await();
            System.out.println(Thread.currentThread().getName() + "--> Gets a permit");
            for (int i = 0; i < 3; i++) {                 
                 Thread.sleep(1000);                 
                 System.out.println(Thread.currentThread().getName() + "-->" + i);
            }
        } catch (BrokenBarrierException | InterruptedException e) {}
    }
}

class Run extends Thread{
    
    public void run(){
        System.out.println("All threads are released");
    }
}

public class Javaapp {

    public static void main(String[] args) {

        CyclicBarrier d = new CyclicBarrier(3, new Run());
        new MyClass(d).start();
        new MyClass(d).start();
        new MyClass(d).start();
    }
}

Leave a Comment