In this post, We will talk and learn about CyclicBarrier in Java using an Example.
A CyclicBarrier is a synchronization helping class from java.util.concurrent package that allows a set of threads to all wait for each other to reach at common barrier point.
CyclicBarrier is useful when a program involves 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 that’s where it differs from CountDownLatch
Complete source code:
PassengerThread.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.infotech.tasks; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class PassengerThread extends Thread { private int duration; private CyclicBarrier barrier; public PassengerThread(int duration, CyclicBarrier barrier, String pname) { super(pname); this.duration = duration; this.barrier = barrier; } @Override public void run() { try { Thread.sleep(duration); System.out.println(Thread.currentThread().getName() + " is arrived."); int await = barrier.await(); if(await == 0) { System.out.println("Four passengers have arrived so cab is going to start.."); } } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.infotech.client; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import com.infotech.tasks.PassengerThread; /** * * This Client Program demonstrates how to use CyclicBarrier, Its used when number * of threads * needs to wait for each other before starting again. */ public class ClientTest { public static void main(String args[]) throws InterruptedException, BrokenBarrierException { System.out.println(Thread.currentThread().getName() + " has started"); CyclicBarrier barrier = new CyclicBarrier(4); PassengerThread p1 = new PassengerThread(1000, barrier, "John"); PassengerThread p2 = new PassengerThread(2000, barrier, "Martin"); PassengerThread p3 = new PassengerThread(3000, barrier, "Joya"); PassengerThread p4 = new PassengerThread(4000, barrier, "Sam"); PassengerThread p5 = new PassengerThread(1000, barrier, "Pipa"); PassengerThread p6 = new PassengerThread(2000, barrier, "Dolly"); PassengerThread p7 = new PassengerThread(3000, barrier, "Harman"); PassengerThread p8 = new PassengerThread(4000, barrier, "Brad"); p1.start(); p2.start(); p3.start(); p4.start(); p5.start(); p6.start(); p7.start(); p8.start(); System.out.println(Thread.currentThread().getName() + " has finished"); } } |
Output of this Program:
main has started
main has finished
John is arrived.
Pipa is arrived.
Martin is arrived.
Dolly is arrived.
Four passengers have arrived so cab is going to start..
Joya is arrived.
Harman is arrived.
Sam is arrived.
Brad is arrived.
Four passengers have arrived so cab is going to start..
You May Also Like:
How would you round a double value to certain decimal Precision and Scale ?
What is BlockingQueue? How can we implement Producer and Consumer problem using Blocking Queue?
How to get number of available processors in Java ?
That’s all about CyclicBarrier Example in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.