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 itsrun()
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 itsstart()
API method in order to beggin execution. It overrides therun()
API method of the Runnable, where it callsawait()
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 theBarAction
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 callawait()
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.