Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to reset CyclicBarrier in java multithreading

I was reading CyclicBarrier in the following link http://java-latte.blogspot.in/2013/10/cyclicbarrier-in-java-concurrency.html.

In the example 1, CyclicRaceDemo.java main method, CyclicBarrier is being reused without calling reset method.

I ran the example and it worked fine. So, I am wondering what's the use of reset method. When should it be called? Or do we need to call it at all?

like image 304
Anand Avatar asked Jun 08 '14 09:06

Anand


People also ask

Can CyclicBarrier in Java be reused?

A CyclicBarrier is a reusable construct where a group of threads waits together until all of the threads arrive. At that point, the barrier is broken and an action can optionally be taken. We can think of this like a group of friends.

Under what circumstances can the await method of CyclicBarrier throw exception?

If the barrier is reset() while any thread is waiting, or if the barrier is broken when await is invoked, or while any thread is waiting, then BrokenBarrierException is thrown.

Can CyclicBarrier and CountDownLatch in Java be reused?

CyclicBarrier is similar to CountDownLatch and allows multiple threads to wait for each other (barrier) before proceeding. But there is the difference: we cannot reuse CountDownLatch once the count reaches zero while we can reuse CyclicBarrier by calling reset() method which resets Barrier to its initial State.

What is CountDownLatch and CyclicBarrier in multithreading?

CountDownLatch − A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. CyclicBarrier − A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.


1 Answers

A CyclicBarrier is cyclic because it can be reused without resetting. From the Javadoc

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.

So in normal usage, once all the threads are collected and the barrier is broken, it resets itself and can be used again.

From the Javadoc for reset()

Resets the barrier to its initial state. If any parties are currently waiting at the barrier, they will return with a BrokenBarrierException. Note that resets after a breakage has occurred for other reasons can be complicated to carry out; threads need to re-synchronize in some other way, and choose one to perform the reset. It may be preferable to instead create a new barrier for subsequent use.

So reset causes any currently waiting threads to throw a BrokenBarrierException and wake immediately. reset is used when you want to "break" the barrier.

Note also the caveat - once the threads have been awoken forcibly it's tricky to synchronize them again.

TL;DR: you should never need to use reset() in normal circumstances.

like image 72
Boris the Spider Avatar answered Oct 02 '22 13:10

Boris the Spider