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.
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
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.
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:
Example of Cyclic Barrier
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
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.
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