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(); |
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) { |
56 |
|
} |
57 |
|
|
58 |
|
pool.shutdown(); |
59 |
< |
} |
59 |
> |
} |
60 |
|
|
61 |
|
static void warmup() throws Exception { |
62 |
|
print = false; |
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 { |
140 |
> |
abstract static class Stage implements Runnable { |
141 |
|
final int iters; |
142 |
|
final BlockingQueue<Integer> queue; |
143 |
|
final CyclicBarrier barrier; |
176 |
|
static class Consumer extends Stage { |
177 |
|
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
178 |
|
super(q, b, iters); |
173 |
– |
|
179 |
|
} |
180 |
|
|
181 |
|
public void run() { |
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 |
|
} |