rt/emul/compact/src/main/java/java/util/concurrent/RecursiveTask.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 result-bearing {@link ForkJoinTask}.
jaroslav@1890
    40
 *
jaroslav@1890
    41
 * <p>For a classic example, here is a task computing Fibonacci numbers:
jaroslav@1890
    42
 *
jaroslav@1890
    43
 *  <pre> {@code
jaroslav@1890
    44
 * class Fibonacci extends RecursiveTask<Integer> {
jaroslav@1890
    45
 *   final int n;
jaroslav@1890
    46
 *   Fibonacci(int n) { this.n = n; }
jaroslav@1890
    47
 *   Integer compute() {
jaroslav@1890
    48
 *     if (n <= 1)
jaroslav@1890
    49
 *        return n;
jaroslav@1890
    50
 *     Fibonacci f1 = new Fibonacci(n - 1);
jaroslav@1890
    51
 *     f1.fork();
jaroslav@1890
    52
 *     Fibonacci f2 = new Fibonacci(n - 2);
jaroslav@1890
    53
 *     return f2.compute() + f1.join();
jaroslav@1890
    54
 *   }
jaroslav@1890
    55
 * }}</pre>
jaroslav@1890
    56
 *
jaroslav@1890
    57
 * However, besides being a dumb way to compute Fibonacci functions
jaroslav@1890
    58
 * (there is a simple fast linear algorithm that you'd use in
jaroslav@1890
    59
 * practice), this is likely to perform poorly because the smallest
jaroslav@1890
    60
 * subtasks are too small to be worthwhile splitting up. Instead, as
jaroslav@1890
    61
 * is the case for nearly all fork/join applications, you'd pick some
jaroslav@1890
    62
 * minimum granularity size (for example 10 here) for which you always
jaroslav@1890
    63
 * sequentially solve rather than subdividing.
jaroslav@1890
    64
 *
jaroslav@1890
    65
 * @since 1.7
jaroslav@1890
    66
 * @author Doug Lea
jaroslav@1890
    67
 */
jaroslav@1890
    68
public abstract class RecursiveTask<V> extends ForkJoinTask<V> {
jaroslav@1890
    69
    private static final long serialVersionUID = 5232453952276485270L;
jaroslav@1890
    70
jaroslav@1890
    71
    /**
jaroslav@1890
    72
     * The result of the computation.
jaroslav@1890
    73
     */
jaroslav@1890
    74
    V result;
jaroslav@1890
    75
jaroslav@1890
    76
    /**
jaroslav@1890
    77
     * The main computation performed by this task.
jaroslav@1890
    78
     */
jaroslav@1890
    79
    protected abstract V compute();
jaroslav@1890
    80
jaroslav@1890
    81
    public final V getRawResult() {
jaroslav@1890
    82
        return result;
jaroslav@1890
    83
    }
jaroslav@1890
    84
jaroslav@1890
    85
    protected final void setRawResult(V value) {
jaroslav@1890
    86
        result = value;
jaroslav@1890
    87
    }
jaroslav@1890
    88
jaroslav@1890
    89
    /**
jaroslav@1890
    90
     * Implements execution conventions for RecursiveTask.
jaroslav@1890
    91
     */
jaroslav@1890
    92
    protected final boolean exec() {
jaroslav@1890
    93
        result = compute();
jaroslav@1890
    94
        return true;
jaroslav@1890
    95
    }
jaroslav@1890
    96
jaroslav@1890
    97
}