9 |
|
/** |
10 |
|
* A recursive result-bearing {@link ForkJoinTask}. |
11 |
|
* |
12 |
< |
* <p>For a classic example, here is a task computing Fibonacci numbers: |
12 |
> |
* <p>For example, here is a task-based program for computing Factorials: |
13 |
|
* |
14 |
|
* <pre> {@code |
15 |
< |
* class Fibonacci extends RecursiveTask<Integer> { |
16 |
< |
* final int n; |
17 |
< |
* Fibonacci(int n) { this.n = n; } |
18 |
< |
* protected Integer compute() { |
19 |
< |
* if (n <= 1) |
20 |
< |
* return n; |
21 |
< |
* Fibonacci f1 = new Fibonacci(n - 1); |
22 |
< |
* f1.fork(); |
23 |
< |
* Fibonacci f2 = new Fibonacci(n - 2); |
24 |
< |
* return f2.compute() + f1.join(); |
15 |
> |
* 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 |
|
* } |
42 |
|
* }}</pre> |
43 |
|
* |
28 |
– |
* However, besides being a dumb way to compute Fibonacci functions |
29 |
– |
* (there is a simple fast linear algorithm that you'd use in |
30 |
– |
* practice), this is likely to perform poorly because the smallest |
31 |
– |
* subtasks are too small to be worthwhile splitting up. Instead, as |
32 |
– |
* is the case for nearly all fork/join applications, you'd pick some |
33 |
– |
* minimum granularity size (for example 10 here) for which you always |
34 |
– |
* sequentially solve rather than subdividing. |
35 |
– |
* |
44 |
|
* @since 1.7 |
45 |
|
* @author Doug Lea |
46 |
|
*/ |