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 |
< |
import java.util.concurrent.*; |
6 |
> |
|
7 |
> |
import java.util.concurrent.BrokenBarrierException; |
8 |
> |
import java.util.concurrent.Callable; |
9 |
> |
import java.util.concurrent.CyclicBarrier; |
10 |
> |
import java.util.concurrent.ExecutorService; |
11 |
> |
import java.util.concurrent.Executors; |
12 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
13 |
> |
import java.util.concurrent.BlockingQueue; |
14 |
> |
import java.util.concurrent.Future; |
15 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
16 |
> |
import java.util.concurrent.LinkedTransferQueue; |
17 |
> |
import java.util.concurrent.SynchronousQueue; |
18 |
> |
import java.util.concurrent.TimeUnit; |
19 |
|
|
20 |
|
public class CancelledProducerConsumerLoops { |
21 |
< |
static final int CAPACITY = 100; |
21 |
> |
static final int CAPACITY = 100; |
22 |
|
static final long TIMEOUT = 100; |
23 |
|
|
24 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
38 |
|
try { |
39 |
|
oneTest(i, iters); |
40 |
|
} |
41 |
< |
catch(BrokenBarrierException bb) { |
41 |
> |
catch (BrokenBarrierException bb) { |
42 |
|
// OK, ignore |
43 |
|
} |
44 |
|
Thread.sleep(100); |
45 |
|
} |
46 |
|
pool.shutdown(); |
47 |
< |
} |
47 |
> |
} |
48 |
|
|
49 |
|
static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception { |
50 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
51 |
|
CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); |
52 |
< |
Future[] prods = new Future[npairs]; |
53 |
< |
Future[] cons = new Future[npairs]; |
52 |
> |
Future<?>[] prods = new Future<?>[npairs]; |
53 |
> |
Future<?>[] cons = new Future<?>[npairs]; |
54 |
|
|
55 |
|
for (int i = 0; i < npairs; ++i) { |
56 |
|
prods[i] = pool.submit(new Producer(q, barrier, iters)); |
84 |
|
long endTime = System.nanoTime(); |
85 |
|
long time = endTime - timer.startTime; |
86 |
|
if (print) { |
87 |
< |
double secs = (double)(time) / 1000000000.0; |
87 |
> |
double secs = (double) time / 1000000000.0; |
88 |
|
System.out.println("\t " + secs + "s run time"); |
89 |
|
} |
90 |
|
} |
107 |
|
System.out.print("SynchronousQueue "); |
108 |
|
oneRun(new SynchronousQueue<Integer>(), pairs, iters / 8); |
109 |
|
|
98 |
– |
|
110 |
|
if (print) |
111 |
|
System.out.print("SynchronousQueue(fair) "); |
112 |
|
oneRun(new SynchronousQueue<Integer>(true), pairs, iters / 8); |
118 |
|
*/ |
119 |
|
} |
120 |
|
|
121 |
< |
static abstract class Stage implements Callable { |
121 |
> |
abstract static class Stage implements Callable { |
122 |
|
final BlockingQueue<Integer> queue; |
123 |
|
final CyclicBarrier barrier; |
124 |
|
final int iters; |
125 |
< |
Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
125 |
> |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
126 |
|
queue = q; |
127 |
|
barrier = b; |
128 |
|
this.iters = iters; |
167 |
|
return new Integer(s); |
168 |
|
} |
169 |
|
} |
159 |
– |
|
160 |
– |
|
170 |
|
} |