Javatpoint Logo
Javatpoint Logo

Cyclic Barrier in Java

In programming, executing more than one thread at the same time is a difficult task. To this task easier, Java introduces the concept of concurrency that allows us to run multiple threads at the same time. An advantage of concurrent programming is that if any of the threads fail to perform its Job, it will not interrupt other threads. In concurrent programming, the cyclic barrier is a concept that is used for the synchronization of threads. In this section, we will discuss what is a cyclic barrier and CyclicBarrier class in Java.

What is the cyclic barrier in Java?

The cyclic barrier is a concurrent utility mechanism of synchronization that allows a set of threads to all wait for each other to reach a common barrier point. It is useful in Java programs that involve a fixed-sized party of threads that must wait for each other. The word cyclic is used for its reused purpose. It means that the barrier can be re-used when all waiting threads are released. The point is the barrier that ensures all the threads complete their execution before reaching here. Therefore, the mechanism is known as the cyclic barrier. In short, cyclic barrier preserves a count of threads.

Cyclic Barrier in Java

Note that we can use CyclicBarrier instead of CountDownLatch but vice-versa is not possible because the latch cannot be reused when the count reaches zero.

Advantages of CyclicBarrier

  • It can be reset and rerun repeatedly.
  • We should break the barrier when the barrier is met.
  • It provides a post barrier routine, optionally.
  • It also provides a kind of split-merge.

When to use cyclic barrier?

Consider a scenario, in which an application needs to read five files using five threads. Parse the read lines and only after reading and parsing all the five files, the application must call another thread for further processing. In this case, we can use CyclicBarrier that provides a runnable action to execute the thread once all the threads reach the barrier.

  • It can be used to break a large task into smaller sub-task in which each task is performed by a separate thread. After performing the same, the result of all the threads is combined in the final task of the barrier.
  • It is also used in a multi-player game where the game starts when all the players have reached.

How to use cyclic barrier?

In order to use the cyclic barrier, first, we should create an object of the CyclicBarrier class by using any of the two constructors. In the constructor, parse the required number of threads that will wait for each other. When the specified number of threads reaches at a common point, invoke the await() method on the CyclicBarrier object. The await() method suspends the threads until all the threads invoke the await() method on the same CyclicBarrier object. When all the threads invoked the await() method, then the barrier is tripped and all the threads continue their operation after tripping.

If the current thread is the last thread to arrive, and a non-null barrier action was supplied in the constructor, then the current thread runs the action before allowing the other threads to continue.

Java CyclicBarrier Class

In Java, CyclicBarrier is a class that belongs to java.util.concurrent package. It was introduced in Java 5 along with other Java classes like CountDownLatch, ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue. The class allows us to maintain the count of threads.

Methods of the CyclicBarrier Class

The CyclicBarrier class has the following methods:

Method Description
await() It returns the arrival index of the current thread.
await(long timeout, TimeUnit unit) Waits until all parties (threads) have invoked await on this barrier, or the specified waiting time elapses.
getNumberWaiting() The method is used to return the number of parties currently waiting at the barrier (blocked in await() method). The method is widely used for debugging and assertions.
getParties() The method returns an integer value that denotes the number of parties required to trip this barrier.
isBroken() The method returns a boolean value if one or more parties broke out of this barrier due to interruption or timeout since construction or the last reset or a barrier action failed due to an exception. Else, returns false.
reset() The method is used to reset the barrier to its initial state. The method throws BrokenBarrierException if any parties are currently waiting at the barrier.

Example of Cyclic Barrier

CyclicBarrierDemo.java

Output:

Done 
Reading file file-1 thread thread-1
Reading file file-3 thread thread-3
Reading file file-2 thread thread-2
In after action class, start further processing as all files are read

Reusing CyclicBarrier

As we have discussed above that CyclicBarrier can be reused when all the waiting threads are released. Let's understand the reusability of CyclicBarrier through a Java program.

In the above program, we have used three threads to read three files. In the following program, we will add three more threads to read three more files.

ReuseCyclicBarrier.java

Output:

Start another set of threads 
Reading file demo6 thread T-6
Reading file demo2 thread T-2
Reading file demo5 thread T-5
In after action class, start further processing as all files are read
Reading file demo3 thread T-3
Reading file demo1 thread T-1
Reading file demo4 thread T-4
In after action class, start further processing as all files are read

Next TopicJava CyclicBarrier





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA