User Tools

Site Tools


Cyclic Barrier Example in Java Threads

The CyclicBarrier in Java is a synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. It is useful in scenarios where you have a parallel task that can be divided into smaller, independent tasks, and you need to wait for all the smaller tasks to complete before proceeding.
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.BrokenBarrierException;
public class CyclicBarrierExample {
    // Runnable task that will be executed by each thread
    static class Task implements Runnable {
        private CyclicBarrier barrier;
        public Task(CyclicBarrier barrier) {
            this.barrier = barrier;
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " is waiting at the barrier.");
                System.out.println(Thread.currentThread().getName() + " has crossed the barrier.");
            } catch (InterruptedException | BrokenBarrierException e) {
    public static void main(String[] args) {
        final int numberOfThreads = 3;
        CyclicBarrier barrier = new CyclicBarrier(numberOfThreads, new Runnable() {
            public void run() {
                // This task will be executed once all threads reach the barrier
                System.out.println("All threads have reached the barrier, now proceeding further.");
        for (int i = 0; i < numberOfThreads; i++) {
            Thread thread = new Thread(new Task(barrier), "Thread " + (i + 1));

CyclicBarrier Creation: A CyclicBarrier is created with a specified number of threads (numberOfThreads). An optional action can be provided that executes once all the threads have reached the barrier. Here, a simple message is printed.

Task Class: This inner class Task implements Runnable and takes a CyclicBarrier in its constructor. Each thread executing this task will wait at the barrier.await() call until all threads have reached this point.

Awaiting at the Barrier: When each thread calls await() on the CyclicBarrier, it waits until all other threads have reached the barrier. Once the last thread calls await(), the optional action is executed (if provided), and then all waiting threads are released to continue.

Creating and Starting Threads: The main method creates and starts the specified number of threads. Each thread executes the Task which involves waiting at the CyclicBarrier.

This example demonstrates the use of CyclicBarrier to synchronize multiple threads at a common point. It is particularly useful in parallel algorithms where a process is divided into smaller independent tasks, and you need to wait for all tasks to complete a phase before proceeding to the next phase.

java/threads/cyclic-barrier-example.txt · Last modified: 2024/01/17 02:44 by odefta