rt/emul/compact/src/main/java/java/util/concurrent/RecursiveAction.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
package java.util.concurrent;
jaroslav@1890
    37
jaroslav@1890
    38
/**
jaroslav@1890
    39
 * A recursive resultless {@link ForkJoinTask}.  This class
jaroslav@1890
    40
 * establishes conventions to parameterize resultless actions as
jaroslav@1890
    41
 * {@code Void} {@code ForkJoinTask}s. Because {@code null} is the
jaroslav@1890
    42
 * only valid value of type {@code Void}, methods such as join always
jaroslav@1890
    43
 * return {@code null} upon completion.
jaroslav@1890
    44
 *
jaroslav@1890
    45
 * <p><b>Sample Usages.</b> Here is a sketch of a ForkJoin sort that
jaroslav@1890
    46
 * sorts a given {@code long[]} array:
jaroslav@1890
    47
 *
jaroslav@1890
    48
 *  <pre> {@code
jaroslav@1890
    49
 * class SortTask extends RecursiveAction {
jaroslav@1890
    50
 *   final long[] array; final int lo; final int hi;
jaroslav@1890
    51
 *   SortTask(long[] array, int lo, int hi) {
jaroslav@1890
    52
 *     this.array = array; this.lo = lo; this.hi = hi;
jaroslav@1890
    53
 *   }
jaroslav@1890
    54
 *   protected void compute() {
jaroslav@1890
    55
 *     if (hi - lo < THRESHOLD)
jaroslav@1890
    56
 *       sequentiallySort(array, lo, hi);
jaroslav@1890
    57
 *     else {
jaroslav@1890
    58
 *       int mid = (lo + hi) >>> 1;
jaroslav@1890
    59
 *       invokeAll(new SortTask(array, lo, mid),
jaroslav@1890
    60
 *                 new SortTask(array, mid, hi));
jaroslav@1890
    61
 *       merge(array, lo, hi);
jaroslav@1890
    62
 *     }
jaroslav@1890
    63
 *   }
jaroslav@1890
    64
 * }}</pre>
jaroslav@1890
    65
 *
jaroslav@1890
    66
 * You could then sort {@code anArray} by creating {@code new
jaroslav@1890
    67
 * SortTask(anArray, 0, anArray.length-1) } and invoking it in a
jaroslav@1890
    68
 * ForkJoinPool.  As a more concrete simple example, the following
jaroslav@1890
    69
 * task increments each element of an array:
jaroslav@1890
    70
 *  <pre> {@code
jaroslav@1890
    71
 * class IncrementTask extends RecursiveAction {
jaroslav@1890
    72
 *   final long[] array; final int lo; final int hi;
jaroslav@1890
    73
 *   IncrementTask(long[] array, int lo, int hi) {
jaroslav@1890
    74
 *     this.array = array; this.lo = lo; this.hi = hi;
jaroslav@1890
    75
 *   }
jaroslav@1890
    76
 *   protected void compute() {
jaroslav@1890
    77
 *     if (hi - lo < THRESHOLD) {
jaroslav@1890
    78
 *       for (int i = lo; i < hi; ++i)
jaroslav@1890
    79
 *         array[i]++;
jaroslav@1890
    80
 *     }
jaroslav@1890
    81
 *     else {
jaroslav@1890
    82
 *       int mid = (lo + hi) >>> 1;
jaroslav@1890
    83
 *       invokeAll(new IncrementTask(array, lo, mid),
jaroslav@1890
    84
 *                 new IncrementTask(array, mid, hi));
jaroslav@1890
    85
 *     }
jaroslav@1890
    86
 *   }
jaroslav@1890
    87
 * }}</pre>
jaroslav@1890
    88
 *
jaroslav@1890
    89
 * <p>The following example illustrates some refinements and idioms
jaroslav@1890
    90
 * that may lead to better performance: RecursiveActions need not be
jaroslav@1890
    91
 * fully recursive, so long as they maintain the basic
jaroslav@1890
    92
 * divide-and-conquer approach. Here is a class that sums the squares
jaroslav@1890
    93
 * of each element of a double array, by subdividing out only the
jaroslav@1890
    94
 * right-hand-sides of repeated divisions by two, and keeping track of
jaroslav@1890
    95
 * them with a chain of {@code next} references. It uses a dynamic
jaroslav@1890
    96
 * threshold based on method {@code getSurplusQueuedTaskCount}, but
jaroslav@1890
    97
 * counterbalances potential excess partitioning by directly
jaroslav@1890
    98
 * performing leaf actions on unstolen tasks rather than further
jaroslav@1890
    99
 * subdividing.
jaroslav@1890
   100
 *
jaroslav@1890
   101
 *  <pre> {@code
jaroslav@1890
   102
 * double sumOfSquares(ForkJoinPool pool, double[] array) {
jaroslav@1890
   103
 *   int n = array.length;
jaroslav@1890
   104
 *   Applyer a = new Applyer(array, 0, n, null);
jaroslav@1890
   105
 *   pool.invoke(a);
jaroslav@1890
   106
 *   return a.result;
jaroslav@1890
   107
 * }
jaroslav@1890
   108
 *
jaroslav@1890
   109
 * class Applyer extends RecursiveAction {
jaroslav@1890
   110
 *   final double[] array;
jaroslav@1890
   111
 *   final int lo, hi;
jaroslav@1890
   112
 *   double result;
jaroslav@1890
   113
 *   Applyer next; // keeps track of right-hand-side tasks
jaroslav@1890
   114
 *   Applyer(double[] array, int lo, int hi, Applyer next) {
jaroslav@1890
   115
 *     this.array = array; this.lo = lo; this.hi = hi;
jaroslav@1890
   116
 *     this.next = next;
jaroslav@1890
   117
 *   }
jaroslav@1890
   118
 *
jaroslav@1890
   119
 *   double atLeaf(int l, int h) {
jaroslav@1890
   120
 *     double sum = 0;
jaroslav@1890
   121
 *     for (int i = l; i < h; ++i) // perform leftmost base step
jaroslav@1890
   122
 *       sum += array[i] * array[i];
jaroslav@1890
   123
 *     return sum;
jaroslav@1890
   124
 *   }
jaroslav@1890
   125
 *
jaroslav@1890
   126
 *   protected void compute() {
jaroslav@1890
   127
 *     int l = lo;
jaroslav@1890
   128
 *     int h = hi;
jaroslav@1890
   129
 *     Applyer right = null;
jaroslav@1890
   130
 *     while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) {
jaroslav@1890
   131
 *        int mid = (l + h) >>> 1;
jaroslav@1890
   132
 *        right = new Applyer(array, mid, h, right);
jaroslav@1890
   133
 *        right.fork();
jaroslav@1890
   134
 *        h = mid;
jaroslav@1890
   135
 *     }
jaroslav@1890
   136
 *     double sum = atLeaf(l, h);
jaroslav@1890
   137
 *     while (right != null) {
jaroslav@1890
   138
 *        if (right.tryUnfork()) // directly calculate if not stolen
jaroslav@1890
   139
 *          sum += right.atLeaf(right.lo, right.hi);
jaroslav@1890
   140
 *       else {
jaroslav@1890
   141
 *          right.join();
jaroslav@1890
   142
 *          sum += right.result;
jaroslav@1890
   143
 *        }
jaroslav@1890
   144
 *        right = right.next;
jaroslav@1890
   145
 *      }
jaroslav@1890
   146
 *     result = sum;
jaroslav@1890
   147
 *   }
jaroslav@1890
   148
 * }}</pre>
jaroslav@1890
   149
 *
jaroslav@1890
   150
 * @since 1.7
jaroslav@1890
   151
 * @author Doug Lea
jaroslav@1890
   152
 */
