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 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.concurrent.*; |
13 |
|
* fibonacci(0) = 0; |
14 |
|
* fibonacci(1) = 1. |
15 |
|
* </pre> |
16 |
< |
**/ |
17 |
< |
|
16 |
> |
*/ |
17 |
|
public final class Fib extends RecursiveAction { |
18 |
|
|
19 |
|
// Performance-tuning constant: |
39 |
|
} |
40 |
|
|
41 |
|
for (int reps = 0; reps < 2; ++reps) { |
42 |
< |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
42 |
> |
ForkJoinPool g = (procs == 0) ? ForkJoinPool.commonPool() : |
43 |
|
new ForkJoinPool(procs); |
44 |
|
lastStealCount = g.getStealCount(); |
45 |
|
for (int i = 0; i < 20; ++i) { |
48 |
|
// Thread.sleep(100); |
49 |
|
} |
50 |
|
System.out.println(g); |
51 |
< |
g.shutdown(); |
52 |
< |
if (!g.awaitTermination(10, TimeUnit.SECONDS)) |
54 |
< |
throw new Error(); |
55 |
< |
g = null; |
51 |
> |
if (g != ForkJoinPool.commonPool()) |
52 |
> |
g.shutdown(); |
53 |
|
Thread.sleep(500); |
54 |
|
} |
55 |
|
} |
56 |
|
|
60 |
– |
|
57 |
|
/** for time conversion */ |
58 |
|
static final long NPS = (1000L * 1000 * 1000); |
59 |
|
|
60 |
|
static void test(ForkJoinPool g, int num) throws Exception { |
61 |
|
int ps = g.getParallelism(); |
66 |
– |
// g.setParallelism(ps); |
62 |
|
long start = System.nanoTime(); |
63 |
|
Fib f = new Fib(num); |
64 |
|
g.invoke(f); |
76 |
|
System.out.println(); |
77 |
|
} |
78 |
|
|
84 |
– |
|
79 |
|
// Initialized with argument; replaced with result |
80 |
|
int number; |
81 |
|
|
93 |
|
else { |
94 |
|
Fib f1 = new Fib(n - 1); |
95 |
|
Fib f2 = new Fib(n - 2); |
102 |
– |
// forkJoin(f1, f2); |
96 |
|
invokeAll(f1, f2); |
97 |
|
number = f1.number + f2.number; |
98 |
|
} |
108 |
|
} while (--n > 1); |
109 |
|
return r; |
110 |
|
} |
111 |
< |
|
111 |
> |
|
112 |
|
} |
120 |
– |
|