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?
A
CyclicBarrier
is cyclic because it can be reused without resetting. From the JavadocSo 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()
So
reset
causes any currently waiting threads to throw aBrokenBarrierException
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.