4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
|
7 |
|
import java.util.*; |
8 |
|
import java.util.concurrent.*; |
9 |
|
|
18 |
|
|
19 |
|
static final Phaser phaser = new Phaser(CONSUMERS + 1); |
20 |
|
|
21 |
< |
static class Sub implements Flow.Subscriber<Boolean> { |
21 |
> |
static final class Sub implements Flow.Subscriber<Boolean> { |
22 |
|
Flow.Subscription sn; |
23 |
|
int count; |
24 |
< |
public void onSubscribe(Flow.Subscription s) { |
25 |
< |
(sn = s).request(CAP); |
24 |
> |
public void onSubscribe(Flow.Subscription s) { |
25 |
> |
(sn = s).request(CAP); |
26 |
|
} |
27 |
< |
public void onNext(Boolean t) { |
27 |
> |
public void onNext(Boolean t) { |
28 |
|
if ((++count & (CAP - 1)) == (CAP >>> 1)) |
29 |
|
sn.request(CAP); |
30 |
|
} |
31 |
|
public void onError(Throwable t) { t.printStackTrace(); } |
32 |
< |
public void onComplete() { |
32 |
> |
public void onComplete() { |
33 |
|
if (count != ITEMS) |
34 |
|
System.out.println("Error: remaining " + (ITEMS - count)); |
35 |
< |
phaser.arrive(); |
35 |
> |
phaser.arrive(); |
36 |
|
} |
37 |
|
} |
38 |
|
|
39 |
|
static final class Pub extends RecursiveAction { |
40 |
< |
final SubmissionPublisher<Boolean> pub = |
40 |
> |
final SubmissionPublisher<Boolean> pub = |
41 |
|
new SubmissionPublisher<Boolean>(ForkJoinPool.commonPool(), CAP); |
42 |
|
public void compute() { |
43 |
|
SubmissionPublisher<Boolean> p = pub; |
44 |
|
for (int i = 0; i < CONSUMERS; ++i) |
45 |
|
p.subscribe(new Sub()); |
46 |
< |
for (int i = 0; i < ITEMS; ++i) |
46 |
> |
for (int i = 0; i < ITEMS; ++i) |
47 |
|
p.submit(Boolean.TRUE); |
48 |
|
p.close(); |
49 |
|
} |
55 |
|
int reps = REPS; |
56 |
|
if (args.length > 0) |
57 |
|
reps = Integer.parseInt(args[0]); |
58 |
< |
|
59 |
< |
System.out.println("ITEMS: " + ITEMS + |
58 |
> |
|
59 |
> |
System.out.println("ITEMS: " + ITEMS + |
60 |
|
" CONSUMERS: " + CONSUMERS + |
61 |
|
" CAP: " + CAP); |
63 |
– |
|
62 |
|
for (int rep = 0; rep < reps; ++rep) { |
63 |
< |
long startTime = System.nanoTime(); |
64 |
< |
new Pub().fork(); |
67 |
< |
phaser.arriveAndAwaitAdvance(); |
68 |
< |
long elapsed = System.nanoTime() - startTime; |
69 |
< |
double secs = ((double)elapsed) / NPS; |
70 |
< |
System.out.printf("\tTime: %7.3f\n", secs); |
63 |
> |
oneRun(); |
64 |
> |
Thread.sleep(1000); |
65 |
|
} |
66 |
|
} |
67 |
+ |
|
68 |
+ |
static void oneRun() throws Exception { |
69 |
+ |
long startTime = System.nanoTime(); |
70 |
+ |
new Pub().fork(); |
71 |
+ |
phaser.arriveAndAwaitAdvance(); |
72 |
+ |
long elapsed = System.nanoTime() - startTime; |
73 |
+ |
double secs = ((double)elapsed) / NPS; |
74 |
+ |
System.out.printf("\tTime: %7.3f\n", secs); |
75 |
+ |
} |
76 |
+ |
|
77 |
|
} |