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 |
< |
|
7 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
8 |
> |
import java.util.concurrent.CyclicBarrier; |
9 |
> |
import java.util.concurrent.BlockingQueue; |
10 |
> |
import java.util.concurrent.ExecutorService; |
11 |
> |
import java.util.concurrent.Executors; |
12 |
> |
import java.util.concurrent.LinkedBlockingDeque; |
13 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
14 |
> |
import java.util.concurrent.LinkedTransferQueue; |
15 |
> |
import java.util.concurrent.Phaser; |
16 |
> |
import java.util.concurrent.PriorityBlockingQueue; |
17 |
> |
import java.util.concurrent.SynchronousQueue; |
18 |
> |
import java.util.concurrent.TimeUnit; |
19 |
|
|
20 |
|
public class TimeoutProducerConsumerLoops { |
21 |
|
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
27 |
|
static final int POOL_MASK = POOL_SIZE-1; |
28 |
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
29 |
|
static { |
30 |
< |
for (int i = 0; i < POOL_SIZE; ++i) |
30 |
> |
for (int i = 0; i < POOL_SIZE; ++i) |
31 |
|
intPool[i] = Integer.valueOf(i); |
32 |
|
} |
33 |
|
|
34 |
< |
// max lag between a producer and consumer to avoid |
34 |
> |
// max lag between a producer and consumer to avoid |
35 |
|
// this becoming a GC test rather than queue test. |
36 |
|
// Used only per-pair to lessen impact on queue sync |
37 |
|
static final int LAG_MASK = (1 << 12) - 1; |
57 |
|
int maxPairs = NCPUS * 3 / 2; |
58 |
|
int iters = 1000000; |
59 |
|
|
60 |
< |
if (args.length > 0) |
60 |
> |
if (args.length > 0) |
61 |
|
maxPairs = Integer.parseInt(args[0]); |
62 |
|
|
63 |
|
print = true; |
64 |
< |
int k = 1; |
55 |
< |
for (int i = 1; i <= maxPairs;) { |
64 |
> |
for (int k = 1, i = 1; i <= maxPairs;) { |
65 |
|
System.out.println("Pairs:" + i); |
66 |
|
oneTest(i, iters); |
67 |
|
Thread.sleep(100); |
68 |
|
if (i == k) { |
69 |
|
k = i << 1; |
70 |
|
i = i + (i >>> 1); |
71 |
< |
} |
72 |
< |
else |
71 |
> |
} |
72 |
> |
else |
73 |
|
i = k; |
74 |
|
} |
75 |
|
pool.shutdown(); |
76 |
< |
} |
76 |
> |
} |
77 |
|
|
78 |
|
static void oneTest(int n, int iters) throws Exception { |
79 |
|
if (print) |
115 |
|
if (print) |
116 |
|
System.out.print("ArrayBlockingQueue(fair) "); |
117 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16); |
109 |
– |
|
118 |
|
} |
119 |
< |
|
120 |
< |
static abstract class Stage implements Runnable { |
119 |
> |
|
120 |
> |
abstract static class Stage implements Runnable { |
121 |
|
final int iters; |
122 |
|
final BlockingQueue<Integer> queue; |
123 |
|
final CyclicBarrier barrier; |
124 |
|
final Phaser lagPhaser; |
125 |
< |
Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
126 |
< |
queue = q; |
125 |
> |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
126 |
> |
queue = q; |
127 |
|
barrier = b; |
128 |
|
lagPhaser = s; |
129 |
|
this.iters = iters; |
159 |
|
addProducerSum(s); |
160 |
|
barrier.await(); |
161 |
|
} |
162 |
< |
catch (Exception ie) { |
163 |
< |
ie.printStackTrace(); |
164 |
< |
return; |
162 |
> |
catch (Exception ie) { |
163 |
> |
ie.printStackTrace(); |
164 |
> |
return; |
165 |
|
} |
166 |
|
} |
167 |
|
} |
168 |
|
|
169 |
|
static class Consumer extends Stage { |
170 |
< |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
170 |
> |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
171 |
|
super(q, b, s, iters); |
172 |
|
} |
173 |
|
|
179 |
|
int i = 0; |
180 |
|
long timeout = 1000; |
181 |
|
while (i < iters) { |
182 |
< |
Integer e = queue.poll(timeout, |
182 |
> |
Integer e = queue.poll(timeout, |
183 |
|
TimeUnit.NANOSECONDS); |
184 |
|
if (e != null) { |
185 |
|
l = LoopHelpers.compute4(e.intValue()); |
196 |
|
addConsumerSum(s); |
197 |
|
barrier.await(); |
198 |
|
} |
199 |
< |
catch (Exception ie) { |
200 |
< |
ie.printStackTrace(); |
201 |
< |
return; |
199 |
> |
catch (Exception ie) { |
200 |
> |
ie.printStackTrace(); |
201 |
> |
return; |
202 |
|
} |
203 |
|
} |
204 |
|
|
224 |
|
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
225 |
|
LTQasSQ() { super(); } |
226 |
|
public void put(T x) { |
227 |
< |
try { super.transfer(x); |
227 |
> |
try { super.transfer(x); |
228 |
|
} catch (InterruptedException ex) { throw new Error(); } |
229 |
|
} |
230 |
|
|
231 |
|
public boolean offer(T x, long timeout, TimeUnit unit) { |
232 |
< |
return super.offer(x, timeout, unit); |
232 |
> |
return super.offer(x, timeout, unit); |
233 |
|
} |
234 |
|
|
235 |
|
} |