threads

CyclicBarrier example

This is an example of how to use a CyclicBarrier. A CyclicBarrier is a synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released. In order to create and use the CyclicBarrier we have created the following:

  • A class named BarAction that implements the Runnable and overrides its run() API method. This runnable is the command to execute when the barrier will be tripped.
  • A class named MyThread that also implements the Runnable and consists of a CyclicBarrier and a String name. The classe’s constructor creates a new Thread for this runnable and calls its start() API method in order to beggin execution. It overrides the run() API method of the Runnable, where it calls await() API method of CyclicBarrier for its CyclicBarrier object. This method waits until all parties (Threads) have invoked await on this barrier. If the current thread is not the last to arrive then it is disabled for thread scheduling purposes and lies dormant until one of the following things happens: the last thread arrives, or some other thread interrupts this or one of the other waiting threads, or some other thread times out while waiting for barrier, or some other thread invokes reset on this barrier. If the current thread has its interrupted status set on entry to this method or is interrupted while waiting then InterruptedException is thrown and the current thread’s interrupted status is cleared. If the barrier is reset while any thread is waiting, or if the barrier is broken when await is invoked, or while any thread is waiting, then BrokenBarrierException is thrown. If any thread is interrupted while waiting, then all other waiting threads will throw BrokenBarrierException and the barrier is placed in the broken state.
  • We create a new CyclicBarrier, using the CyclicBarrier(int parties, Runnable barrierAction) constructor, with 3 threads to be the parties and the BarAction runnable to be the barrier action.
  • Then, we create three new instances of MyThread, using the above created cyclic barrier and a String message. All three parties will start execution and call await() method of CyclicBarrier, as described above. When all parties have invoked await on this barrier, the barrier action will be executed.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

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

class BarDemo {

    public static void main(String args[]) {

  

  CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new BarAction());


  System.out.println("Start");


  new MyThread(cyclicBarrier, "One");

  new MyThread(cyclicBarrier, "Two");

  new MyThread(cyclicBarrier, "Three");

    }
}

class MyThread implements Runnable {

    CyclicBarrier cbar;
    String name;

    MyThread(CyclicBarrier c, String n) {

  cbar = c;

  name = n;

  new Thread(this).start();
    }

    @Override
    public void run() {


  System.out.println(name);


  try {


cbar.await();

  } catch (BrokenBarrierException exc) {


System.out.println(exc);

  } catch (InterruptedException exc) {


System.out.println(exc);

  }
    }
}

class BarAction implements Runnable {

    @Override
    public void run() {

  System.out.println("Barrier");
    }
}

public class CyclicBarrierExample {

    public static void main(String args[]) {

  CyclicBarrier cb = new CyclicBarrier(3, new BarAction());


  System.out.println("Start");


  new MyThread(cb, "One");

  new MyThread(cb, "Two");

  new MyThread(cb, "Three");


  new MyThread(cb, "X");

  new MyThread(cb, "Y");

  new MyThread(cb, "Z");

    }
}

Output:

Start
One
Two
Three
Barrier

 
This was an example of how to use a CyclicBarrier in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button