Advanced Java October 06 ,2025

Custom Implementation of CyclicBarrier in Java

In multithreading, sometimes we need multiple threads to wait for each other to reach a common point before continuing execution.
This is where CyclicBarrier comes in handy.

A CyclicBarrier is a synchronization aid that allows a set of threads to wait for each other to reach a common barrier point.
It is part of the java.util.concurrent package.


Let’s create our own version of CyclicBarrier.

Step 1 — Plan the Logic

Our custom CyclicBarrier needs:

  • A count of threads to wait for (parties).
  • A counter to track threads that have arrived.
  • A mechanism to make threads wait (wait() / notifyAll()).
  • Barrier reset for reuse.

Step 2 — Create CustomCyclicBarrier Class

public class CustomCyclicBarrier {
    private final int parties;
    private int waiting = 0;

    public CustomCyclicBarrier(int parties) {
        this.parties = parties;
    }

    public synchronized void await() throws InterruptedException {
        waiting++;
        if (waiting < parties) {
            wait(); // thread waits here until all threads arrive
        } else {
            waiting = 0; // reset for reuse
            notifyAll(); // release all waiting threads
        }
    }
}

Step 3 — Test CustomCyclicBarrier

public class CustomCyclicBarrierTest {
    public static void main(String[] args) {
        int parties = 3;
        CustomCyclicBarrier barrier = new CustomCyclicBarrier(parties);

        Runnable task = () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " is doing work...");
                Thread.sleep((long) (Math.random() * 3000));
                System.out.println(Thread.currentThread().getName() + " reached barrier");
                barrier.await();
                System.out.println(Thread.currentThread().getName() + " passed the barrier");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < parties; i++) {
            new Thread(task).start();
        }
    }
}

Expected Output

Thread-0 is doing work...
Thread-1 is doing work...
Thread-2 is doing work...
Thread-1 reached barrier
Thread-0 reached barrier
Thread-2 reached barrier
Thread-1 passed the barrier
Thread-0 passed the barrier
Thread-2 passed the barrier

Step 4 — Add Barrier Action (Optional)

We can extend our custom barrier to execute a task once all threads arrive.

public class CustomCyclicBarrier {
    private final int parties;
    private int waiting = 0;
    private Runnable barrierAction;

    public CustomCyclicBarrier(int parties, Runnable barrierAction) {
        this.parties = parties;
        this.barrierAction = barrierAction;
    }

    public synchronized void await() throws InterruptedException {
        waiting++;
        if (waiting < parties) {
            wait();
        } else {
            if (barrierAction != null) barrierAction.run();
            waiting = 0;
            notifyAll();
        }
    }
}

Test with Barrier Action

public class CustomCyclicBarrierTest {
    public static void main(String[] args) {
        int parties = 3;
        Runnable barrierAction = () -> System.out.println("All threads reached the barrier!");

        CustomCyclicBarrier barrier = new CustomCyclicBarrier(parties, barrierAction);

        Runnable task = () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " is doing work...");
                Thread.sleep((long) (Math.random() * 3000));
                System.out.println(Thread.currentThread().getName() + " reached barrier");
                barrier.await();
                System.out.println(Thread.currentThread().getName() + " passed the barrier");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < parties; i++) {
            new Thread(task).start();
        }
    }
}

Expected Output:

Thread-0 is doing work...
Thread-1 is doing work...
Thread-2 is doing work...
Thread-1 reached barrier
Thread-0 reached barrier
Thread-2 reached barrier
All threads reached the barrier!
Thread-1 passed the barrier
Thread-0 passed the barrier
Thread-2 passed the barrier

 

Complete CustomCyclicBarrier Implementation

// CustomCyclicBarrier.java
public class CustomCyclicBarrier {
    private final int parties;
    private int waiting = 0;
    private final Runnable barrierAction;

    public CustomCyclicBarrier(int parties) {
        this(parties, null);
    }

    public CustomCyclicBarrier(int parties, Runnable barrierAction) {
        this.parties = parties;
        this.barrierAction = barrierAction;
    }

    public synchronized void await() throws InterruptedException {
        waiting++;
        if (waiting < parties) {
            wait(); // thread waits until all threads arrive
        } else {
            if (barrierAction != null) {
                barrierAction.run(); // execute barrier action
            }
            waiting = 0; // reset barrier for reuse
            notifyAll(); // release all waiting threads
        }
    }
}

Test Code for CustomCyclicBarrier

// CustomCyclicBarrierTest.java
public class CustomCyclicBarrierTest {
    public static void main(String[] args) {
        int parties = 3;

        // Optional barrier action
        Runnable barrierAction = () -> System.out.println("All threads reached the barrier!");

        CustomCyclicBarrier barrier = new CustomCyclicBarrier(parties, barrierAction);

        Runnable task = () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " is doing work...");
                Thread.sleep((long) (Math.random() * 3000)); // simulate work
                System.out.println(Thread.currentThread().getName() + " reached barrier");
                barrier.await();
                System.out.println(Thread.currentThread().getName() + " passed the barrier");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < parties; i++) {
            new Thread(task).start();
        }
    }
}

Expected Output

Thread-0 is doing work...
Thread-1 is doing work...
Thread-2 is doing work...
Thread-1 reached barrier
Thread-0 reached barrier
Thread-2 reached barrier
All threads reached the barrier!
Thread-1 passed the barrier
Thread-0 passed the barrier
Thread-2 passed the barrier

 

Advantages of CustomCyclicBarrier

  • Helps understand how synchronization works.
  • Useful learning exercise for low-level concurrency.
  • Can be tailored for specific needs.

 Disadvantages

  • More error-prone than using Java’s built-in CyclicBarrier.
  • Lacks optimizations such as thread fairness and interruption handling.
  • No built-in safety against deadlocks.

 Real-World Use Case

CustomCyclicBarrier can be used for:

  • Simulating teamwork in games or simulations.
  • Coordinating tasks in scientific computations.
  • Parallel processing with synchronization points.

Conclusion

The custom implementation of CyclicBarrier shows:

  • How synchronization works internally.
  • How threads can coordinate with low-level locking mechanisms.
  • That Java’s built-in CyclicBarrier already handles complexity like reusability, fairness, and barrier actions efficiently.

For production-grade systems, always prefer Java’s built-in CyclicBarrier, but for understanding concurrency deeply, building a custom version is invaluable.

 

Next Blog- Custom Implementation of CountDownLatch in Java

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Multi...
Advanced Java August 08 ,2025

Java Multithreading...

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Semaphore...
Advanced Java October 10 ,2025

Semaphore in Java

ExecutorSe...
Advanced Java October 10 ,2025

ExecutorService in J...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Producer-C...
Advanced Java October 10 ,2025

Producer-Consumer Pr...

Implementi...
Advanced Java October 10 ,2025

Implementing a Custo...

Busy Spin
Advanced Java October 10 ,2025

Busy Spin

Serializat...
Advanced Java October 10 ,2025

Serialization and De...

Segment Lo...
Advanced Java October 10 ,2025

Segment Locking in J...

Tree Bins...
Advanced Java October 10 ,2025

Tree Bins in Java

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com