1 |
/* |
2 |
* @test %I% %E% |
3 |
* @bug 4486658 |
4 |
* @compile -source 1.5 CancelledProducerConsumerLoops.java |
5 |
* @run main/timeout=7000 CancelledProducerConsumerLoops |
6 |
* @summary Checks for responsiveness of blocking queues to cancellation. |
7 |
* Runs under the assumption that ITERS computations require more than |
8 |
* TIMEOUT msecs to complete. |
9 |
*/ |
10 |
/* |
11 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
12 |
* Expert Group and released to the public domain. Use, modify, and |
13 |
* redistribute this code in any way without acknowledgement. |
14 |
*/ |
15 |
|
16 |
import java.util.concurrent.*; |
17 |
|
18 |
public class CancelledProducerConsumerLoops { |
19 |
static final int CAPACITY = 100; |
20 |
static final long TIMEOUT = 100; |
21 |
|
22 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
23 |
static boolean print = false; |
24 |
|
25 |
public static void main(String[] args) throws Exception { |
26 |
int maxPairs = 8; |
27 |
int iters = 1000000; |
28 |
|
29 |
if (args.length > 0) |
30 |
maxPairs = Integer.parseInt(args[0]); |
31 |
|
32 |
print = true; |
33 |
|
34 |
for (int i = 1; i <= maxPairs; i += (i+1) >>> 1) { |
35 |
System.out.println("Pairs:" + i); |
36 |
try { |
37 |
oneTest(i, iters); |
38 |
} |
39 |
catch(BrokenBarrierException bb) { |
40 |
// OK, ignore |
41 |
} |
42 |
Thread.sleep(100); |
43 |
} |
44 |
pool.shutdown(); |
45 |
} |
46 |
|
47 |
static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception { |
48 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
49 |
CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); |
50 |
Future[] prods = new Future[npairs]; |
51 |
Future[] cons = new Future[npairs]; |
52 |
|
53 |
for (int i = 0; i < npairs; ++i) { |
54 |
prods[i] = pool.submit(new Producer(q, barrier, iters)); |
55 |
cons[i] = pool.submit(new Consumer(q, barrier, iters)); |
56 |
} |
57 |
barrier.await(); |
58 |
Thread.sleep(TIMEOUT); |
59 |
boolean tooLate = false; |
60 |
|
61 |
for (int i = 1; i < npairs; ++i) { |
62 |
if (!prods[i].cancel(true)) |
63 |
tooLate = true; |
64 |
if (!cons[i].cancel(true)) |
65 |
tooLate = true; |
66 |
} |
67 |
|
68 |
Object p0 = prods[0].get(); |
69 |
Object c0 = cons[0].get(); |
70 |
|
71 |
if (!tooLate) { |
72 |
for (int i = 1; i < npairs; ++i) { |
73 |
if (!prods[i].isDone() || !prods[i].isCancelled()) |
74 |
throw new Error("Only one producer thread should complete"); |
75 |
if (!cons[i].isDone() || !cons[i].isCancelled()) |
76 |
throw new Error("Only one consumer thread should complete"); |
77 |
} |
78 |
} |
79 |
else |
80 |
System.out.print("(cancelled too late) "); |
81 |
|
82 |
long endTime = System.nanoTime(); |
83 |
long time = endTime - timer.startTime; |
84 |
if (print) { |
85 |
double secs = (double)(time) / 1000000000.0; |
86 |
System.out.println("\t " + secs + "s run time"); |
87 |
} |
88 |
} |
89 |
|
90 |
static void oneTest(int pairs, int iters) throws Exception { |
91 |
|
92 |
if (print) |
93 |
System.out.print("ArrayBlockingQueue "); |
94 |
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters); |
95 |
|
96 |
if (print) |
97 |
System.out.print("LinkedBlockingQueue "); |
98 |
oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters); |
99 |
|
100 |
|
101 |
if (print) |
102 |
System.out.print("SynchronousQueue "); |
103 |
oneRun(new SynchronousQueue<Integer>(), pairs, iters / 8); |
104 |
|
105 |
|
106 |
if (print) |
107 |
System.out.print("SynchronousQueue(fair) "); |
108 |
oneRun(new SynchronousQueue<Integer>(true), pairs, iters / 8); |
109 |
|
110 |
/* Can legitimately run out of memory before cancellation |
111 |
if (print) |
112 |
System.out.print("PriorityBlockingQueue "); |
113 |
oneRun(new PriorityBlockingQueue<Integer>(ITERS / 2 * pairs), pairs, iters / 4); |
114 |
*/ |
115 |
} |
116 |
|
117 |
static abstract class Stage implements Callable { |
118 |
final BlockingQueue<Integer> queue; |
119 |
final CyclicBarrier barrier; |
120 |
final int iters; |
121 |
Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
122 |
queue = q; |
123 |
barrier = b; |
124 |
this.iters = iters; |
125 |
} |
126 |
} |
127 |
|
128 |
static class Producer extends Stage { |
129 |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
130 |
super(q, b, iters); |
131 |
} |
132 |
|
133 |
public Object call() throws Exception { |
134 |
barrier.await(); |
135 |
int s = 0; |
136 |
int l = 4321; |
137 |
for (int i = 0; i < iters; ++i) { |
138 |
l = LoopHelpers.compute1(l); |
139 |
s += LoopHelpers.compute2(l); |
140 |
if (!queue.offer(new Integer(l), 1, TimeUnit.SECONDS)) |
141 |
break; |
142 |
} |
143 |
return new Integer(s); |
144 |
} |
145 |
} |
146 |
|
147 |
static class Consumer extends Stage { |
148 |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) { |
149 |
super(q, b, iters); |
150 |
} |
151 |
|
152 |
public Object call() throws Exception { |
153 |
barrier.await(); |
154 |
int l = 0; |
155 |
int s = 0; |
156 |
for (int i = 0; i < iters; ++i) { |
157 |
Integer x = queue.poll(1, TimeUnit.SECONDS); |
158 |
if (x == null) |
159 |
break; |
160 |
l = LoopHelpers.compute1(x.intValue()); |
161 |
s += l; |
162 |
} |
163 |
return new Integer(s); |
164 |
} |
165 |
} |
166 |
|
167 |
|
168 |
} |