1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * Written by Doug Lea with assistance from members of JCP JSR-166 32 * Expert Group and released to the public domain, as explained at 33 * http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util.concurrent; 37 38 /** 39 * A recursive result-bearing {@link ForkJoinTask}. 40 * 41 * <p>For example, here is a task-based program for computing Factorials: 42 * 43 * <pre> {@code 44 * import java.util.concurrent.RecursiveTask; 45 * import java.math.BigInteger; 46 * public class Factorial { 47 * static class FactorialTask extends RecursiveTask<BigInteger> { 48 * private final int from, to; 49 * FactorialTask(int from, int to) { this.from = from; this.to = to; } 50 * protected BigInteger compute() { 51 * int range = to - from; 52 * if (range == 0) { // base case 53 * return BigInteger.valueOf(from); 54 * } else if (range == 1) { // too small to parallelize 55 * return BigInteger.valueOf(from).multiply(BigInteger.valueOf(to)); 56 * } else { // split in half 57 * int mid = from + range / 2; 58 * FactorialTask leftTask = new FactorialTask(from, mid); 59 * leftTask.fork(); // perform about half the work locally 60 * return new FactorialTask(mid + 1, to).compute() 61 * .multiply(leftTask.join()); 62 * } 63 * } 64 * } 65 * static BigInteger factorial(int n) { // uses ForkJoinPool.commonPool() 66 * return (n <= 1) ? BigInteger.ONE : new FactorialTask(1, n).invoke(); 67 * } 68 * public static void main(String[] args) { 69 * System.out.println(factorial(Integer.parseInt(args[0]))); 70 * } 71 * }}</pre> 72 * 73 * @param <V> the type of the result of the task 74 * 75 * @since 1.7 76 * @author Doug Lea 77 */ 78 public abstract class RecursiveTask<V> extends ForkJoinTask<V> { 79 private static final long serialVersionUID = 5232453952276485270L; 80 81 /** 82 * Constructor for subclasses to call. 83 */ RecursiveTask()84 public RecursiveTask() {} 85 86 /** 87 * The result of the computation. 88 */ 89 @SuppressWarnings("serial") // Conditionally serializable 90 V result; 91 92 /** 93 * The main computation performed by this task. 94 * @return the result of the computation 95 */ compute()96 protected abstract V compute(); 97 getRawResult()98 public final V getRawResult() { 99 return result; 100 } 101 setRawResult(V value)102 protected final void setRawResult(V value) { 103 result = value; 104 } 105 106 /** 107 * Implements execution conventions for RecursiveTask. 108 */ exec()109 protected final boolean exec() { 110 result = compute(); 111 return true; 112 } 113 114 } 115