34 |
|
static final int POOL_MASK = POOL_SIZE-1; |
35 |
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
36 |
|
static { |
37 |
< |
for (int i = 0; i < POOL_SIZE; ++i) |
37 |
> |
for (int i = 0; i < POOL_SIZE; ++i) |
38 |
|
intPool[i] = Integer.valueOf(i); |
39 |
|
} |
40 |
|
|
41 |
|
// Number of puts by producers or takes by consumers |
42 |
|
static final int ITERS = 1 << 20; |
43 |
|
|
44 |
< |
// max lag between a producer and consumer to avoid |
44 |
> |
// max lag between a producer and consumer to avoid |
45 |
|
// this becoming a GC test rather than queue test. |
46 |
|
// Used only per-pair to lessen impact on queue sync |
47 |
|
static final int LAG_MASK = (1 << 12) - 1; |
49 |
|
public static void main(String[] args) throws Exception { |
50 |
|
int maxPairs = NCPUS * 3 / 2; |
51 |
|
|
52 |
< |
if (args.length > 0) |
52 |
> |
if (args.length > 0) |
53 |
|
maxPairs = Integer.parseInt(args[0]); |
54 |
|
|
55 |
|
warmup(); |
61 |
|
if (i == k) { |
62 |
|
k = i << 1; |
63 |
|
i = i + (i >>> 1); |
64 |
< |
} |
65 |
< |
else |
64 |
> |
} |
65 |
> |
else |
66 |
|
i = k; |
67 |
|
} |
68 |
|
pool.shutdown(); |
73 |
|
System.out.print("Warmup "); |
74 |
|
int it = 2000; |
75 |
|
for (int j = 5; j > 0; --j) { |
76 |
< |
oneTest(j, it); |
76 |
> |
oneTest(j, it); |
77 |
|
System.out.print("."); |
78 |
|
it += 1000; |
79 |
|
} |
80 |
|
System.gc(); |
81 |
|
it = 20000; |
82 |
|
for (int j = 5; j > 0; --j) { |
83 |
< |
oneTest(j, it); |
83 |
> |
oneTest(j, it); |
84 |
|
System.out.print("."); |
85 |
|
it += 10000; |
86 |
|
} |
110 |
|
if (print) |
111 |
|
System.out.print("LinkedBlockingDeque "); |
112 |
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
113 |
< |
|
113 |
> |
|
114 |
|
Thread.sleep(100); // System.gc(); |
115 |
|
if (print) |
116 |
|
System.out.print("ArrayBlockingQueue "); |
121 |
|
System.out.print("SynchronousQueue "); |
122 |
|
oneRun(new SynchronousQueue<Integer>(), n, iters); |
123 |
|
|
124 |
< |
|
124 |
> |
|
125 |
|
Thread.sleep(100); // System.gc(); |
126 |
|
if (print) |
127 |
|
System.out.print("SynchronousQueue(fair) "); |
136 |
|
if (print) |
137 |
|
System.out.print("LinkedTransferQueue(half)"); |
138 |
|
oneRun(new HalfSyncLTQ<Integer>(), n, iters); |
139 |
< |
|
139 |
> |
|
140 |
|
Thread.sleep(100); // System.gc(); |
141 |
|
if (print) |
142 |
|
System.out.print("PriorityBlockingQueue "); |
148 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
149 |
|
|
150 |
|
} |
151 |
< |
|
151 |
> |
|
152 |
|
static abstract class Stage implements Runnable { |
153 |
|
final int iters; |
154 |
|
final BlockingQueue<Integer> queue; |
155 |
|
final CyclicBarrier barrier; |
156 |
|
final Phaser lagPhaser; |
157 |
< |
Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
158 |
< |
int iters) { |
159 |
< |
queue = q; |
157 |
> |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
158 |
> |
queue = q; |
159 |
|
barrier = b; |
160 |
|
lagPhaser = s; |
161 |
|
this.iters = iters; |
185 |
|
addProducerSum(ps); |
186 |
|
barrier.await(); |
187 |
|
} |
188 |
< |
catch (Exception ie) { |
189 |
< |
ie.printStackTrace(); |
190 |
< |
return; |
188 |
> |
catch (Exception ie) { |
189 |
> |
ie.printStackTrace(); |
190 |
> |
return; |
191 |
|
} |
192 |
|
} |
193 |
|
} |
194 |
|
|
195 |
|
static class Consumer extends Stage { |
196 |
|
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
197 |
< |
int iters) { |
197 |
> |
int iters) { |
198 |
|
super(q, b, s, iters); |
199 |
|
} |
200 |
|
|
212 |
|
addConsumerSum(cs); |
213 |
|
barrier.await(); |
214 |
|
} |
215 |
< |
catch (Exception ie) { |
216 |
< |
ie.printStackTrace(); |
217 |
< |
return; |
215 |
> |
catch (Exception ie) { |
216 |
> |
ie.printStackTrace(); |
217 |
> |
return; |
218 |
|
} |
219 |
|
} |
220 |
|
|
239 |
|
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
240 |
|
LTQasSQ() { super(); } |
241 |
|
public void put(T x) { |
242 |
< |
try { super.transfer(x); |
242 |
> |
try { super.transfer(x); |
243 |
|
} catch (InterruptedException ex) { throw new Error(); } |
244 |
|
} |
245 |
|
} |
251 |
|
if ((++calls & 1) == 0) |
252 |
|
super.put(x); |
253 |
|
else { |
254 |
< |
try { super.transfer(x); |
255 |
< |
} catch (InterruptedException ex) { |
256 |
< |
throw new Error(); |
254 |
> |
try { super.transfer(x); |
255 |
> |
} catch (InterruptedException ex) { |
256 |
> |
throw new Error(); |
257 |
|
} |
258 |
|
} |
259 |
|
} |