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 maxN = NCPUS * 3 / 2; |
51 |
|
|
52 |
< |
if (args.length > 0) |
52 |
> |
if (args.length > 0) |
53 |
|
maxN = 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 |
|
} |
103 |
|
|
104 |
|
Thread.sleep(100); // System.gc(); |
105 |
|
if (print) |
106 |
+ |
System.out.print("ConcurrentLinkedDeque "); |
107 |
+ |
oneRun(new ConcurrentLinkedDeque<Integer>(), n, iters); |
108 |
+ |
|
109 |
+ |
Thread.sleep(100); // System.gc(); |
110 |
+ |
if (print) |
111 |
|
System.out.print("LinkedBlockingQueue "); |
112 |
|
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
113 |
|
|
120 |
|
if (print) |
121 |
|
System.out.print("LinkedBlockingDeque "); |
122 |
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
123 |
< |
|
123 |
> |
|
124 |
|
Thread.sleep(100); // System.gc(); |
125 |
|
if (print) |
126 |
|
System.out.print("ArrayBlockingQueue "); |
138 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
139 |
|
|
140 |
|
} |
141 |
< |
|
141 |
> |
|
142 |
|
static abstract class Stage implements Runnable { |
143 |
|
final int iters; |
144 |
|
final Queue<Integer> queue; |
145 |
|
final CyclicBarrier barrier; |
146 |
|
final Phaser lagPhaser; |
147 |
< |
Stage (Queue<Integer> q, CyclicBarrier b, Phaser s, |
148 |
< |
int iters) { |
144 |
< |
queue = q; |
147 |
> |
Stage(Queue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
148 |
> |
queue = q; |
149 |
|
barrier = b; |
150 |
|
lagPhaser = s; |
151 |
|
this.iters = iters; |
175 |
|
break; |
176 |
|
if ((i & LAG_MASK) == LAG_MASK) |
177 |
|
lagPhaser.arriveAndAwaitAdvance(); |
178 |
< |
} |
178 |
> |
} |
179 |
|
} |
180 |
|
addProducerSum(ps); |
181 |
|
barrier.await(); |
182 |
|
} |
183 |
< |
catch (Exception ie) { |
184 |
< |
ie.printStackTrace(); |
185 |
< |
return; |
183 |
> |
catch (Exception ie) { |
184 |
> |
ie.printStackTrace(); |
185 |
> |
return; |
186 |
|
} |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
static class Consumer extends Stage { |
191 |
|
Consumer(Queue<Integer> q, CyclicBarrier b, Phaser s, |
192 |
< |
int iters) { |
192 |
> |
int iters) { |
193 |
|
super(q, b, s, iters); |
194 |
|
} |
195 |
|
|
213 |
|
addConsumerSum(cs); |
214 |
|
barrier.await(); |
215 |
|
} |
216 |
< |
catch (Exception ie) { |
217 |
< |
ie.printStackTrace(); |
218 |
< |
return; |
216 |
> |
catch (Exception ie) { |
217 |
> |
ie.printStackTrace(); |
218 |
> |
return; |
219 |
|
} |
220 |
|
} |
221 |
|
|
239 |
|
|
240 |
|
|
241 |
|
} |
238 |
– |
|