0
0
JavaConceptBeginner · 3 min read

What is CyclicBarrier in Java: Explanation and Example

CyclicBarrier in Java is a synchronization aid that allows a set of threads to wait for each other to reach a common barrier point before continuing. It is useful when you want multiple threads to pause and then proceed together repeatedly.
⚙️

How It Works

Imagine a group of friends planning to meet at a restaurant. Each friend travels separately but agrees to wait until everyone arrives before entering together. CyclicBarrier works similarly for threads: it makes threads wait at a barrier point until all threads reach it.

Once all threads have arrived, the barrier opens, and all threads continue their work simultaneously. The "cyclic" part means this waiting and releasing can happen repeatedly, like meeting friends multiple times.

💻

Example

This example shows three threads waiting at a CyclicBarrier. When all three reach the barrier, they print a message and continue.

java
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierExample {
    public static void main(String[] args) {
        final int parties = 3;
        CyclicBarrier barrier = new CyclicBarrier(parties, () -> System.out.println("All threads reached the barrier. Let's proceed!"));

        Runnable task = () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " is waiting at the barrier.");
                barrier.await();
                System.out.println(Thread.currentThread().getName() + " has crossed the barrier.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < parties; i++) {
            new Thread(task, "Thread-" + (i + 1)).start();
        }
    }
}
Output
Thread-1 is waiting at the barrier. Thread-2 is waiting at the barrier. Thread-3 is waiting at the barrier. All threads reached the barrier. Let's proceed! Thread-3 has crossed the barrier. Thread-1 has crossed the barrier. Thread-2 has crossed the barrier.
🎯

When to Use

Use CyclicBarrier when you have multiple threads that must wait for each other at certain points before continuing. This is common in parallel computations where results from all threads are needed before moving on.

For example, in a game, you might want all players to finish their turn before starting the next round. Or in data processing, threads might process parts of data and wait to combine results together.

Key Points

  • CyclicBarrier lets threads wait for each other at a barrier point.
  • The barrier can be reused multiple times (cyclic).
  • It can run a task when all threads reach the barrier.
  • Useful for coordinating phases in parallel tasks.

Key Takeaways

CyclicBarrier makes threads wait until all reach a common point before continuing.
It can be reused multiple times for repeated synchronization.
You can specify a task to run once all threads arrive at the barrier.
Ideal for coordinating multiple threads in phases or steps.
Helps ensure threads proceed together after completing parts of work.