public class CyclicBarrier(循环屏障) extends Object
A CyclicBarrier supports an optional(可选) Runnable
command(命令) that is run once per barrier point(每个障碍点), after the last thread in the party arrives, but before any threads are released. This barrier action is useful for updating shared-state before any of the parties(各方) continue.
Sample usage: Here is an example of using a barrier in a parallel(并行) decomposition(分解) design:
Runnable
barrier action is executed and merges the rows. If the merger determines that a solution has been found then
done() will return
true and each worker will terminate.
If the barrier action does not rely on the parties being suspended when it is executed, then any of the threads in the party could execute that action when it is released. To facilitate this, each invocation of await()
returns the arrival index of that thread at the barrier. You can then choose which thread should execute the barrier action, for example:
if (barrier.await() == 0) { // log the completion of this iteration }
The CyclicBarrier uses an all-or-none breakage model for failed synchronization attempts: If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads waiting at that barrier point will also leave abnormally via BrokenBarrierException
(or InterruptedException
if they too were interrupted at about the same time).
Memory consistency effects: Actions in a thread prior to calling await()
happen-before actions that are part of the barrier action, which in turn happen-before actions following a successful return from the correspondingawait()
in other threads.
-
Since:
- 1.5 See Also:
-
CountDownLatch