--- jsr166/src/jsr166y/RecursiveAction.java 2009/01/06 14:30:31 1.1 +++ jsr166/src/jsr166y/RecursiveAction.java 2009/07/22 01:36:51 1.6 @@ -8,57 +8,54 @@ package jsr166y; /** * Recursive resultless ForkJoinTasks. This class establishes - * conventions to parameterize resultless actions as Void - * ForkJoinTasks. Because null is the only valid value of - * Void, methods such as join always return null + * conventions to parameterize resultless actions as {@code Void} + * ForkJoinTasks. Because {@code null} is the only valid value of + * type {@code Void}, methods such as join always return {@code null} * upon completion. - * + * *

Sample Usages. Here is a sketch of a ForkJoin sort that - * sorts a given long[] array: + * sorts a given {@code long[]} array: * - *

+ *  
 {@code
  * class SortTask extends RecursiveAction {
  *   final long[] array; final int lo; final int hi;
  *   SortTask(long[] array, int lo, int hi) {
  *     this.array = array; this.lo = lo; this.hi = hi;
  *   }
  *   protected void compute() {
- *     if (hi - lo < THRESHOLD)
+ *     if (hi - lo < THRESHOLD)
  *       sequentiallySort(array, lo, hi);
  *     else {
- *       int mid = (lo + hi) >>> 1;
+ *       int mid = (lo + hi) >>> 1;
  *       invokeAll(new SortTask(array, lo, mid),
  *                 new SortTask(array, mid, hi));
  *       merge(array, lo, hi);
  *     }
  *   }
- * }
- * 
+ * }}
* - * You could then sort anArray by creating new SortTask(anArray, 0, - * anArray.length-1) and invoking it in a ForkJoinPool. + * You could then sort anArray by creating {@code new SortTask(anArray, 0, + * anArray.length-1) } and invoking it in a ForkJoinPool. * As a more concrete simple example, the following task increments * each element of an array: - *
+ *  
 {@code
  * class IncrementTask extends RecursiveAction {
  *   final long[] array; final int lo; final int hi;
  *   IncrementTask(long[] array, int lo, int hi) {
  *     this.array = array; this.lo = lo; this.hi = hi;
  *   }
  *   protected void compute() {
- *     if (hi - lo < THRESHOLD) {
- *       for (int i = lo; i < hi; ++i)
+ *     if (hi - lo < THRESHOLD) {
+ *       for (int i = lo; i < hi; ++i)
  *         array[i]++;
  *     }
  *     else {
- *       int mid = (lo + hi) >>> 1;
+ *       int mid = (lo + hi) >>> 1;
  *       invokeAll(new IncrementTask(array, lo, mid),
  *                 new IncrementTask(array, mid, hi));
  *     }
  *   }
- * }
- * 
- * + * }}
* *

The following example illustrates some refinements and idioms * that may lead to better performance: RecursiveActions need not be @@ -66,12 +63,12 @@ package jsr166y; * divide-and-conquer approach. Here is a class that sums the squares * of each element of a double array, by subdividing out only the * right-hand-sides of repeated divisions by two, and keeping track of - * them with a chain of next references. It uses a dynamic - * threshold based on method surplus, but counterbalances + * them with a chain of {@code next} references. It uses a dynamic + * threshold based on method {@code surplus}, but counterbalances * potential excess partitioning by directly performing leaf actions * on unstolen tasks rather than further subdividing. * - *

+ *  
 {@code
  * double sumOfSquares(ForkJoinPool pool, double[] array) {
  *   int n = array.length;
  *   int seqSize = 1 + n / (8 * pool.getParallelism());
@@ -92,7 +89,7 @@ package jsr166y;
  *
  *   double atLeaf(int l, int r) {
  *     double sum = 0;
- *     for (int i = l; i < h; ++i) // perform leftmost base step
+ *     for (int i = l; i < h; ++i) // perform leftmost base step
  *       sum += array[i] * array[i];
  *     return sum;
  *   }
@@ -101,9 +98,9 @@ package jsr166y;
  *     int l = lo;
  *     int h = hi;
  *     Applyer right = null;
- *     while (h - l > 1 &&
- *        ForkJoinWorkerThread.getEstimatedSurplusTaskCount() <= 3) {
- *        int mid = (l + h) >>> 1;
+ *     while (h - l > 1 &&
+ *        ForkJoinWorkerThread.getEstimatedSurplusTaskCount() <= 3) {
+ *        int mid = (l + h) >>> 1;
  *        right = new Applyer(array, mid, h, seqSize, right);
  *        right.fork();
  *        h = mid;
@@ -120,18 +117,20 @@ package jsr166y;
  *      }
  *     result = sum;
  *   }
- * }
- * 
+ * }}
+ * + * @since 1.7 + * @author Doug Lea */ public abstract class RecursiveAction extends ForkJoinTask { /** - * The main computation performed by this task. + * The main computation performed by this task. */ protected abstract void compute(); /** - * Always returns null + * Always returns null. */ public final Void getRawResult() { return null; } @@ -141,7 +140,7 @@ public abstract class RecursiveAction ex protected final void setRawResult(Void mustBeNull) { } /** - * Implements execution conventions for RecursiveActions + * Implements execution conventions for RecursiveActions. */ protected final boolean exec() { compute();