CyclicBarrier in Java

The java.util.concurrent package provides many synchronizers utilities that cover the use cases where several thread communicate with each other and some sort of synchronizer is needed to regulate that interaction among the threads. CyclicBarrier in Java is one such synchronization aid that is useful when you want threads to wait at a common execution point until all the threads in the set have reached that common barrier point.

CyclicBarrier class in Java

CyclicBarrier class was added in Java 1.5 and it is part of java.util.concurrent package in Java.
The class is named as CyclicBarrier because it can be reused after the waiting threads are released.

How CyclicBarrier works in Java

A CyclicBarrier is initialized with a given value and the barrier is tripped when the number of threads waiting at the barrier is equal to that value.
To make the thread wait at this barrier await() method is called upon the thread.
A thread calling the await() method signifies that the thread has reached the common barrier point and that thread is disabled until the required number of threads have invoked await() method at which point the barrier is tripped and the threads are unblocked. That way threads can synchronize their execution.

Java CyclicBarrier constructors

  • CyclicBarrier(int parties)– Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and does not perform a predefined action when the barrier is tripped.
  • CyclicBarrier(int parties, Runnable barrierAction)– Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and which will execute the given barrier action when the barrier is tripped, performed by the last thread entering the barrier.

Java CyclicBarrier methods

  1. await()– Waits until all parties have invoked await on this barrier.
  2. await(long timeout, TimeUnit unit)– Waits until all parties have invoked await on this barrier, or the specified waiting time elapses.
  3. getNumberWaiting()– Returns the number of parties currently waiting at the barrier.
  4. getParties()– Returns the number of parties required to trip this barrier.
  5. isBroken()– Queries if this barrier is in a broken state.
  6. reset()– Resets the barrier to its initial state.

CyclicBarrier Java example

In the example three threads are created and an ArrayList is shared among these threads. All of these threads process some data and put the results in the ArrayList. Further processing should start only after all the three threads have finished and called await().
For this scenario we’ll use a CyclicBarrier initialized with the value 3 and also a runnable action for further processing.

Output

What happens when await method is called

When the await() method is called upon the current thread and the current thread is not the last to arrive then it is disabled for thread scheduling purposes and lies dormant until one of the following things happens-

  • The last thread arrives; or
  • Some other thread interrupts the current thread; or
  • Some other thread interrupts one of the other waiting threads; or
  • Some other thread times out while waiting for barrier; or
  • Some other thread invokes reset() on this barrier.

If the current thread is the last thread to arrive then-

If a non-null barrier action was supplied in the constructor, then the current thread runs the action before allowing the other threads to continue. If an exception occurs during the barrier action then that exception will be propagated in the current thread and the barrier is placed in the broken state.

BrokenBarrierException in CyclicBarrier

If any thread is interrupted while waiting at a barrier point, then all other waiting threads will throw BrokenBarrierException and the barrier is placed in the broken state.

If the barrier is reset() while any thread is waiting then BrokenBarrierException is thrown.

CyclicBarrier is reusable

Unlike one of the other synchronization aid CountDownLatch which can’t be reused, CyclicBarrier in Java can be reused after the waiting threads are released.

Let’s reuse the same example as above but now the CyclicBarrier is initialized with the value 4 as we’ll wait for the main thread too. Three more threads will be started once the initial set of three threads are released from the barrier these new threads use the same CyclicBarrier object.

Output

That’s all for the topic CyclicBarrier in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.