CyclicBarrier is another concurrency utility introduced in JDK 1.5 , Whis is a java synchronizer that allows two or more threads to wait for each other at a common point known as the barrier before it starts processing.
When all threads have reached barrier point then all waiting threads are released, and if any Runnable action is defined then it will be triggered as well.
CyclicBarrier’s constructor
There are two constructors with the below parameters.
- (int parties) : Here parties is the number of threads need to wait at barrier.
- (int parties, Runnable barrierAction) : The second parameter is the barrier action which will get executed after all threads have reached common barrier point.
Important methods:
- await();This method is similar to countdown() method of CountDownLatch .This method is called on the CyclicBarrier object in the thread which is to be paused. If there are 4 threads that must pause at barrier point then await() must be called in the run() of those threads when the last thread (4) calls the await() method, after that every thread should wake up and start processing.
- await(long time,TimeUnit units):This method is same as the above method but the thread here is paused for a given time If barrier is broken before a thread calls await() then this method will throw BrokenBarrierException.
- reset(); This method resets the barrier to the initial state.
How it works ?
Create instance of CyclicBarrier and initialized it with 4(working threads), this is actually number of threads need to wait at barrier. The barrier will not be broken until all threads are arrived. A thread is said to be arrived when it calls await() method.
In below program, we will use four worker threads and one main thread, which is running your main method. We have given each thread a different name, starting from Thread-1 to Thread-4 just to have a better understanding. Now pass same instance of the cyclic barrier to each thread. In run method implementation we define sleep method that causes each thread to sleep for some seconds and then call await() method on the barrier.
Example:
In below program, we will use four worker threads and one main thread, which is running your main method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class CyclicBarrierDemo { public static void main(String[] args) { CyclicBarrier cyclicBarrier=new CyclicBarrier(4 ,new RunnableAction()); System.out.println("Created CyclicBarrier with 4 threads and Runnable Action will be triggered when all " + "4 threads will reach at common barrier point "); // Creating and starting 4 threads for(int i=1;i<=4;i++){ new Thread(new CyclicRunnable(cyclicBarrier),"Thread-"+i).start(); } } } |
1 2 3 4 5 6 7 8 |
class RunnableAction implements Runnable{ @Override public void run() { System.out.println("Runnable Action will be triggered when all the threads reach barrier point"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class CyclicRunnable implements Runnable{ private CyclicBarrier cyclicBarrier; CyclicRunnable(CyclicBarrier cyclicBarrier){ this.cyclicBarrier=cyclicBarrier; } @Override public void run() { System.out.println(Thread.currentThread().getName() +" is arrived at common barrier point"); try { Thread.sleep(1000); cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " has been released for further processing"); } } |
Output:
Created CyclicBarrier with 4 threads and Runnable Action will be triggered when all 4 threads will reach at common barrier point
Thread-1 is arrived at common barrier point
Thread-3 is arrived at common barrier point
Thread-2 is arrived at common barrier point
Thread-4 is arrived at common barrier point
Runnable Action will be triggered when all the threads reach barrier point
Thread-1 has been released for further processing
Thread-4 has been released for further processing
Thread-2 has been released for further processing
Thread-3 has been released for further processing
Realtime Use cases :
- Multiplayer video game, game is started only when all players joined.
- If you are going to a picnic, and you need to first meet at some common point from where you all will start your journey.
- It can be used where big task is broken down into smaller tasks and to complete the job you need output from individual small task
Differences between CountDownLatch and CyclicBarrier:
- CyclicBarrier has reset method which is not present in CountDownLatch,so CyclicBarrier can be used again once count becomes 0
- CyclicBarrier has constructor that defines Runnable event,so it can be use to trigger Runnable event once counter becomes 0 but CountDownLatch cant not do this. Refer countDownLatch