1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/RecursiveAction.java Sat Mar 19 10:46:31 2016 +0100
1.3 @@ -0,0 +1,181 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.6 + *
1.7 + * This code is free software; you can redistribute it and/or modify it
1.8 + * under the terms of the GNU General Public License version 2 only, as
1.9 + * published by the Free Software Foundation. Oracle designates this
1.10 + * particular file as subject to the "Classpath" exception as provided
1.11 + * by Oracle in the LICENSE file that accompanied this code.
1.12 + *
1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.16 + * version 2 for more details (a copy is included in the LICENSE file that
1.17 + * accompanied this code).
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License version
1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.22 + *
1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.24 + * or visit www.oracle.com if you need additional information or have any
1.25 + * questions.
1.26 + */
1.27 +
1.28 +/*
1.29 + * This file is available under and governed by the GNU General Public
1.30 + * License version 2 only, as published by the Free Software Foundation.
1.31 + * However, the following notice accompanied the original version of this
1.32 + * file:
1.33 + *
1.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
1.35 + * Expert Group and released to the public domain, as explained at
1.36 + * http://creativecommons.org/publicdomain/zero/1.0/
1.37 + */
1.38 +
1.39 +package java.util.concurrent;
1.40 +
1.41 +/**
1.42 + * A recursive resultless {@link ForkJoinTask}. This class
1.43 + * establishes conventions to parameterize resultless actions as
1.44 + * {@code Void} {@code ForkJoinTask}s. Because {@code null} is the
1.45 + * only valid value of type {@code Void}, methods such as join always
1.46 + * return {@code null} upon completion.
1.47 + *
1.48 + * <p><b>Sample Usages.</b> Here is a sketch of a ForkJoin sort that
1.49 + * sorts a given {@code long[]} array:
1.50 + *
1.51 + * <pre> {@code
1.52 + * class SortTask extends RecursiveAction {
1.53 + * final long[] array; final int lo; final int hi;
1.54 + * SortTask(long[] array, int lo, int hi) {
1.55 + * this.array = array; this.lo = lo; this.hi = hi;
1.56 + * }
1.57 + * protected void compute() {
1.58 + * if (hi - lo < THRESHOLD)
1.59 + * sequentiallySort(array, lo, hi);
1.60 + * else {
1.61 + * int mid = (lo + hi) >>> 1;
1.62 + * invokeAll(new SortTask(array, lo, mid),
1.63 + * new SortTask(array, mid, hi));
1.64 + * merge(array, lo, hi);
1.65 + * }
1.66 + * }
1.67 + * }}</pre>
1.68 + *
1.69 + * You could then sort {@code anArray} by creating {@code new
1.70 + * SortTask(anArray, 0, anArray.length-1) } and invoking it in a
1.71 + * ForkJoinPool. As a more concrete simple example, the following
1.72 + * task increments each element of an array:
1.73 + * <pre> {@code
1.74 + * class IncrementTask extends RecursiveAction {
1.75 + * final long[] array; final int lo; final int hi;
1.76 + * IncrementTask(long[] array, int lo, int hi) {
1.77 + * this.array = array; this.lo = lo; this.hi = hi;
1.78 + * }
1.79 + * protected void compute() {
1.80 + * if (hi - lo < THRESHOLD) {
1.81 + * for (int i = lo; i < hi; ++i)
1.82 + * array[i]++;
1.83 + * }
1.84 + * else {
1.85 + * int mid = (lo + hi) >>> 1;
1.86 + * invokeAll(new IncrementTask(array, lo, mid),
1.87 + * new IncrementTask(array, mid, hi));
1.88 + * }
1.89 + * }
1.90 + * }}</pre>
1.91 + *
1.92 + * <p>The following example illustrates some refinements and idioms
1.93 + * that may lead to better performance: RecursiveActions need not be
1.94 + * fully recursive, so long as they maintain the basic
1.95 + * divide-and-conquer approach. Here is a class that sums the squares
1.96 + * of each element of a double array, by subdividing out only the
1.97 + * right-hand-sides of repeated divisions by two, and keeping track of
1.98 + * them with a chain of {@code next} references. It uses a dynamic
1.99 + * threshold based on method {@code getSurplusQueuedTaskCount}, but
1.100 + * counterbalances potential excess partitioning by directly
1.101 + * performing leaf actions on unstolen tasks rather than further
1.102 + * subdividing.
1.103 + *
1.104 + * <pre> {@code
1.105 + * double sumOfSquares(ForkJoinPool pool, double[] array) {
1.106 + * int n = array.length;
1.107 + * Applyer a = new Applyer(array, 0, n, null);
1.108 + * pool.invoke(a);
1.109 + * return a.result;
1.110 + * }
1.111 + *
1.112 + * class Applyer extends RecursiveAction {
1.113 + * final double[] array;
1.114 + * final int lo, hi;
1.115 + * double result;
1.116 + * Applyer next; // keeps track of right-hand-side tasks
1.117 + * Applyer(double[] array, int lo, int hi, Applyer next) {
1.118 + * this.array = array; this.lo = lo; this.hi = hi;
1.119 + * this.next = next;
1.120 + * }
1.121 + *
1.122 + * double atLeaf(int l, int h) {
1.123 + * double sum = 0;
1.124 + * for (int i = l; i < h; ++i) // perform leftmost base step
1.125 + * sum += array[i] * array[i];
1.126 + * return sum;
1.127 + * }
1.128 + *
1.129 + * protected void compute() {
1.130 + * int l = lo;
1.131 + * int h = hi;
1.132 + * Applyer right = null;
1.133 + * while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) {
1.134 + * int mid = (l + h) >>> 1;
1.135 + * right = new Applyer(array, mid, h, right);
1.136 + * right.fork();
1.137 + * h = mid;
1.138 + * }
1.139 + * double sum = atLeaf(l, h);
1.140 + * while (right != null) {
1.141 + * if (right.tryUnfork()) // directly calculate if not stolen
1.142 + * sum += right.atLeaf(right.lo, right.hi);
1.143 + * else {
1.144 + * right.join();
1.145 + * sum += right.result;
1.146 + * }
1.147 + * right = right.next;
1.148 + * }
1.149 + * result = sum;
1.150 + * }
1.151 + * }}</pre>
1.152 + *
1.153 + * @since 1.7
1.154 + * @author Doug Lea
1.155 + */
1.156 +public abstract class RecursiveAction extends ForkJoinTask<Void> {
1.157 + private static final long serialVersionUID = 5232453952276485070L;
1.158 +
1.159 + /**
1.160 + * The main computation performed by this task.
1.161 + */
1.162 + protected abstract void compute();
1.163 +
1.164 + /**
1.165 + * Always returns {@code null}.
1.166 + *
1.167 + * @return {@code null} always
1.168 + */
1.169 + public final Void getRawResult() { return null; }
1.170 +
1.171 + /**
1.172 + * Requires null completion value.
1.173 + */
1.174 + protected final void setRawResult(Void mustBeNull) { }
1.175 +
1.176 + /**
1.177 + * Implements execution conventions for RecursiveActions.
1.178 + */
1.179 + protected final boolean exec() {
1.180 + compute();
1.181 + return true;
1.182 + }
1.183 +
1.184 +}