|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjsr166y.forkjoin.TaskBarrier
public class TaskBarrier
A synchronization barrier for ForkJoinTasks. A TaskBarrier is
similar in functionality to a CyclicBarrier
, but differs in the following
ways.
A TaskBarrier may be used to support a style of programming in which a task waits for others to complete, without otherwise needing to keep track of which tasks it is waiting for. This is similar to the "sync" construct in Cilk and "clocks" in X10. Special constructions based on such barriers are available using the LinkedAsyncAction and CyclicAction classes, but they can be useful in other contexts as well. For a simple (but not very useful) example, here is a variant of Fibonacci:
class BarrierFibonacci extends RecursiveAction { int argument, result; final TaskBarrier parentBarrier; BarrierFibonacci(int n, TaskBarrier parentBarrier) { this.argument = n; this.parentBarrier = parentBarrier; parentBarrier.register(); } protected void compute() { int n = argument; if (n <= 1) result = n; else { TaskBarrier childBarrier = new TaskBarrier(1); BarrierFibonacci f1 = new BarrierFibonacci(n - 1, childBarrier); BarrierFibonacci f2 = new BarrierFibonacci(n - 2, childBarrier); f1.fork(); f2.fork(); childBarrier.arriveAndAwait(); result = f1.result + f2.result; } parentBarrier.arriveAndDeregister(); } }
Implementation notes: This implementation restricts the maximum number of registered parties to 65535. Attempts to register additional parties result in IllegalStateExceptions.
Constructor Summary | |
---|---|
TaskBarrier()
Creates a new barrier without any initially registered parties. |
|
TaskBarrier(int parties)
Creates a new barrier with the given numbers of registered active parties. |
Method Summary | |
---|---|
int |
arrive()
Arrives at the barrier, but does not wait for others. |
int |
arriveAndAwait()
Arrives at the barrier and awaits others. |
int |
arriveAndDeregister()
Arrives at the barrier, and deregisters from it. |
int |
awaitCycleAdvance(int cycle)
Awaits the cycle of the barrier to advance from the given value, by helping other tasks. |
int |
getActiveParties()
Returns the number of parties that have not yet arrived at the current cycle of this barrier. |
int |
getCycle()
Returns the current cycle number. |
int |
getRegisteredParties()
Returns the number of parties registered at this barrier. |
boolean |
isTerminated()
Returns true if this barrier has been terminated |
int |
register()
Adds a new active party to the barrier. |
protected boolean |
terminate(int cycle,
int registeredParties)
Overridable method to control termination. |
java.lang.String |
toString()
Returns a string identifying this barrier, as well as its state. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public TaskBarrier()
public TaskBarrier(int parties)
parties
- the number of parties required to trip barrier.
java.lang.IllegalArgumentException
- if parties less than zero
or greater than the maximum number of parties supported.Method Detail |
---|
public int register()
public int arrive()
awaitCycleAdvance(int)
).
public int arriveAndDeregister()
public int awaitCycleAdvance(int cycle)
cycle
- the cycle on entry to this method
public int arriveAndAwait()
if (b.arriveAndAwait()== 0) action(); b.arriveAndAwait();
public int getCycle()
public int getRegisteredParties()
public int getActiveParties()
public boolean isTerminated()
protected boolean terminate(int cycle, int registeredParties)
The default version returns true only when the number of registered parties is zero.
cycle
- the cycle count on entering the barrierregisteredParties
- the current number of registered
parties.public java.lang.String toString()
"Cycle ="
followed by the cycle number, "parties ="
followed by the number of registered parties, and "acivie ="
followed by the number of active parties
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |