ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/RecursiveAction.java
(Generate patch)

Comparing jsr166/src/jsr166y/RecursiveAction.java (file contents):
Revision 1.8 by dl, Wed Jul 29 12:05:55 2009 UTC vs.
Revision 1.20 by jsr166, Sun Nov 25 21:21:11 2012 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   package jsr166y;
8  
9   /**
10 < * Recursive resultless ForkJoinTasks. This class establishes
11 < * conventions to parameterize resultless actions as {@code Void}
12 < * ForkJoinTasks. Because {@code null} is the only valid value of
13 < * type {@code Void}, methods such as join always return {@code null}
14 < * upon completion.
10 > * A recursive resultless {@link ForkJoinTask}.  This class
11 > * establishes conventions to parameterize resultless actions as
12 > * {@code Void} {@code ForkJoinTask}s. Because {@code null} is the
13 > * only valid value of type {@code Void}, methods such as {@code join}
14 > * always return {@code null} upon completion.
15   *
16 < * <p><b>Sample Usages.</b> Here is a sketch of a ForkJoin sort that
17 < * sorts a given {@code long[]} array:
16 > * <p><b>Sample Usages.</b> Here is a simple but complete ForkJoin
17 > * sort that sorts a given {@code long[]} array:
18   *
19   *  <pre> {@code
20 < * class SortTask extends RecursiveAction {
21 < *   final long[] array; final int lo; final int hi;
20 > * static class SortTask extends RecursiveAction {
21 > *   final long[] array; final int lo, hi;
22   *   SortTask(long[] array, int lo, int hi) {
23   *     this.array = array; this.lo = lo; this.hi = hi;
24   *   }
25 + *   SortTask(long[] array) { this(array, 0, array.length); }
26   *   protected void compute() {
27   *     if (hi - lo < THRESHOLD)
28 < *       sequentiallySort(array, lo, hi);
28 > *       sortSequentially(lo, hi);
29   *     else {
30   *       int mid = (lo + hi) >>> 1;
31   *       invokeAll(new SortTask(array, lo, mid),
32   *                 new SortTask(array, mid, hi));
33 < *       merge(array, lo, hi);
33 > *       merge(lo, mid, hi);
34   *     }
35   *   }
36 + *   // implementation details follow:
37 + *   final static int THRESHOLD = 1000;
38 + *   void sortSequentially(int lo, int hi) {
39 + *     Arrays.sort(array, lo, hi);
40 + *   }
41 + *   void merge(int lo, int mid, int hi) {
42 + *     long[] buf = Arrays.copyOfRange(array, lo, mid);
43 + *     for (int i = 0, j = lo, k = mid; i < buf.length; j++)
44 + *       array[j] = (k == hi || buf[i] < array[k]) ?
45 + *         buf[i++] : array[k++];
46 + *   }
47   * }}</pre>
48   *
49   * You could then sort {@code anArray} by creating {@code new
50 < * SortTask(anArray, 0, anArray.length-1) } and invoking it in a
51 < * ForkJoinPool.  As a more concrete simple example, the following
52 < * task increments each element of an array:
50 > * SortTask(anArray)} and invoking it in a ForkJoinPool.  As a more
51 > * concrete simple example, the following task increments each element
52 > * of an array:
53   *  <pre> {@code
54   * class IncrementTask extends RecursiveAction {
55 < *   final long[] array; final int lo; final int hi;
55 > *   final long[] array; final int lo, hi;
56   *   IncrementTask(long[] array, int lo, int hi) {
57   *     this.array = array; this.lo = lo; this.hi = hi;
58   *   }
# Line 64 | Line 76 | package jsr166y;
76   * of each element of a double array, by subdividing out only the
77   * right-hand-sides of repeated divisions by two, and keeping track of
78   * them with a chain of {@code next} references. It uses a dynamic
79 < * threshold based on method {@code surplus}, but counterbalances
80 < * potential excess partitioning by directly performing leaf actions
81 < * on unstolen tasks rather than further subdividing.
79 > * threshold based on method {@code getSurplusQueuedTaskCount}, but
80 > * counterbalances potential excess partitioning by directly
81 > * performing leaf actions on unstolen tasks rather than further
82 > * subdividing.
83   *
84   *  <pre> {@code
85   * double sumOfSquares(ForkJoinPool pool, double[] array) {
86   *   int n = array.length;
87 < *   int seqSize = 1 + n / (8 * pool.getParallelism());
75 < *   Applyer a = new Applyer(array, 0, n, seqSize, null);
87 > *   Applyer a = new Applyer(array, 0, n, null);
88   *   pool.invoke(a);
89   *   return a.result;
90   * }
91   *
92   * class Applyer extends RecursiveAction {
93   *   final double[] array;
94 < *   final int lo, hi, seqSize;
94 > *   final int lo, hi;
95   *   double result;
96   *   Applyer next; // keeps track of right-hand-side tasks
97 < *   Applyer(double[] array, int lo, int hi, int seqSize, Applyer next) {
97 > *   Applyer(double[] array, int lo, int hi, Applyer next) {
98   *     this.array = array; this.lo = lo; this.hi = hi;
99 < *     this.seqSize = seqSize; this.next = next;
99 > *     this.next = next;
100   *   }
101   *
102 < *   double atLeaf(int l, int r) {
102 > *   double atLeaf(int l, int h) {
103   *     double sum = 0;
104   *     for (int i = l; i < h; ++i) // perform leftmost base step
105   *       sum += array[i] * array[i];
# Line 99 | Line 111 | package jsr166y;
111   *     int h = hi;
112   *     Applyer right = null;
113   *     while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) {
114 < *        int mid = (l + h) >>> 1;
115 < *        right = new Applyer(array, mid, h, seqSize, right);
116 < *        right.fork();
117 < *        h = mid;
114 > *       int mid = (l + h) >>> 1;
115 > *       right = new Applyer(array, mid, h, right);
116 > *       right.fork();
117 > *       h = mid;
118   *     }
119   *     double sum = atLeaf(l, h);
120   *     while (right != null) {
121 < *        if (right.tryUnfork()) // directly calculate if not stolen
122 < *          sum += right.atLeaf(right.lo, right.hi);
121 > *       if (right.tryUnfork()) // directly calculate if not stolen
122 > *         sum += right.atLeaf(right.lo, right.hi);
123   *       else {
124 < *          right.helpJoin();
125 < *          sum += right.result;
126 < *        }
127 < *        right = right.next;
128 < *      }
124 > *         right.join();
125 > *         sum += right.result;
126 > *       }
127 > *       right = right.next;
128 > *     }
129   *     result = sum;
130   *   }
131   * }}</pre>
# Line 122 | Line 134 | package jsr166y;
134   * @author Doug Lea
135   */
136   public abstract class RecursiveAction extends ForkJoinTask<Void> {
137 +    private static final long serialVersionUID = 5232453952276485070L;
138  
139      /**
140       * The main computation performed by this task.
# Line 129 | Line 142 | public abstract class RecursiveAction ex
142      protected abstract void compute();
143  
144      /**
145 <     * Always returns null.
145 >     * Always returns {@code null}.
146 >     *
147 >     * @return {@code null} always
148       */
149      public final Void getRawResult() { return null; }
150  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines