diff options
Diffstat (limited to 'src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java')
-rw-r--r-- | src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java | 89 |
1 files changed, 45 insertions, 44 deletions
diff --git a/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java b/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java index 2d36f7eb33..bb24d9e575 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java +++ b/src/forkjoin/scala/concurrent/forkjoin/RecursiveAction.java @@ -1,64 +1,61 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ package scala.concurrent.forkjoin; /** - * Recursive resultless ForkJoinTasks. This class establishes - * conventions to parameterize resultless actions as <tt>Void</tt> - * ForkJoinTasks. Because <tt>null</tt> is the only valid value of - * <tt>Void</tt>, methods such as join always return <tt>null</tt> - * upon completion. + * A recursive resultless {@link ForkJoinTask}. This class + * establishes conventions to parameterize resultless actions as + * {@code Void} {@code ForkJoinTask}s. Because {@code null} is the + * only valid value of type {@code Void}, methods such as join always + * return {@code null} upon completion. * * <p><b>Sample Usages.</b> Here is a sketch of a ForkJoin sort that - * sorts a given <tt>long[]</tt> array: + * sorts a given {@code long[]} array: * - * <pre> + * <pre> {@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); * } * } - * } - * </pre> + * }}</pre> * - * You could then sort anArray by creating <tt>new SortTask(anArray, 0, - * anArray.length-1) </tt> and invoking it in a ForkJoinPool. - * As a more concrete simple example, the following task increments - * each element of an array: - * <pre> + * You could then sort {@code 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: + * <pre> {@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)); * } * } - * } - * </pre> - * + * }}</pre> * * <p>The following example illustrates some refinements and idioms * that may lead to better performance: RecursiveActions need not be @@ -66,33 +63,33 @@ package scala.concurrent.forkjoin; * 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 <tt>next</tt> references. It uses a dynamic - * threshold based on method <tt>surplus</tt>, but counterbalances - * potential excess partitioning by directly performing leaf actions - * on unstolen tasks rather than further subdividing. + * them with a chain of {@code next} references. It uses a dynamic + * threshold based on method {@code getSurplusQueuedTaskCount}, but + * counterbalances potential excess partitioning by directly + * performing leaf actions on unstolen tasks rather than further + * subdividing. * - * <pre> + * <pre> {@code * double sumOfSquares(ForkJoinPool pool, double[] array) { * int n = array.length; - * int seqSize = 1 + n / (8 * pool.getParallelism()); - * Applyer a = new Applyer(array, 0, n, seqSize, null); + * Applyer a = new Applyer(array, 0, n, null); * pool.invoke(a); * return a.result; * } * * class Applyer extends RecursiveAction { * final double[] array; - * final int lo, hi, seqSize; + * final int lo, hi; * double result; * Applyer next; // keeps track of right-hand-side tasks - * Applyer(double[] array, int lo, int hi, int seqSize, Applyer next) { + * Applyer(double[] array, int lo, int hi, Applyer next) { * this.array = array; this.lo = lo; this.hi = hi; - * this.seqSize = seqSize; this.next = next; + * this.next = next; * } * - * double atLeaf(int l, int r) { + * double atLeaf(int l, int h) { * 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,10 +98,9 @@ package scala.concurrent.forkjoin; * int l = lo; * int h = hi; * Applyer right = null; - * while (h - l > 1 && - * ForkJoinWorkerThread.getEstimatedSurplusTaskCount() <= 3) { - * int mid = (l + h) >>> 1; - * right = new Applyer(array, mid, h, seqSize, right); + * while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) { + * int mid = (l + h) >>> 1; + * right = new Applyer(array, mid, h, right); * right.fork(); * h = mid; * } @@ -113,17 +109,20 @@ package scala.concurrent.forkjoin; * if (right.tryUnfork()) // directly calculate if not stolen * sum += right.atLeaf(right.lo, right.hi); * else { - * right.helpJoin(); + * right.join(); * sum += right.result; * } * right = right.next; * } * result = sum; * } - * } - * </pre> + * }}</pre> + * + * @since 1.7 + * @author Doug Lea */ public abstract class RecursiveAction extends ForkJoinTask<Void> { + private static final long serialVersionUID = 5232453952276485070L; /** * The main computation performed by this task. @@ -131,7 +130,9 @@ public abstract class RecursiveAction extends ForkJoinTask<Void> { protected abstract void compute(); /** - * Always returns null + * Always returns {@code null}. + * + * @return {@code null} always */ public final Void getRawResult() { return null; } @@ -141,7 +142,7 @@ public abstract class RecursiveAction extends ForkJoinTask<Void> { protected final void setRawResult(Void mustBeNull) { } /** - * Implements execution conventions for RecursiveActions + * Implements execution conventions for RecursiveActions. */ protected final boolean exec() { compute(); |