CyclicBarrier in Java with Examples
Last Updated :
07 May, 2025
Java CyclicBarrier is used to make threads wait for each other. It is used when different threads process a part of the computation, and when all threads have completed the execution, the result needs to be combined in the parent thread. In other words, a CyclicBarrier is used when multiple threads carry out different sub-tasks and the output of these sub-tasks needs to be combined to form the final output. After completing its execution, threads call the await() method and waits for other threads to reach the barrier. Once all the threads have reached the barriers, then give the way for the threads to proceed.
Working of CyclicBarrier
CyclicBarriers are defined in java.util.concurrent package. First, a new instance of a CyclicBarrier is created specifying the number of threads that the barrier should wait upon.
CyclicBarrier newBarrier = new CyclicBarrier(numberOfThreads);
Each thread does some computation, and after completing its execution, it calls the await() method as shown below:
public void run() {
// thread does the computation
newBarrier.await();
}
The image below demonstrates the Working of the Cyclic Barrier

Once all the threads that called await() reach the barrier and the total count equals the specified number of threads then the barrier will allow the threads to continue. The CyclicBarrier can be set up with an action that runs after all threads have reached the barrier. This action can be used to combine or make use of the results from the threads waiting at the barrier.
Runnable action = ... // action to be performed when all threads reach the barrier;
CyclicBarrier newBarrier = new CyclicBarrier(numberOfThreads, action);
Methods of CyclicBarrier
Now, we are going to discuss the methods of CyclicBarrier in Java
1. getParties(): This method returns the number of parties requires to trip this barrier.
Syntax:
public int getParties()
Return Type: Returns the number of parties required to trip this barrier.
2. reset(): This method resets the barrier to its initial state.
Syntax:
public void reset()
Return Type: void but resets the barrier to its initial state. If any parties are currently waiting at the barrier, they will return with a BrokenBarrierException.
3. isBroken(): This method queries if this barrier is in a broken state.
Syntax:
public boolean isBroken()
Return Type: Returns true if one or more parties broke out of this barrier due to interruption or timeout since construction or the last reset, or a barrier action failed due to an exception; false otherwise.
4. getNumberWaiting(): This method returns the number of parties currently waiting at the barrier.
Syntax:
public int getNumberWaiting()
Return Type: Returns the number of parties currently blocked in await().
5. await(): This method Waits until all parties have invoked await on this barrier.
Syntax:
public int await() throws InterruptedException, BrokenBarrierException
Return Type: Return the arrival index of the current thread, where index getParties() - 1 indicates the first to arrive and zero indicates the last to arrive.
6. await(long timeout, TimeUnit unit): This method waits until all parties have invoked await() on this barrier, or the specified waiting time elapses.
Syntax:
public int await(long timeout, TimeUnit unit)
throws InterruptedException, BrokenBarrierException, TimeoutException
Return Type: Return the arrival index of the current thread, where index getParties() - 1 indicates the first to arrive and zero indicates the last to arrive.
Program to Demonstrate Execution on CyclicBarrier
Java
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
class Computation1 implements Runnable {
public static int product = 0;
public void run() {
product = 2 * 3;
try {
Tester.newBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
}
class Computation2 implements Runnable {
public static int sum = 0;
public void run() {
// check if newBarrier is broken or not
System.out.println("Is the barrier broken? - " + Tester.newBarrier.isBroken());
sum = 10 + 20;
try {
Tester.newBarrier.await(3000, TimeUnit.MILLISECONDS);
// number of parties waiting at the barrier
System.out.println("Number of parties waiting at the barrier at this point = " + Tester.newBarrier.getNumberWaiting());
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
public class Tester implements Runnable {
public static CyclicBarrier newBarrier = new CyclicBarrier(3);
public static void main(String[] args) {
// parent thread
Tester test = new Tester();
Thread t1 = new Thread(test);
t1.start();
}
public void run() {
System.out.println("Number of parties required to trip the barrier = " + newBarrier.getParties());
System.out.println("Sum of product and sum = " + (Computation1.product + Computation2.sum));
// objects on which the child thread has to run
Computation1 comp1 = new Computation1();
Computation2 comp2 = new Computation2();
// creation of child threads
Thread t1 = new Thread(comp1);
Thread t2 = new Thread(comp2);
// moving child threads to runnable state
t1.start();
t2.start();
try {
Tester.newBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
// barrier breaks as the number of threads waiting for the barrier
// at this point = 3
System.out.println("Sum of product and sum = " + (Computation1.product + Computation2.sum));
// Resetting the newBarrier
newBarrier.reset();
System.out.println("Barrier reset successful");
}
}
Output:
Explanation: The value of (sum + product) = 0 is printed on the console because the child thread have not yet run to set the values of sum and product variable. Following this, (sum + product) = 36 is printed on the console because the child threads ran setting the values of sum and product. Furthermore, the number of waiting thread on the barrier reached 3, due to which the barrier then allowed all thread to pass and finally 36 was printed. The value of "Number of parties waiting at the barrier at this point" = 0 because all the three threads had already called await() method and hence, the barrier is no longer active. In the end, newBarrier is reset and can be used again.
BrokenBarrierException
A barrier breaks when any of the waiting thread leaves the barrier. This happens when one or more waiting thread is interrupted or when the waiting time is completed because the thread called the await() methods with a timeout as follows:
newBarrier.await(1000, TimeUnit.MILLISECONDS);
This method waits for a maximum of 1000 milliseconds. If the barrier is broken because one or more threads didn't reach it, all the other threads waiting at the barrier will get a BrokenBarrierException when they call await(). The threads that are already waiting will have their await() call stopped.
CyclicBarrier vs CountDownLatch
The table below demonstrates the difference between CyclicBarrier and a CountDownLatch
Features | CyclicBarrier | CountDownLatch |
---|
Reusability | It can be used multiple times. | It can only be used once. |
---|
Scenarios | Suitable for scenarios where you need to coordinate multiple phases of computation. | Suitable for scenarios where you need to wait for a set of events to complete just once. |
---|
Similar Reads
Multithreading in Java Multithreading is a Java feature that allows the concurrent execution of two or more parts of a program for maximum utilization of the CPU. Each part of such a program is called a thread. So, threads are lightweight processes within a process.Different Ways to Create ThreadsThreads can be created by
3 min read
Lifecycle and States of a Thread in Java A thread in Java can exist in any one of the following states at any given time. A thread lies only in one of the shown states at any instant:New StateRunnable StateBlocked StateWaiting StateTimed Waiting StateTerminated StateThe diagram below represents various states of a thread at any instant:Lif
5 min read
Main thread in Java Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.When a Java program starts up, one thread begins running i
4 min read
Java Concurrency - yield(), sleep() and join() Methods In this article, we will learn what is yield(), join(), and sleep() methods in Java and what is the basic difference between these three. First, we will see the basic introduction of all these three methods, and then we compare these three. We can prevent the execution of a thread by using one of th
5 min read
Inter-thread Communication in Java Inter-thread communication in Java is a mechanism in which a thread is paused from running in its critical section, and another thread is allowed to enter (or lock) the same critical section to be executed.Note: Inter-thread communication is also known as Cooperation in Java.What is Polling, and Wha
6 min read
Java Thread Class Thread is a line of execution within a program. Each program can have multiple associated threads. Each thread has a priority which is used by the thread scheduler to determine which thread must run first. Java provides a thread class that has various method calls to manage the behavior of threads b
5 min read
What does start() function do in multithreading in Java? We have discussed that Java threads are typically created using one of the two methods : (1) Extending thread class. (2) Implementing RunnableIn both the approaches, we override the run() function, but we start a thread by calling the start() function. So why don't we directly call the overridden ru
2 min read
Java Thread Priority in Multithreading Java being Object-Oriented works within a Multithreading environment in which the thread scheduler assigns the processor to a thread based on the priority of the thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can either be given by JVM while creatin
5 min read
Joining Threads in Java java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program. If th
3 min read
Java Naming a Thread and Fetching Name of Current Thread A thread can be referred to as a lightweight process. Assigning descriptive names to threads enhances code readability and simplifies debugging. Now let us discuss the different ways to name a thread in Java.Methods to Set the Thread NameThere are two ways by which we can set the name either be it d
4 min read