|
|||||||||
| Home >> All >> org >> ematgine >> utils >> [ concurrent overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
org.ematgine.utils.concurrent
Class CyclicBarrier

java.lang.Objectorg.ematgine.utils.concurrent.CyclicBarrier
- All Implemented Interfaces:
- Barrier
- public class CyclicBarrier
- extends java.lang.Object
- implements Barrier
- extends java.lang.Object
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.
Sample usage Here is a code sketch of a barrier in a parallel decomposition design.
class Solver {
final int N;
final float[][] data;
final CyclicBarrier barrier;
class Worker implements Runnable {
int myRow;
Worker(int row) { myRow = row; }
public void run() {
while (!done()) {
processRow(myRow);
try {
barrier.barrier();
}
catch (InterruptedException ex) { return; }
catch (BrokenBarrierException ex) { return; }
}
}
}
public Solver(float[][] matrix) {
data = matrix;
N = matrix.length;
barrier = new CyclicBarrier(N);
barrier.setBarrierCommand(new Runnable() {
public void run() { mergeRows(...); }
});
for (int i = 0; i < N; ++i) {
new Thread(new Worker(i)).start();
waitUntilDone();
}
}
[ Introduction to this package. ]
| Field Summary | |
protected java.lang.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,
java.lang.Runnable command)
Create a CyclicBarrier for the indicated number of parties. |
|
| Method Summary | |
int |
attemptBarrier(long msecs)
Enter barrier and wait at most msecs for the other parties()-1 threads. |
int |
barrier()
Enter barrier and wait for the other parties()-1 threads. |
boolean |
broken()
Returns true if the barrier has been compromised by threads leaving the barrier before a synchronization point (normally due to interruption or timeout). |
protected int |
doBarrier(boolean timed,
long msecs)
|
int |
parties()
Return the number of parties that must meet per barrier point. |
void |
restart()
Reset to initial state. |
java.lang.Runnable |
setBarrierCommand(java.lang.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 |
parties_
protected final int parties_
broken_
protected boolean broken_
barrierCommand_
protected java.lang.Runnable barrierCommand_
count_
protected int count_
resets_
protected int resets_
| Constructor Detail |
CyclicBarrier
public CyclicBarrier(int parties)
- Create a CyclicBarrier for the indicated number of parties,
and no command to run at each barrier.
CyclicBarrier
public CyclicBarrier(int parties,
java.lang.Runnable command)
- Create a CyclicBarrier for the indicated number of parties.
and the given command to run at each barrier point.
| Method Detail |
setBarrierCommand
public java.lang.Runnable setBarrierCommand(java.lang.Runnable command)
- Set the command to run at the point at which all threads reach the
barrier. This command is run exactly once, by the thread
that trips the barrier. The command is not run if the barrier is
broken.
broken
public boolean broken()
- Description copied from interface:
Barrier - Returns true if the barrier has been compromised
by threads leaving the barrier before a synchronization
point (normally due to interruption or timeout).
Barrier methods in implementation classes throw
throw BrokenBarrierException upon detection of breakage.
Implementations may also support some means
to clear this status.
restart
public void restart()
- Reset to initial state. Clears both the broken status
and any record of waiting threads, and releases all
currently waiting threads with indeterminate return status.
This method is intended only for use in recovery actions
in which it is somehow known
that no thread could possibly be relying on the
the synchronization properties of this barrier.
parties
public int parties()
- Description copied from interface:
Barrier - Return the number of parties that must meet per barrier
point. The number of parties is always at least 1.
barrier
public int barrier()
throws java.lang.InterruptedException,
BrokenBarrierException
- Enter barrier and wait for the other parties()-1 threads.
attemptBarrier
public int attemptBarrier(long msecs)
throws java.lang.InterruptedException,
TimeoutException,
BrokenBarrierException
- Enter barrier and wait at most msecs for the other parties()-1 threads.
doBarrier
protected int doBarrier(boolean timed,
long msecs)
throws java.lang.InterruptedException,
TimeoutException,
BrokenBarrierException
|
|||||||||
| Home >> All >> org >> ematgine >> utils >> [ concurrent overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
JAVADOC
org.ematgine.utils.concurrent.CyclicBarrier