|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.terracotta.coordination.CyclicBarrier
public class CyclicBarrier
A cyclic barrier is a reasonable choice for a barrier in contexts involving a fixed sized group of threads that must occasionally wait for each other. (A Rendezvous better handles applications in which any number of threads meet, n-at-a-time.)
CyclicBarriers use an all-or-none breakage model for failed synchronization attempts: If threads leave a barrier
point prematurely because of timeout or interruption, others will also leave abnormally (via BrokenBarrierException),
until the barrier is restarted. This is usually the simplest and best strategy for sharing knowledge
about failures among cooperating threads in the most common usages contexts of Barriers. This implementation has the
property that interruptions among newly arriving threads can cause as-yet-unresumed threads from a previous barrier
cycle to return out as broken. This transmits breakage as early as possible, but with the possible byproduct that
only some threads returning out of a barrier will realize that it is newly broken. (Others will not realize this
until a future cycle.) (The Rendezvous class has a more uniform, but sometimes less desirable policy.)
Barriers support an optional Runnable command that is run once per barrier point.
| Field Summary | |
|---|---|
protected Runnable |
barrierCommand_
|
protected boolean |
broken_
|
protected int |
count_
|
protected int |
parties_
|
protected int |
resets_
|
| Constructor Summary | |
|---|---|
CyclicBarrier(int parties)
Create a CyclicBarrier for the indicated number of parties, and no command to run at each barrier. |
|
CyclicBarrier(int parties,
Runnable command)
Create a CyclicBarrier for the indicated number of parties. |
|
| Method Summary | |
|---|---|
int |
await()
Enter barrier and wait for the other parties()-1 threads. |
int |
await(long msecs)
Enter barrier and wait at most msecs for the other parties()-1 threads. |
protected int |
doAwait(boolean timed,
long msecs)
|
int |
getParties()
Return the number of parties that must meet per barrier point. |
boolean |
isBroken()
Returns true if the barrier has been compromised by threads leaving the barrier before a synchronization point (normally due to interruption or timeout). |
void |
reset()
Reset to initial state. |
Runnable |
setBarrierCommand(Runnable command)
Set the command to run at the point at which all threads reach the barrier. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected final int parties_
protected boolean broken_
protected Runnable barrierCommand_
protected int count_
protected int resets_
| Constructor Detail |
|---|
public CyclicBarrier(int parties)
IllegalArgumentException - if parties less than or equal to zero.
public CyclicBarrier(int parties,
Runnable command)
IllegalArgumentException - if parties less than or equal to zero.| Method Detail |
|---|
public Runnable setBarrierCommand(Runnable command)
command - the command to run. If null, no command is run.
public boolean isBroken()
Barrier
isBroken in interface Barrierpublic void reset()
Barrier
reset in interface Barrierpublic int getParties()
Barrier
getParties in interface Barrier
public int await()
throws InterruptedException,
BrokenBarrierException
Barrier
await in interface BarrierInterruptedException - if this thread was interrupted during the barrier, and was the one causing
breakage. If so, broken status is also set.
BrokenBarrierException - if any other thread in any previous or current barrier since either creation or
the last restart operation left the barrier prematurely due to interruption or time-out.
(If so, the broken status is also set.) Threads that are notified to have been interrupted
after being released are not considered to have broken the barrier. In all cases, the
interruption status of the current thread is preserved, so can be tested by checking
Thread.interrupted.
public int await(long msecs)
throws InterruptedException,
TimeoutException,
BrokenBarrierException
Barrier
await in interface BarrierInterruptedException - if this thread was interrupted during the barrier. If so, broken
status is also set.
TimeoutException - if this thread timed out waiting for the barrier. If the timeout occured while already
in the barrier, broken status is also set.
BrokenBarrierException - if any other thread in any previous or current barrier since either creation or
the last restart operation left the barrier prematurely due to interruption or time-out.
(If so, the broken status is also set.) Threads that are noticed to have been interrupted
after being released are not considered to have broken the barrier. In all cases, the
interruption status of the current thread is preserved, so can be tested by checking
Thread.interrupted.
protected int doAwait(boolean timed,
long msecs)
throws InterruptedException,
TimeoutException,
BrokenBarrierException
InterruptedException
TimeoutException
BrokenBarrierException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||