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.*; |
7 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
8 |
> |
import java.util.concurrent.BlockingQueue; |
9 |
> |
import java.util.concurrent.CyclicBarrier; |
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.PriorityBlockingQueue; |
16 |
> |
import java.util.concurrent.SynchronousQueue; |
17 |
|
|
18 |
|
public class SingleProducerMultipleConsumerLoops { |
19 |
|
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
30 |
|
static final int POOL_MASK = POOL_SIZE-1; |
31 |
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
32 |
|
static { |
33 |
< |
for (int i = 0; i < POOL_SIZE; ++i) |
33 |
> |
for (int i = 0; i < POOL_SIZE; ++i) |
34 |
|
intPool[i] = Integer.valueOf(i); |
35 |
|
} |
36 |
|
|
37 |
|
public static void main(String[] args) throws Exception { |
38 |
|
int maxn = 12; |
39 |
|
|
40 |
< |
if (args.length > 0) |
40 |
> |
if (args.length > 0) |
41 |
|
maxn = Integer.parseInt(args[0]); |
42 |
|
|
43 |
|
print = false; |
44 |
|
warmup(); |
45 |
|
print = true; |
46 |
|
|
47 |
< |
int k = 1; |
40 |
< |
for (int i = 1; i <= maxn;) { |
47 |
> |
for (int k = 1, i = 1; i <= maxn;) { |
48 |
|
System.out.println("Consumers:" + i); |
49 |
|
oneTest(i, ITERS); |
50 |
|
if (i == k) { |
51 |
|
k = i << 1; |
52 |
|
i = i + (i >>> 1); |
53 |
< |
} |
54 |
< |
else |
53 |
> |
} |
54 |
> |
else |
55 |
|
i = k; |
56 |
|
} |
57 |
< |
|
57 |
> |
|
58 |
|
pool.shutdown(); |
59 |
< |
} |
59 |
> |
} |
60 |
|
|
61 |
|
static void warmup() throws Exception { |
62 |
|
print = false; |
63 |
|
System.out.print("Warmup "); |
64 |
|
int it = 2000; |
65 |
|
for (int j = 5; j > 0; --j) { |
66 |
< |
oneTest(j, it); |
66 |
> |
oneTest(j, it); |
67 |
|
System.out.print("."); |
68 |
|
it += 1000; |
69 |
|
} |
70 |
|
System.gc(); |
71 |
|
it = 20000; |
72 |
|
for (int j = 5; j > 0; --j) { |
73 |
< |
oneTest(j, it); |
73 |
> |
oneTest(j, it); |
74 |
|
System.out.print("."); |
75 |
|
it += 10000; |
76 |
|
} |
100 |
|
if (print) |
101 |
|
System.out.print("LinkedBlockingDeque "); |
102 |
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
103 |
< |
|
103 |
> |
|
104 |
|
Thread.sleep(100); // System.gc(); |
105 |
|
if (print) |
106 |
|
System.out.print("ArrayBlockingQueue "); |
110 |
|
if (print) |
111 |
|
System.out.print("SynchronousQueue "); |
112 |
|
oneRun(new SynchronousQueue<Integer>(), n, iters); |
113 |
< |
|
113 |
> |
|
114 |
|
Thread.sleep(100); // System.gc(); |
115 |
|
if (print) |
116 |
|
System.out.print("SynchronousQueue(fair) "); |
125 |
|
if (print) |
126 |
|
System.out.print("LinkedTransferQueue(half)"); |
127 |
|
oneRun(new HalfSyncLTQ<Integer>(), n, iters); |
128 |
< |
|
128 |
> |
|
129 |
|
Thread.sleep(100); // System.gc(); |
130 |
|
if (print) |
131 |
|
System.out.print("PriorityBlockingQueue "); |
135 |
|
if (print) |
136 |
|
System.out.print("ArrayBlockingQueue(fair)"); |
137 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
131 |
– |
|
138 |
|
} |
139 |
< |
|
140 |
< |
static abstract class Stage implements Runnable { |
139 |
> |
|
140 |
> |
abstract static class Stage implements Runnable { |
141 |
|
final int iters; |
142 |
|
final BlockingQueue<Integer> queue; |
143 |
|
final CyclicBarrier barrier; |
144 |
|
volatile int result; |
145 |
< |
Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
146 |
< |
queue = q; |
145 |
> |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
146 |
> |
queue = q; |
147 |
|
barrier = b; |
148 |
|
this.iters = iters; |
149 |
|
} |
166 |
|
barrier.await(); |
167 |
|
result = 432; |
168 |
|
} |
169 |
< |
catch (Exception ie) { |
170 |
< |
ie.printStackTrace(); |
171 |
< |
return; |
169 |
> |
catch (Exception ie) { |
170 |
> |
ie.printStackTrace(); |
171 |
> |
return; |
172 |
|
} |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
static class Consumer extends Stage { |
177 |
< |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
177 |
> |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
178 |
|
super(q, b, iters); |
173 |
– |
|
179 |
|
} |
180 |
|
|
181 |
|
public void run() { |
191 |
|
result = s; |
192 |
|
if (s == 0) System.out.print(" "); |
193 |
|
} |
194 |
< |
catch (Exception ie) { |
195 |
< |
ie.printStackTrace(); |
196 |
< |
return; |
194 |
> |
catch (Exception ie) { |
195 |
> |
ie.printStackTrace(); |
196 |
> |
return; |
197 |
|
} |
198 |
|
} |
199 |
|
|
216 |
|
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
217 |
|
LTQasSQ() { super(); } |
218 |
|
public void put(T x) { |
219 |
< |
try { super.transfer(x); |
220 |
< |
} catch (InterruptedException ex) { throw new Error(); } |
219 |
> |
try { super.transfer(x); } |
220 |
> |
catch (InterruptedException ex) { throw new Error(ex); } |
221 |
|
} |
222 |
|
} |
223 |
|
|
228 |
|
if ((++calls & 1) == 0) |
229 |
|
super.put(x); |
230 |
|
else { |
231 |
< |
try { super.transfer(x); |
232 |
< |
} catch (InterruptedException ex) { |
228 |
< |
throw new Error(); |
229 |
< |
} |
231 |
> |
try { super.transfer(x); } |
232 |
> |
catch (InterruptedException ex) { throw new Error(ex); } |
233 |
|
} |
234 |
|
} |
235 |
|
} |