1 |
/* |
2 |
* @test |
3 |
* @synopsis multiple producers and consumers using timeouts in blocking queues |
4 |
*/ |
5 |
/* |
6 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
7 |
* Expert Group and released to the public domain. Use, modify, and |
8 |
* redistribute this code in any way without acknowledgement. |
9 |
*/ |
10 |
|
11 |
import java.util.concurrent.*; |
12 |
|
13 |
public class TimeoutProducerConsumerLoops { |
14 |
static final int CAPACITY = 100; |
15 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
16 |
static boolean print = false; |
17 |
static int producerSum; |
18 |
static int consumerSum; |
19 |
static synchronized void addProducerSum(int x) { |
20 |
producerSum += x; |
21 |
} |
22 |
|
23 |
static synchronized void addConsumerSum(int x) { |
24 |
consumerSum += x; |
25 |
} |
26 |
|
27 |
static synchronized void checkSum() { |
28 |
if (producerSum != consumerSum) { |
29 |
throw new Error("CheckSum mismatch"); |
30 |
} |
31 |
} |
32 |
|
33 |
public static void main(String[] args) throws Exception { |
34 |
int maxPairs = 100; |
35 |
int iters = 100000; |
36 |
|
37 |
if (args.length > 0) |
38 |
maxPairs = Integer.parseInt(args[0]); |
39 |
|
40 |
print = false; |
41 |
System.out.println("Warmup..."); |
42 |
oneTest(1, 10000); |
43 |
Thread.sleep(100); |
44 |
oneTest(2, 10000); |
45 |
Thread.sleep(100); |
46 |
oneTest(2, 10000); |
47 |
Thread.sleep(100); |
48 |
print = true; |
49 |
|
50 |
int k = 1; |
51 |
for (int i = 1; i <= maxPairs;) { |
52 |
System.out.println("Pairs:" + i); |
53 |
oneTest(i, iters); |
54 |
Thread.sleep(100); |
55 |
if (i == k) { |
56 |
k = i << 1; |
57 |
i = i + (i >>> 1); |
58 |
} |
59 |
else |
60 |
i = k; |
61 |
} |
62 |
pool.shutdown(); |
63 |
} |
64 |
|
65 |
static void oneTest(int pairs, int iters) throws Exception { |
66 |
int fairIters = iters/20; |
67 |
if (print) |
68 |
System.out.print("ArrayBlockingQueue "); |
69 |
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters); |
70 |
|
71 |
if (print) |
72 |
System.out.print("LinkedBlockingQueue "); |
73 |
oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters); |
74 |
|
75 |
if (print) |
76 |
System.out.print("LinkedBlockingDeque "); |
77 |
oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters); |
78 |
|
79 |
if (print) |
80 |
System.out.print("SynchronousQueue "); |
81 |
oneRun(new SynchronousQueue<Integer>(), pairs, iters); |
82 |
|
83 |
if (print) |
84 |
System.out.print("SynchronousQueue(fair) "); |
85 |
oneRun(new SynchronousQueue<Integer>(true), pairs, fairIters); |
86 |
|
87 |
if (print) |
88 |
System.out.print("PriorityBlockingQueue "); |
89 |
oneRun(new PriorityBlockingQueue<Integer>(), pairs, fairIters); |
90 |
|
91 |
if (print) |
92 |
System.out.print("ArrayBlockingQueue(fair)"); |
93 |
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, fairIters); |
94 |
|
95 |
} |
96 |
|
97 |
static abstract class Stage implements Runnable { |
98 |
final int iters; |
99 |
final BlockingQueue<Integer> queue; |
100 |
final CyclicBarrier barrier; |
101 |
Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
102 |
queue = q; |
103 |
barrier = b; |
104 |
this.iters = iters; |
105 |
} |
106 |
} |
107 |
|
108 |
static class Producer extends Stage { |
109 |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
110 |
super(q, b, iters); |
111 |
} |
112 |
|
113 |
public void run() { |
114 |
try { |
115 |
barrier.await(); |
116 |
int s = 0; |
117 |
int l = hashCode(); |
118 |
int i = 0; |
119 |
long timeout = 1; |
120 |
while (i < iters) { |
121 |
l = LoopHelpers.compute4(l); |
122 |
if (queue.offer(new Integer(l), |
123 |
timeout, TimeUnit.NANOSECONDS)) { |
124 |
s += LoopHelpers.compute4(l); |
125 |
++i; |
126 |
if (timeout > 1) |
127 |
timeout--; |
128 |
} |
129 |
else |
130 |
timeout++; |
131 |
} |
132 |
addProducerSum(s); |
133 |
barrier.await(); |
134 |
} |
135 |
catch (Exception ie) { |
136 |
ie.printStackTrace(); |
137 |
return; |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
static class Consumer extends Stage { |
143 |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
144 |
super(q, b, iters); |
145 |
} |
146 |
|
147 |
public void run() { |
148 |
try { |
149 |
barrier.await(); |
150 |
int l = 0; |
151 |
int s = 0; |
152 |
int i = 0; |
153 |
long timeout = 1; |
154 |
while (i < iters) { |
155 |
Integer e = queue.poll(timeout, |
156 |
TimeUnit.NANOSECONDS); |
157 |
if (e != null) { |
158 |
l = LoopHelpers.compute4(e.intValue()); |
159 |
s += l; |
160 |
++i; |
161 |
if (timeout > 1) |
162 |
--timeout; |
163 |
} |
164 |
else |
165 |
++timeout; |
166 |
} |
167 |
addConsumerSum(s); |
168 |
barrier.await(); |
169 |
} |
170 |
catch (Exception ie) { |
171 |
ie.printStackTrace(); |
172 |
return; |
173 |
} |
174 |
} |
175 |
|
176 |
} |
177 |
|
178 |
static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception { |
179 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
180 |
CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); |
181 |
for (int i = 0; i < npairs; ++i) { |
182 |
pool.execute(new Producer(q, barrier, iters)); |
183 |
pool.execute(new Consumer(q, barrier, iters)); |
184 |
} |
185 |
barrier.await(); |
186 |
barrier.await(); |
187 |
long time = timer.getTime(); |
188 |
checkSum(); |
189 |
if (print) |
190 |
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer"); |
191 |
} |
192 |
|
193 |
} |