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.*; |
8 |
– |
//import jsr166y.*; |
8 |
|
|
9 |
|
public class TimeoutProducerConsumerLoops { |
10 |
|
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
20 |
|
intPool[i] = Integer.valueOf(i); |
21 |
|
} |
22 |
|
|
23 |
+ |
// max lag between a producer and consumer to avoid |
24 |
+ |
// this becoming a GC test rather than queue test. |
25 |
+ |
// Used only per-pair to lessen impact on queue sync |
26 |
+ |
static final int LAG_MASK = (1 << 12) - 1; |
27 |
|
|
28 |
|
static boolean print = false; |
29 |
|
static int producerSum; |
50 |
|
maxPairs = Integer.parseInt(args[0]); |
51 |
|
|
52 |
|
print = true; |
53 |
< |
int k = 1; |
51 |
< |
for (int i = 1; i <= maxPairs;) { |
53 |
> |
for (int k = 1, i = 1; i <= maxPairs;) { |
54 |
|
System.out.println("Pairs:" + i); |
55 |
|
oneTest(i, iters); |
56 |
|
Thread.sleep(100); |
62 |
|
i = k; |
63 |
|
} |
64 |
|
pool.shutdown(); |
65 |
< |
} |
65 |
> |
} |
66 |
|
|
67 |
|
static void oneTest(int n, int iters) throws Exception { |
68 |
|
if (print) |
107 |
|
|
108 |
|
} |
109 |
|
|
110 |
< |
static abstract class Stage implements Runnable { |
110 |
> |
abstract static class Stage implements Runnable { |
111 |
|
final int iters; |
112 |
|
final BlockingQueue<Integer> queue; |
113 |
|
final CyclicBarrier barrier; |
114 |
< |
Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
114 |
> |
final Phaser lagPhaser; |
115 |
> |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
116 |
|
queue = q; |
117 |
|
barrier = b; |
118 |
+ |
lagPhaser = s; |
119 |
|
this.iters = iters; |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
|
static class Producer extends Stage { |
124 |
< |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
125 |
< |
super(q, b, iters); |
124 |
> |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
125 |
> |
super(q, b, s, iters); |
126 |
|
} |
127 |
|
|
128 |
|
public void run() { |
140 |
|
++i; |
141 |
|
if (timeout > 1) |
142 |
|
timeout--; |
143 |
+ |
if ((i & LAG_MASK) == LAG_MASK) |
144 |
+ |
lagPhaser.arriveAndAwaitAdvance(); |
145 |
|
} |
146 |
|
else |
147 |
|
timeout++; |
157 |
|
} |
158 |
|
|
159 |
|
static class Consumer extends Stage { |
160 |
< |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
161 |
< |
super(q, b, iters); |
160 |
> |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
161 |
> |
super(q, b, s, iters); |
162 |
|
} |
163 |
|
|
164 |
|
public void run() { |
177 |
|
++i; |
178 |
|
if (timeout > 1) |
179 |
|
--timeout; |
180 |
+ |
if ((i & LAG_MASK) == LAG_MASK) |
181 |
+ |
lagPhaser.arriveAndAwaitAdvance(); |
182 |
|
} |
183 |
|
else |
184 |
|
++timeout; |
198 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
199 |
|
CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); |
200 |
|
for (int i = 0; i < npairs; ++i) { |
201 |
< |
pool.execute(new Producer(q, barrier, iters)); |
202 |
< |
pool.execute(new Consumer(q, barrier, iters)); |
201 |
> |
Phaser s = new Phaser(2); |
202 |
> |
pool.execute(new Producer(q, barrier, s, iters)); |
203 |
> |
pool.execute(new Consumer(q, barrier, s, iters)); |
204 |
|
} |
205 |
|
barrier.await(); |
206 |
|
barrier.await(); |