jaroslav@1890
   153
public abstract class RecursiveAction extends ForkJoinTask<Void> {
jaroslav@1890
   154
    private static final long serialVersionUID = 5232453952276485070L;
jaroslav@1890
   155
jaroslav@1890
   156
    /**
jaroslav@1890
   157
     * The main computation performed by this task.
jaroslav@1890
   158
     */
jaroslav@1890
   159
    protected abstract void compute();
jaroslav@1890
   160
jaroslav@1890
   161
    /**
jaroslav@1890
   162
     * Always returns {@code null}.
jaroslav@1890
   163
     *
jaroslav@1890
   164
     * @return {@code null} always
jaroslav@1890
   165
     */
jaroslav@1890
   166
    public final Void getRawResult() { return null; }
jaroslav@1890
   167
jaroslav@1890
   168
    /**
jaroslav@1890
   169
     * Requires null completion value.
jaroslav@1890
   170
     */
jaroslav@1890
   171
    protected final void setRawResult(Void mustBeNull) { }
jaroslav@1890
   172
jaroslav@1890
   173
    /**
jaroslav@1890
   174
     * Implements execution conventions for RecursiveActions.
jaroslav@1890
   175
     */
jaroslav@1890
   176
    protected final boolean exec() {
jaroslav@1890
   177
        compute();
jaroslav@1890
   178
        return true;
jaroslav@1890
   179
    }
jaroslav@1890
   180
jaroslav@1890
   181
}