jsr166z.forkjoin
Class CyclicAction

java.lang.Object
  extended by jsr166z.forkjoin.ForkJoinTask<java.lang.Void>
      extended by jsr166z.forkjoin.CyclicAction

public abstract class CyclicAction
extends ForkJoinTask<java.lang.Void>

A computation that is broken into a series of task executions, each separated by a TaskBarrier arrival. Concrete subclasses must define method compute, that performs the action occurring at each step of the barrier. Upon invocation of this task, the compute method is repeatedly invoked until the barrier isTerminated or until its execution throws an exception.

Sample Usage. Here is a sketch of a set of CyclicActions that each perform 500 iterations of an imagined image smoothing operation. Note that the aggregate ImageSmoother task itself is not a CyclicTask.

 class ImageSmoother extends RecursiveAction {
   protected void compute() {
     TaskBarrier b = new TaskBarrier() {
       protected boolean terminate(int cycle, int registeredParties) {
          return registeredParties <= 0 || cycle >= 500;
       }
     }
     int n = pool.getPoolSize();
     CyclicAction[] actions = new CyclicAction[n];
     for (int i = 0; i < n; ++i) {
       action[i] = new CyclicAction(b) {
         protected void compute() {
           smoothImagePart(i);
         }
       }
     }
     for (int i = 0; i < n; ++i) 
       actions[i].fork();
     for (int i = 0; i < n; ++i) 
       actions[i].join();
   }
 }
 


Constructor Summary
CyclicAction(TaskBarrier barrier)
          Constructs a new CyclicAction using the supplied barrier, registering for this barrier upon construction.
 
Method Summary
protected abstract  void compute()
          The computation performed by this task on each cycle of the barrier.
 java.lang.Throwable exec()
          Immediately commences execution of this task by the current worker thread unless already cancelled, returning any exception thrown by its compute method.
 void finish()
          Equivalent to finish(null).
 void finish(java.lang.Void result)
          Completes this task, and if not already aborted or cancelled, returning the given result upon join and related operations.
 void finishExceptionally(java.lang.Throwable ex)
          Completes this task abnormally, and if not already aborted or cancelled, causes it to throw the given exception upon join and related operations.
 TaskBarrier getBarrier()
          Returns the barrier
 int getCycle()
          Returns the current cycle of the barrier
 java.lang.Void invoke()
          Equivalent in effect to the sequence fork(); join(); but may be more efficient.
 java.lang.Void rawResult()
          Always returns null.
 
Methods inherited from class jsr166z.forkjoin.ForkJoinTask
cancel, fork, getException, isCancelled, isDone, isStolen, join, quietlyJoin, reinitialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CyclicAction

public CyclicAction(TaskBarrier barrier)
Constructs a new CyclicAction using the supplied barrier, registering for this barrier upon construction.

Parameters:
barrier - the barrier
Method Detail

compute

protected abstract void compute()
The computation performed by this task on each cycle of the barrier. While you must define this method, you should not in general call it directly.


getBarrier

public final TaskBarrier getBarrier()
Returns the barrier


getCycle

public final int getCycle()
Returns the current cycle of the barrier


rawResult

public final java.lang.Void rawResult()
Always returns null.

Specified by:
rawResult in class ForkJoinTask<java.lang.Void>
Returns:
null

exec

public final java.lang.Throwable exec()
Description copied from class: ForkJoinTask
Immediately commences execution of this task by the current worker thread unless already cancelled, returning any exception thrown by its compute method.

Specified by:
exec in class ForkJoinTask<java.lang.Void>
Returns:
exception thrown by compute (or via cancellation), or null if none

invoke

public final java.lang.Void invoke()
Description copied from class: ForkJoinTask
Equivalent in effect to the sequence fork(); join(); but may be more efficient.

Specified by:
invoke in class ForkJoinTask<java.lang.Void>
Returns:
the computed result

finish

public final void finish()
Equivalent to finish(null).


finish

public final void finish(java.lang.Void result)
Description copied from class: ForkJoinTask
Completes this task, and if not already aborted or cancelled, returning the given result upon join and related operations.

Specified by:
finish in class ForkJoinTask<java.lang.Void>
Parameters:
result - the result to return

finishExceptionally

public final void finishExceptionally(java.lang.Throwable ex)
Description copied from class: ForkJoinTask
Completes this task abnormally, and if not already aborted or cancelled, causes it to throw the given exception upon join and related operations.

Specified by:
finishExceptionally in class ForkJoinTask<java.lang.Void>
Parameters:
ex - the exception to throw. While not necessarily statically enforced, this must be a RuntimeException or Error.