1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/RecursiveTask.java Sat Mar 19 10:46:31 2016 +0100
1.3 @@ -0,0 +1,97 @@
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 result-bearing {@link ForkJoinTask}.
1.43 + *
1.44 + * <p>For a classic example, here is a task computing Fibonacci numbers:
1.45 + *
1.46 + * <pre> {@code
1.47 + * class Fibonacci extends RecursiveTask<Integer> {
1.48 + * final int n;
1.49 + * Fibonacci(int n) { this.n = n; }
1.50 + * Integer compute() {
1.51 + * if (n <= 1)
1.52 + * return n;
1.53 + * Fibonacci f1 = new Fibonacci(n - 1);
1.54 + * f1.fork();
1.55 + * Fibonacci f2 = new Fibonacci(n - 2);
1.56 + * return f2.compute() + f1.join();
1.57 + * }
1.58 + * }}</pre>
1.59 + *
1.60 + * However, besides being a dumb way to compute Fibonacci functions
1.61 + * (there is a simple fast linear algorithm that you'd use in
1.62 + * practice), this is likely to perform poorly because the smallest
1.63 + * subtasks are too small to be worthwhile splitting up. Instead, as
1.64 + * is the case for nearly all fork/join applications, you'd pick some
1.65 + * minimum granularity size (for example 10 here) for which you always
1.66 + * sequentially solve rather than subdividing.
1.67 + *
1.68 + * @since 1.7
1.69 + * @author Doug Lea
1.70 + */
1.71 +public abstract class RecursiveTask<V> extends ForkJoinTask<V> {
1.72 + private static final long serialVersionUID = 5232453952276485270L;
1.73 +
1.74 + /**
1.75 + * The result of the computation.
1.76 + */
1.77 + V result;
1.78 +
1.79 + /**
1.80 + * The main computation performed by this task.
1.81 + */
1.82 + protected abstract V compute();
1.83 +
1.84 + public final V getRawResult() {
1.85 + return result;
1.86 + }
1.87 +
1.88 + protected final void setRawResult(V value) {
1.89 + result = value;
1.90 + }
1.91 +
1.92 + /**
1.93 + * Implements execution conventions for RecursiveTask.
1.94 + */
1.95 + protected final boolean exec() {
1.96 + result = compute();
1.97 + return true;
1.98 + }
1.99 +
1.100 +}