1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.2 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.7 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
import java.util.concurrent.*; |
8 |
dl |
1.3 |
//import jsr166y.*; |
9 |
dl |
1.1 |
|
10 |
|
|
public class SingleProducerMultipleConsumerLoops { |
11 |
dl |
1.3 |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
12 |
|
|
|
13 |
|
|
// Number of puts by producers or takes by consumers |
14 |
|
|
static final int ITERS = 1 << 20; |
15 |
dl |
1.1 |
|
16 |
|
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
17 |
|
|
static boolean print = false; |
18 |
|
|
|
19 |
dl |
1.3 |
// Number of elements passed around -- must be power of two |
20 |
|
|
// Elements are reused from pool to minimize alloc impact |
21 |
|
|
static final int POOL_SIZE = 1 << 8; |
22 |
|
|
static final int POOL_MASK = POOL_SIZE-1; |
23 |
|
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
24 |
|
|
static { |
25 |
jsr166 |
1.4 |
for (int i = 0; i < POOL_SIZE; ++i) |
26 |
dl |
1.3 |
intPool[i] = Integer.valueOf(i); |
27 |
|
|
} |
28 |
|
|
|
29 |
dl |
1.1 |
public static void main(String[] args) throws Exception { |
30 |
dl |
1.3 |
int maxn = 12; |
31 |
dl |
1.1 |
|
32 |
jsr166 |
1.4 |
if (args.length > 0) |
33 |
dl |
1.3 |
maxn = Integer.parseInt(args[0]); |
34 |
dl |
1.1 |
|
35 |
|
|
print = false; |
36 |
dl |
1.3 |
warmup(); |
37 |
dl |
1.1 |
print = true; |
38 |
dl |
1.3 |
|
39 |
jsr166 |
1.9 |
for (int k = 1, i = 1; i <= maxn;) { |
40 |
dl |
1.1 |
System.out.println("Consumers:" + i); |
41 |
dl |
1.3 |
oneTest(i, ITERS); |
42 |
|
|
if (i == k) { |
43 |
|
|
k = i << 1; |
44 |
|
|
i = i + (i >>> 1); |
45 |
jsr166 |
1.4 |
} |
46 |
|
|
else |
47 |
dl |
1.3 |
i = k; |
48 |
dl |
1.1 |
} |
49 |
jsr166 |
1.4 |
|
50 |
dl |
1.1 |
pool.shutdown(); |
51 |
jsr166 |
1.8 |
} |
52 |
dl |
1.1 |
|
53 |
dl |
1.3 |
static void warmup() throws Exception { |
54 |
|
|
print = false; |
55 |
|
|
System.out.print("Warmup "); |
56 |
|
|
int it = 2000; |
57 |
|
|
for (int j = 5; j > 0; --j) { |
58 |
jsr166 |
1.4 |
oneTest(j, it); |
59 |
dl |
1.3 |
System.out.print("."); |
60 |
|
|
it += 1000; |
61 |
|
|
} |
62 |
|
|
System.gc(); |
63 |
|
|
it = 20000; |
64 |
|
|
for (int j = 5; j > 0; --j) { |
65 |
jsr166 |
1.4 |
oneTest(j, it); |
66 |
dl |
1.3 |
System.out.print("."); |
67 |
|
|
it += 10000; |
68 |
|
|
} |
69 |
|
|
System.gc(); |
70 |
|
|
System.out.println(); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
static void oneTest(int n, int iters) throws Exception { |
74 |
|
|
int fairIters = iters/16; |
75 |
|
|
|
76 |
|
|
Thread.sleep(100); // System.gc(); |
77 |
dl |
1.1 |
if (print) |
78 |
dl |
1.3 |
System.out.print("LinkedTransferQueue "); |
79 |
|
|
oneRun(new LinkedTransferQueue<Integer>(), n, iters); |
80 |
dl |
1.1 |
|
81 |
dl |
1.3 |
Thread.sleep(100); // System.gc(); |
82 |
dl |
1.1 |
if (print) |
83 |
|
|
System.out.print("LinkedBlockingQueue "); |
84 |
dl |
1.3 |
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
85 |
dl |
1.1 |
|
86 |
dl |
1.3 |
Thread.sleep(100); // System.gc(); |
87 |
dl |
1.1 |
if (print) |
88 |
dl |
1.3 |
System.out.print("LinkedBlockingQueue(cap)"); |
89 |
|
|
oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters); |
90 |
|
|
|
91 |
|
|
Thread.sleep(100); // System.gc(); |
92 |
|
|
if (print) |
93 |
|
|
System.out.print("LinkedBlockingDeque "); |
94 |
|
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
95 |
jsr166 |
1.4 |
|
96 |
dl |
1.3 |
Thread.sleep(100); // System.gc(); |
97 |
|
|
if (print) |
98 |
|
|
System.out.print("ArrayBlockingQueue "); |
99 |
|
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
100 |
dl |
1.1 |
|
101 |
dl |
1.3 |
Thread.sleep(100); // System.gc(); |
102 |
dl |
1.1 |
if (print) |
103 |
|
|
System.out.print("SynchronousQueue "); |
104 |
dl |
1.3 |
oneRun(new SynchronousQueue<Integer>(), n, iters); |
105 |
jsr166 |
1.4 |
|
106 |
dl |
1.3 |
Thread.sleep(100); // System.gc(); |
107 |
|
|
if (print) |
108 |
|
|
System.out.print("SynchronousQueue(fair) "); |
109 |
|
|
oneRun(new SynchronousQueue<Integer>(true), n, iters); |
110 |
|
|
|
111 |
|
|
Thread.sleep(100); // System.gc(); |
112 |
|
|
if (print) |
113 |
|
|
System.out.print("LinkedTransferQueue(xfer)"); |
114 |
|
|
oneRun(new LTQasSQ<Integer>(), n, iters); |
115 |
dl |
1.1 |
|
116 |
dl |
1.3 |
Thread.sleep(100); // System.gc(); |
117 |
|
|
if (print) |
118 |
|
|
System.out.print("LinkedTransferQueue(half)"); |
119 |
|
|
oneRun(new HalfSyncLTQ<Integer>(), n, iters); |
120 |
jsr166 |
1.4 |
|
121 |
dl |
1.3 |
Thread.sleep(100); // System.gc(); |
122 |
|
|
if (print) |
123 |
|
|
System.out.print("PriorityBlockingQueue "); |
124 |
|
|
oneRun(new PriorityBlockingQueue<Integer>(), n, fairIters); |
125 |
|
|
|
126 |
|
|
Thread.sleep(100); // System.gc(); |
127 |
dl |
1.1 |
if (print) |
128 |
|
|
System.out.print("ArrayBlockingQueue(fair)"); |
129 |
dl |
1.3 |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
130 |
|
|
|
131 |
dl |
1.1 |
} |
132 |
jsr166 |
1.4 |
|
133 |
jsr166 |
1.6 |
abstract static class Stage implements Runnable { |
134 |
dl |
1.1 |
final int iters; |
135 |
|
|
final BlockingQueue<Integer> queue; |
136 |
|
|
final CyclicBarrier barrier; |
137 |
|
|
volatile int result; |
138 |
jsr166 |
1.5 |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
139 |
jsr166 |
1.4 |
queue = q; |
140 |
dl |
1.1 |
barrier = b; |
141 |
|
|
this.iters = iters; |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
static class Producer extends Stage { |
146 |
|
|
Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
147 |
|
|
super(q, b, iters); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
public void run() { |
151 |
|
|
try { |
152 |
|
|
barrier.await(); |
153 |
dl |
1.3 |
int r = hashCode(); |
154 |
dl |
1.1 |
for (int i = 0; i < iters; ++i) { |
155 |
dl |
1.3 |
r = LoopHelpers.compute7(r); |
156 |
|
|
Integer v = intPool[r & POOL_MASK]; |
157 |
|
|
queue.put(v); |
158 |
dl |
1.1 |
} |
159 |
|
|
barrier.await(); |
160 |
|
|
result = 432; |
161 |
|
|
} |
162 |
jsr166 |
1.4 |
catch (Exception ie) { |
163 |
|
|
ie.printStackTrace(); |
164 |
|
|
return; |
165 |
dl |
1.1 |
} |
166 |
|
|
} |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
static class Consumer extends Stage { |
170 |
jsr166 |
1.4 |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
171 |
dl |
1.1 |
super(q, b, iters); |
172 |
dl |
1.3 |
|
173 |
dl |
1.1 |
} |
174 |
|
|
|
175 |
|
|
public void run() { |
176 |
|
|
try { |
177 |
|
|
barrier.await(); |
178 |
|
|
int l = 0; |
179 |
|
|
int s = 0; |
180 |
|
|
for (int i = 0; i < iters; ++i) { |
181 |
|
|
Integer item = queue.take(); |
182 |
dl |
1.3 |
s += item.intValue(); |
183 |
dl |
1.1 |
} |
184 |
|
|
barrier.await(); |
185 |
|
|
result = s; |
186 |
dl |
1.3 |
if (s == 0) System.out.print(" "); |
187 |
dl |
1.1 |
} |
188 |
jsr166 |
1.4 |
catch (Exception ie) { |
189 |
|
|
ie.printStackTrace(); |
190 |
|
|
return; |
191 |
dl |
1.1 |
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
static void oneRun(BlockingQueue<Integer> q, int nconsumers, int iters) throws Exception { |
197 |
|
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
198 |
|
|
CyclicBarrier barrier = new CyclicBarrier(nconsumers + 2, timer); |
199 |
|
|
pool.execute(new Producer(q, barrier, iters * nconsumers)); |
200 |
|
|
for (int i = 0; i < nconsumers; ++i) { |
201 |
|
|
pool.execute(new Consumer(q, barrier, iters)); |
202 |
|
|
} |
203 |
|
|
barrier.await(); |
204 |
|
|
barrier.await(); |
205 |
|
|
long time = timer.getTime(); |
206 |
|
|
if (print) |
207 |
|
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * nconsumers)) + " ns per transfer"); |
208 |
|
|
} |
209 |
|
|
|
210 |
dl |
1.3 |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
211 |
|
|
LTQasSQ() { super(); } |
212 |
|
|
public void put(T x) { |
213 |
jsr166 |
1.4 |
try { super.transfer(x); |
214 |
dl |
1.3 |
} catch (InterruptedException ex) { throw new Error(); } |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> { |
219 |
|
|
int calls; |
220 |
|
|
HalfSyncLTQ() { super(); } |
221 |
|
|
public void put(T x) { |
222 |
|
|
if ((++calls & 1) == 0) |
223 |
|
|
super.put(x); |
224 |
|
|
else { |
225 |
jsr166 |
1.4 |
try { super.transfer(x); |
226 |
|
|
} catch (InterruptedException ex) { |
227 |
|
|
throw new Error(); |
228 |
dl |
1.3 |
} |
229 |
|
|
} |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
|
233 |
dl |
1.1 |
} |