Thursday, March 5, 2026

What is CyclicBarrier in Java?

In multithreaded programming, there are situations where multiple threads must wait for each other to reach a common point before continuing execution.

Java provides a synchronization utility called CyclicBarrier to handle this requirement.

CyclicBarrier is part of the java.util.concurrent package and is used to allow a group of threads to wait for each other at a barrier point before proceeding further.





Definition

A CyclicBarrier is a synchronization mechanism that allows multiple threads to wait until all threads reach a common barrier point.

Once all threads reach the barrier, they are released simultaneously to continue execution.

The word "cyclic" means the barrier can be reused multiple times.


How CyclicBarrier Works

The process works like this:

1️⃣ A CyclicBarrier is created with a number of threads.
2️⃣ Each thread performs some work.
3️⃣ Each thread calls await() when it reaches the barrier.
4️⃣ When all threads reach the barrier, they are released together.


Important Methods of CyclicBarrier

1. await()

This method makes the thread wait until all threads reach the barrier.

barrier.await();

2. getNumberWaiting()

Returns the number of threads currently waiting at the barrier.

barrier.getNumberWaiting();

3. reset()

Resets the barrier so it can be used again.

barrier.reset();

Example of CyclicBarrier

import java.util.concurrent.CyclicBarrier;

class Worker extends Thread {

    CyclicBarrier barrier;

    Worker(CyclicBarrier barrier){
        this.barrier = barrier;
    }

    public void run(){
        try {
            System.out.println(Thread.currentThread().getName() + " is waiting at barrier");
            barrier.await();
            System.out.println(Thread.currentThread().getName() + " crossed the barrier");
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

public class CyclicBarrierExample {

    public static void main(String[] args) {

        CyclicBarrier barrier = new CyclicBarrier(3);

        new Worker(barrier).start();
        new Worker(barrier).start();
        new Worker(barrier).start();
    }
}

Example Output

Thread-1 is waiting at barrier
Thread-2 is waiting at barrier
Thread-3 is waiting at barrier
Thread-1 crossed the barrier
Thread-2 crossed the barrier
Thread-3 crossed the barrier

All threads wait until the third thread reaches the barrier, and then they continue together.


Real-World Use Cases

CyclicBarrier is useful in scenarios like:

  • Parallel data processing

  • Multiplayer game synchronization

  • Scientific simulations

  • Batch processing tasks

  • Multi-stage processing pipelines


Difference Between CountDownLatch and CyclicBarrier




Key Points to Remember

  • Introduced in Java 5

  • Part of java.util.concurrent

  • Allows multiple threads to wait for each other

  • Barrier can be reused

  • Useful for parallel task coordination


🚀 Master System Design and Advanced Java

Concepts like CyclicBarrier, CountDownLatch, ExecutorService, multithreading, and concurrency utilities are very important for building high-performance Java applications.

If you want to master these advanced concepts with real-time projects and industry examples, explore:

👉 Top System Design with Java Online Training

In this training program you will learn:

  • Advanced Java Concurrency

  • System Design Concepts

  • Scalable Application Architecture

  • Microservices Design

  • High Performance Backend Development

  • Real-Time Java Projects


No comments:

Post a Comment

To build frictionless production-ready Java applications in 2026, developers must move beyond traditional coding styles and adopt modern pra...