1771N/A/*
1771N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1771N/A *
1771N/A * This code is free software; you can redistribute it and/or modify it
1771N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1771N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1771N/A *
1771N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1771N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1771N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1771N/A * version 2 for more details (a copy is included in the LICENSE file that
1771N/A * accompanied this code).
1771N/A *
1771N/A * You should have received a copy of the GNU General Public License version
1771N/A * 2 along with this work; if not, write to the Free Software Foundation,
1771N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1771N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1771N/A */
1771N/A
1771N/A/*
1771N/A * This file is available under and governed by the GNU General Public
1771N/A * License version 2 only, as published by the Free Software Foundation.
1771N/A * However, the following notice accompanied the original version of this
1771N/A * file:
1771N/A *
1771N/A * Written by Doug Lea with assistance from members of JCP JSR-166
1771N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
1771N/A */
1771N/A
1771N/Apackage java.util.concurrent;
1771N/A
1771N/A/**
1771N/A * A recursive result-bearing {@link ForkJoinTask}.
1771N/A *
1771N/A * <p>For a classic example, here is a task computing Fibonacci numbers:
1771N/A *
1771N/A * <pre> {@code
1771N/A * class Fibonacci extends RecursiveTask<Integer> {
1771N/A * final int n;
1771N/A * Fibonacci(int n) { this.n = n; }
1771N/A * Integer compute() {
1771N/A * if (n <= 1)
1771N/A * return n;
1771N/A * Fibonacci f1 = new Fibonacci(n - 1);
1771N/A * f1.fork();
1771N/A * Fibonacci f2 = new Fibonacci(n - 2);
1771N/A * return f2.compute() + f1.join();
1771N/A * }
1771N/A * }}</pre>
1771N/A *
1771N/A * However, besides being a dumb way to compute Fibonacci functions
1771N/A * (there is a simple fast linear algorithm that you'd use in
1771N/A * practice), this is likely to perform poorly because the smallest
1771N/A * subtasks are too small to be worthwhile splitting up. Instead, as
1771N/A * is the case for nearly all fork/join applications, you'd pick some
1771N/A * minimum granularity size (for example 10 here) for which you always
1771N/A * sequentially solve rather than subdividing.
1771N/A *
1771N/A * @since 1.7
1771N/A * @author Doug Lea
1771N/A */
1771N/Apublic abstract class RecursiveTask<V> extends ForkJoinTask<V> {
1771N/A private static final long serialVersionUID = 5232453952276485270L;
1771N/A
1771N/A /**
1771N/A * The result of the computation.
1771N/A */
1771N/A V result;
1771N/A
1771N/A /**
1771N/A * The main computation performed by this task.
1771N/A */
1771N/A protected abstract V compute();
1771N/A
1771N/A public final V getRawResult() {
1771N/A return result;
1771N/A }
1771N/A
1771N/A protected final void setRawResult(V value) {
1771N/A result = value;
1771N/A }
1771N/A
1771N/A /**
1771N/A * Implements execution conventions for RecursiveTask.
1771N/A */
1771N/A protected final boolean exec() {
1771N/A result = compute();
1771N/A return true;
1771N/A }
1771N/A
1771N/A}