In this post, we will see about CyclicBarrier in java. CyclicBarrier was introduced in Java 5 with other concurrency utils such as CountDownLatch, ConcurrentHashMap and BlockingQueue.
CyclicBarrier is synchronized aid which allows set of threads to wait for each other at common barrier points.It is called cyclic because it can be reused once waiting threads are released.
For example:
Let’s say you have 3 threads, you want all threads(terms as parties) to reach a common point and then only they should proceed ahead.In this case, you can use CyclicBarrier with 3 parties and once 3 threads reach a common point, you can call an event which will implement runnable interface and Three threads will be released.
Difference between CountDownLatch and CyclicBarrier
The major difference between CyclicBarrier and CoundDownLatch is that CyclicBarrier can be reused.You can not use CountDownLatch once used. You can read more differences between CountDownLatch and CyclicBarrier.
Java CyclicBarrier example:
Step 1: Create a file named "RunnableTask.java" in package .src.org.arpit.java2blog
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 42 |
package org.arpit.java2blog; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class RunnableTask implements Runnable{ CyclicBarrier cyclicBarrier; long sleepTime; RunnableTask(CyclicBarrier cyclicBarrier,long sleepTime){ this.cyclicBarrier=cyclicBarrier; this.sleepTime=sleepTime; } @Override public void run() { try { Thread.sleep(sleepTime); System.out.println(Thread.currentThread().getName() + " is waiting for "+(cyclicBarrier.getParties()-cyclicBarrier.getNumberWaiting()-1)+ " other threads to reach common barrier point"); /* * when 3 parties will call await() method (i.e. common barrier point) * CyclicBarrrierEvent will be triggered and all waiting threads will be released. */ cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } System.out.println("As "+cyclicBarrier.getParties()+ " threads have reached common barrier point " + Thread.currentThread().getName() + " has been released"); } } |
This is a Runnable task which will be executed by each thread.
Step 2: Create a file named "CyclicBarrierFinishEvent.java" in package .src.org.arpit.java2blog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; public class CyclicBarrierFinishEvent implements Runnable{ public void run() { System.out.println("As 3 threads have reached common barrier point " + ", CyclicBarrrierFinishEvent has been triggered"); System.out.println("You can update shared variables if any"); } } |
CyclicBarrierFinishEvent will be called when 3 parties (Initialized with CyclicBarrier object) reaches to a common barrier point.
Step 3: Create a file named "CyclicBarrierMain.java" in package .src.org.arpit.java2blog
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 |
package org.arpit.java2blog; import java.util.concurrent.CyclicBarrier; /** @author Arpit Mandliya*/ public class CyclicBarrierMain { public static void main(String[] args) { /* * Create CountDownLatch with 3 parties, when all 3 parties * will reach common barrier point CyclicBarrrierFinishEvent will be * triggered */ CyclicBarrier cyclicBarrier=new CyclicBarrier(3 ,new CyclicBarrierFinishEvent()); RunnableTask runnableTask1=new RunnableTask(cyclicBarrier,1000); RunnableTask runnableTask2=new RunnableTask(cyclicBarrier,2000); RunnableTask runnableTask3=new RunnableTask(cyclicBarrier,3000); //Create and start 3 threads new Thread(runnableTask1,"Thread-1").start(); new Thread(runnableTask2,"Thread-2").start(); new Thread(runnableTask3,"Thread-3").start(); /* * We are reusing cyclic barrier using below threads * */ RunnableTask runnableTask4=new RunnableTask(cyclicBarrier,4000); RunnableTask runnableTask5=new RunnableTask(cyclicBarrier,5000); RunnableTask runnableTask6=new RunnableTask(cyclicBarrier,6000); // Create and start 3 more threads new Thread(runnableTask4,"Thread-4").start(); new Thread(runnableTask5,"Thread-5").start(); new Thread(runnableTask6,"Thread-6").start(); } } |
Let’s run the program, then we will understand the output:
Thread-2 is waiting for 1 other threads to reach common barrier point
Thread-3 is waiting for 0 other threads to reach common barrier point
As 3 threads have reached common barrier point ,CyclicBarrrierFinishEvent has been triggered
You can update shared variables if any
As 3 threads have reached common barrier point Thread-3 has been released
As 3 threads have reached common barrier point Thread-1 has been released
As 3 threads have reached common barrier point Thread-2 has been released
Thread-4 is waiting for 2 other threads to reach common barrier point
Thread-5 is waiting for 1 other threads to reach common barrier point
Thread-6 is waiting for 0 other threads to reach common barrier point
As 3 threads have reached common barrier point ,CyclicBarrrierFinishEvent has been triggered
You can update shared variables if any
As 3 threads have reached common barrier point Thread-6 has been released
As 3 threads have reached common barrier point Thread-4 has been released
As 3 threads have reached common barrier point Thread-5 has been released
Below diagram will make you understand output better.
That’s all about Java CyclicBarrier example.