1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.5 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
|
|
|
9 |
|
|
/** |
10 |
jsr166 |
1.4 |
* A recursive result-bearing {@link ForkJoinTask}. |
11 |
|
|
* |
12 |
dl |
1.12 |
* <p>For example, here is a task-based program for computing Factorials: |
13 |
jsr166 |
1.1 |
* |
14 |
jsr166 |
1.9 |
* <pre> {@code |
15 |
dl |
1.12 |
* import java.util.concurrent.RecursiveTask; |
16 |
|
|
* import java.math.BigInteger; |
17 |
|
|
* public class Factorial { |
18 |
|
|
* static class FactorialTask extends RecursiveTask<BigInteger> { |
19 |
|
|
* private final int from, to; |
20 |
|
|
* FactorialTask(int from, int to) { this.from = from; this.to = to; } |
21 |
|
|
* protected BigInteger compute() { |
22 |
|
|
* int range = to - from; |
23 |
|
|
* if (range == 0) { // base case |
24 |
|
|
* return BigInteger.valueOf(from); |
25 |
|
|
* } else if (range == 1) { // too small to parallelize |
26 |
|
|
* return BigInteger.valueOf(from).multiply(BigInteger.valueOf(to)); |
27 |
|
|
* } else { // split in half |
28 |
|
|
* int mid = from + range / 2; |
29 |
|
|
* FactorialTask leftTask = new FactorialTask(from, mid); |
30 |
|
|
* leftTask.fork(); // perform about half the work locally |
31 |
|
|
* return new FactorialTask(mid + 1, to).compute() |
32 |
|
|
* .multiply(leftTask.join()); |
33 |
|
|
* } |
34 |
|
|
* } |
35 |
|
|
* } |
36 |
|
|
* static BigInteger factorial(int n) { // uses ForkJoinPool.commonPool() |
37 |
|
|
* return (n <= 1) ? BigInteger.ONE : new FactorialTask(1, n).invoke(); |
38 |
|
|
* } |
39 |
|
|
* public static void main(String[] args) { |
40 |
|
|
* System.out.println(factorial(Integer.parseInt(args[0]))); |
41 |
jsr166 |
1.1 |
* } |
42 |
|
|
* }}</pre> |
43 |
|
|
* |
44 |
|
|
* @since 1.7 |
45 |
|
|
* @author Doug Lea |
46 |
|
|
*/ |
47 |
|
|
public abstract class RecursiveTask<V> extends ForkJoinTask<V> { |
48 |
jsr166 |
1.3 |
private static final long serialVersionUID = 5232453952276485270L; |
49 |
jsr166 |
1.1 |
|
50 |
|
|
/** |
51 |
jsr166 |
1.11 |
* Constructor for subclasses to call. |
52 |
|
|
*/ |
53 |
|
|
public RecursiveTask() {} |
54 |
|
|
|
55 |
|
|
/** |
56 |
jsr166 |
1.2 |
* The result of the computation. |
57 |
jsr166 |
1.1 |
*/ |
58 |
jsr166 |
1.10 |
@SuppressWarnings("serial") // Conditionally serializable |
59 |
jsr166 |
1.1 |
V result; |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* The main computation performed by this task. |
63 |
jsr166 |
1.7 |
* @return the result of the computation |
64 |
jsr166 |
1.1 |
*/ |
65 |
|
|
protected abstract V compute(); |
66 |
|
|
|
67 |
|
|
public final V getRawResult() { |
68 |
|
|
return result; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
protected final void setRawResult(V value) { |
72 |
|
|
result = value; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* Implements execution conventions for RecursiveTask. |
77 |
|
|
*/ |
78 |
|
|
protected final boolean exec() { |
79 |
|
|
result = compute(); |
80 |
|
|
return true; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
} |