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.*; |
21 |
|
static final long NPS = (1000L * 1000 * 1000); |
22 |
|
|
23 |
|
static int tasksPerPhaser = Math.max(NCPUS / 4, 4); |
24 |
< |
|
24 |
> |
|
25 |
|
static void build(Runnable[] actions, int sz, int lo, int hi, Phaser b) { |
26 |
< |
int step = (hi - lo) / tasksPerPhaser; |
27 |
< |
if (step > 1) { |
28 |
< |
int i = lo; |
29 |
< |
while (i < hi) { |
30 |
< |
int r = Math.min(i + step, hi); |
31 |
< |
build(actions, sz, i, r, new Phaser(b)); |
32 |
< |
i = r; |
26 |
> |
if (hi - lo > tasksPerPhaser) { |
27 |
> |
for (int i = lo; i < hi; i += tasksPerPhaser) { |
28 |
> |
int j = Math.min(i + tasksPerPhaser, hi); |
29 |
> |
build(actions, sz, i, j, new Phaser(b)); |
30 |
|
} |
31 |
< |
} |
32 |
< |
else { |
36 |
< |
for (int i = lo; i < hi; ++i) |
31 |
> |
} else { |
32 |
> |
for (int i = lo; i < hi; ++i) |
33 |
|
actions[i] = new PhaserAction(i, b, sz); |
34 |
|
} |
35 |
|
} |
49 |
|
public void run() { |
50 |
|
int n = size; |
51 |
|
Phaser b = phaser; |
52 |
< |
for(int i = 0; i < n; ++i) { |
52 |
> |
for (int i = 0; i < n; ++i) { |
53 |
|
int p = b.arrive(); |
54 |
|
while (b.getPhase() == p) { |
55 |
|
if ((ThreadLocalRandom.current().nextInt() & 127) == 0) |
65 |
|
nthreads = Integer.parseInt(args[0]); |
66 |
|
if (args.length > 1) |
67 |
|
tasksPerPhaser = Integer.parseInt(args[1]); |
68 |
< |
|
68 |
> |
|
69 |
|
System.out.printf("Max %d Threads, %d tasks per phaser\n", nthreads, tasksPerPhaser); |
70 |
|
|
71 |
|
for (int k = 2; k <= nthreads; k *= 2) { |
72 |
|
for (int size = FIRST_SIZE; size <= LAST_SIZE; size *= 10) { |
73 |
|
long startTime = System.nanoTime(); |
74 |
< |
|
74 |
> |
|
75 |
|
Runnable[] actions = new Runnable [k]; |
76 |
|
build(actions, size, 0, k, new Phaser()); |
77 |
|
Future[] futures = new Future[k]; |
89 |
|
} |
90 |
|
pool.shutdown(); |
91 |
|
} |
96 |
– |
|
97 |
– |
} |
98 |
– |
|
92 |
|
|
93 |
+ |
} |