1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
jsr166 |
1.17 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.9 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
jsr166 |
1.24 |
import java.util.concurrent.BrokenBarrierException; |
12 |
|
|
import java.util.concurrent.CountDownLatch; |
13 |
|
|
import java.util.concurrent.CyclicBarrier; |
14 |
|
|
import java.util.concurrent.TimeoutException; |
15 |
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
jsr166 |
1.13 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
dl |
1.1 |
|
18 |
jsr166 |
1.11 |
public class CyclicBarrierTest extends JSR166TestCase { |
19 |
dl |
1.1 |
public static void main(String[] args) { |
20 |
jsr166 |
1.15 |
junit.textui.TestRunner.run(suite()); |
21 |
dl |
1.1 |
} |
22 |
|
|
public static Test suite() { |
23 |
jsr166 |
1.12 |
return new TestSuite(CyclicBarrierTest.class); |
24 |
dl |
1.1 |
} |
25 |
dl |
1.3 |
|
26 |
|
|
private volatile int countAction; |
27 |
|
|
private class MyAction implements Runnable { |
28 |
|
|
public void run() { ++countAction; } |
29 |
|
|
} |
30 |
jsr166 |
1.9 |
|
31 |
dl |
1.3 |
/** |
32 |
jsr166 |
1.23 |
* Spin-waits till the number of waiters == numberOfWaiters. |
33 |
|
|
*/ |
34 |
|
|
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
35 |
|
|
long startTime = System.nanoTime(); |
36 |
|
|
while (barrier.getNumberWaiting() != numberOfWaiters) { |
37 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
38 |
|
|
fail("timed out"); |
39 |
|
|
Thread.yield(); |
40 |
|
|
} |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
/** |
44 |
dl |
1.4 |
* Creating with negative parties throws IAE |
45 |
dl |
1.3 |
*/ |
46 |
|
|
public void testConstructor1() { |
47 |
|
|
try { |
48 |
dl |
1.1 |
new CyclicBarrier(-1, (Runnable)null); |
49 |
dl |
1.3 |
shouldThrow(); |
50 |
jsr166 |
1.14 |
} catch (IllegalArgumentException success) {} |
51 |
dl |
1.1 |
} |
52 |
|
|
|
53 |
dl |
1.3 |
/** |
54 |
dl |
1.4 |
* Creating with negative parties and no action throws IAE |
55 |
dl |
1.3 |
*/ |
56 |
|
|
public void testConstructor2() { |
57 |
|
|
try { |
58 |
dl |
1.1 |
new CyclicBarrier(-1); |
59 |
dl |
1.3 |
shouldThrow(); |
60 |
jsr166 |
1.14 |
} catch (IllegalArgumentException success) {} |
61 |
dl |
1.1 |
} |
62 |
|
|
|
63 |
dl |
1.3 |
/** |
64 |
dl |
1.4 |
* getParties returns the number of parties given in constructor |
65 |
dl |
1.3 |
*/ |
66 |
dl |
1.4 |
public void testGetParties() { |
67 |
dl |
1.1 |
CyclicBarrier b = new CyclicBarrier(2); |
68 |
jsr166 |
1.12 |
assertEquals(2, b.getParties()); |
69 |
dl |
1.1 |
assertEquals(0, b.getNumberWaiting()); |
70 |
|
|
} |
71 |
|
|
|
72 |
dl |
1.3 |
/** |
73 |
dl |
1.5 |
* A 1-party barrier triggers after single await |
74 |
dl |
1.3 |
*/ |
75 |
jsr166 |
1.14 |
public void testSingleParty() throws Exception { |
76 |
|
|
CyclicBarrier b = new CyclicBarrier(1); |
77 |
|
|
assertEquals(1, b.getParties()); |
78 |
|
|
assertEquals(0, b.getNumberWaiting()); |
79 |
|
|
b.await(); |
80 |
|
|
b.await(); |
81 |
|
|
assertEquals(0, b.getNumberWaiting()); |
82 |
dl |
1.1 |
} |
83 |
jsr166 |
1.9 |
|
84 |
dl |
1.3 |
/** |
85 |
dl |
1.4 |
* The supplied barrier action is run at barrier |
86 |
dl |
1.3 |
*/ |
87 |
jsr166 |
1.14 |
public void testBarrierAction() throws Exception { |
88 |
|
|
countAction = 0; |
89 |
|
|
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
90 |
|
|
assertEquals(1, b.getParties()); |
91 |
|
|
assertEquals(0, b.getNumberWaiting()); |
92 |
|
|
b.await(); |
93 |
|
|
b.await(); |
94 |
|
|
assertEquals(0, b.getNumberWaiting()); |
95 |
|
|
assertEquals(countAction, 2); |
96 |
dl |
1.1 |
} |
97 |
|
|
|
98 |
dl |
1.3 |
/** |
99 |
dl |
1.5 |
* A 2-party/thread barrier triggers after both threads invoke await |
100 |
dl |
1.3 |
*/ |
101 |
jsr166 |
1.14 |
public void testTwoParties() throws Exception { |
102 |
dl |
1.1 |
final CyclicBarrier b = new CyclicBarrier(2); |
103 |
jsr166 |
1.21 |
Thread t = newStartedThread(new CheckedRunnable() { |
104 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
105 |
|
|
b.await(); |
106 |
|
|
b.await(); |
107 |
|
|
b.await(); |
108 |
|
|
b.await(); |
109 |
|
|
}}); |
110 |
|
|
|
111 |
|
|
b.await(); |
112 |
|
|
b.await(); |
113 |
|
|
b.await(); |
114 |
|
|
b.await(); |
115 |
jsr166 |
1.21 |
awaitTermination(t); |
116 |
dl |
1.1 |
} |
117 |
|
|
|
118 |
dl |
1.3 |
/** |
119 |
dl |
1.4 |
* An interruption in one party causes others waiting in await to |
120 |
|
|
* throw BrokenBarrierException |
121 |
dl |
1.3 |
*/ |
122 |
jsr166 |
1.21 |
public void testAwait1_Interrupted_BrokenBarrier() { |
123 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(3); |
124 |
jsr166 |
1.21 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
125 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
126 |
|
|
public void realRun() throws Exception { |
127 |
jsr166 |
1.21 |
pleaseInterrupt.countDown(); |
128 |
jsr166 |
1.14 |
c.await(); |
129 |
|
|
}}; |
130 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
131 |
|
|
public void realRun() throws Exception { |
132 |
jsr166 |
1.21 |
pleaseInterrupt.countDown(); |
133 |
jsr166 |
1.14 |
c.await(); |
134 |
|
|
}}; |
135 |
|
|
|
136 |
|
|
t1.start(); |
137 |
|
|
t2.start(); |
138 |
jsr166 |
1.21 |
await(pleaseInterrupt); |
139 |
jsr166 |
1.14 |
t1.interrupt(); |
140 |
jsr166 |
1.21 |
awaitTermination(t1); |
141 |
|
|
awaitTermination(t2); |
142 |
dl |
1.1 |
} |
143 |
|
|
|
144 |
dl |
1.3 |
/** |
145 |
dl |
1.4 |
* An interruption in one party causes others waiting in timed await to |
146 |
|
|
* throw BrokenBarrierException |
147 |
dl |
1.3 |
*/ |
148 |
jsr166 |
1.14 |
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
149 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
150 |
jsr166 |
1.21 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
151 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
152 |
|
|
public void realRun() throws Exception { |
153 |
jsr166 |
1.21 |
pleaseInterrupt.countDown(); |
154 |
jsr166 |
1.14 |
c.await(LONG_DELAY_MS, MILLISECONDS); |
155 |
|
|
}}; |
156 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
157 |
|
|
public void realRun() throws Exception { |
158 |
jsr166 |
1.21 |
pleaseInterrupt.countDown(); |
159 |
jsr166 |
1.14 |
c.await(LONG_DELAY_MS, MILLISECONDS); |
160 |
|
|
}}; |
161 |
|
|
|
162 |
|
|
t1.start(); |
163 |
|
|
t2.start(); |
164 |
jsr166 |
1.21 |
await(pleaseInterrupt); |
165 |
jsr166 |
1.14 |
t1.interrupt(); |
166 |
jsr166 |
1.21 |
awaitTermination(t1); |
167 |
|
|
awaitTermination(t2); |
168 |
dl |
1.1 |
} |
169 |
jsr166 |
1.9 |
|
170 |
dl |
1.3 |
/** |
171 |
dl |
1.4 |
* A timeout in timed await throws TimeoutException |
172 |
dl |
1.3 |
*/ |
173 |
jsr166 |
1.19 |
public void testAwait3_TimeoutException() throws InterruptedException { |
174 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(2); |
175 |
jsr166 |
1.21 |
Thread t = newStartedThread(new CheckedRunnable() { |
176 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
177 |
jsr166 |
1.21 |
long startTime = System.nanoTime(); |
178 |
|
|
try { |
179 |
|
|
c.await(timeoutMillis(), MILLISECONDS); |
180 |
|
|
shouldThrow(); |
181 |
|
|
} catch (TimeoutException success) {} |
182 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
183 |
|
|
}}); |
184 |
jsr166 |
1.9 |
|
185 |
jsr166 |
1.21 |
awaitTermination(t); |
186 |
dl |
1.5 |
} |
187 |
|
|
|
188 |
|
|
/** |
189 |
|
|
* A timeout in one party causes others waiting in timed await to |
190 |
|
|
* throw BrokenBarrierException |
191 |
|
|
*/ |
192 |
jsr166 |
1.14 |
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
193 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
194 |
jsr166 |
1.21 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
195 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
196 |
jsr166 |
1.21 |
try { |
197 |
|
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
198 |
|
|
shouldThrow(); |
199 |
|
|
} catch (BrokenBarrierException success) {} |
200 |
|
|
}}); |
201 |
|
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
202 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
203 |
jsr166 |
1.23 |
awaitNumberWaiting(c, 1); |
204 |
jsr166 |
1.21 |
long startTime = System.nanoTime(); |
205 |
|
|
try { |
206 |
|
|
c.await(timeoutMillis(), MILLISECONDS); |
207 |
|
|
shouldThrow(); |
208 |
|
|
} catch (TimeoutException success) {} |
209 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
210 |
|
|
}}); |
211 |
jsr166 |
1.14 |
|
212 |
jsr166 |
1.21 |
awaitTermination(t1); |
213 |
|
|
awaitTermination(t2); |
214 |
dl |
1.5 |
} |
215 |
|
|
|
216 |
|
|
/** |
217 |
|
|
* A timeout in one party causes others waiting in await to |
218 |
|
|
* throw BrokenBarrierException |
219 |
|
|
*/ |
220 |
jsr166 |
1.14 |
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
221 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
222 |
jsr166 |
1.21 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
223 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
224 |
jsr166 |
1.21 |
try { |
225 |
|
|
c.await(); |
226 |
|
|
shouldThrow(); |
227 |
|
|
} catch (BrokenBarrierException success) {} |
228 |
|
|
}}); |
229 |
|
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
230 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
231 |
jsr166 |
1.23 |
awaitNumberWaiting(c, 1); |
232 |
jsr166 |
1.21 |
long startTime = System.nanoTime(); |
233 |
|
|
try { |
234 |
|
|
c.await(timeoutMillis(), MILLISECONDS); |
235 |
|
|
shouldThrow(); |
236 |
|
|
} catch (TimeoutException success) {} |
237 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
238 |
|
|
}}); |
239 |
jsr166 |
1.14 |
|
240 |
jsr166 |
1.21 |
awaitTermination(t1); |
241 |
|
|
awaitTermination(t2); |
242 |
dl |
1.1 |
} |
243 |
jsr166 |
1.9 |
|
244 |
dl |
1.4 |
/** |
245 |
|
|
* A reset of an active barrier causes waiting threads to throw |
246 |
|
|
* BrokenBarrierException |
247 |
|
|
*/ |
248 |
jsr166 |
1.14 |
public void testReset_BrokenBarrier() throws InterruptedException { |
249 |
dl |
1.4 |
final CyclicBarrier c = new CyclicBarrier(3); |
250 |
jsr166 |
1.21 |
final CountDownLatch pleaseReset = new CountDownLatch(2); |
251 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
252 |
|
|
public void realRun() throws Exception { |
253 |
jsr166 |
1.21 |
pleaseReset.countDown(); |
254 |
jsr166 |
1.14 |
c.await(); |
255 |
|
|
}}; |
256 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
257 |
|
|
public void realRun() throws Exception { |
258 |
jsr166 |
1.21 |
pleaseReset.countDown(); |
259 |
jsr166 |
1.14 |
c.await(); |
260 |
|
|
}}; |
261 |
|
|
|
262 |
|
|
t1.start(); |
263 |
|
|
t2.start(); |
264 |
jsr166 |
1.21 |
await(pleaseReset); |
265 |
jsr166 |
1.23 |
|
266 |
|
|
awaitNumberWaiting(c, 2); |
267 |
jsr166 |
1.14 |
c.reset(); |
268 |
jsr166 |
1.21 |
awaitTermination(t1); |
269 |
|
|
awaitTermination(t2); |
270 |
dl |
1.4 |
} |
271 |
|
|
|
272 |
|
|
/** |
273 |
|
|
* A reset before threads enter barrier does not throw |
274 |
|
|
* BrokenBarrierException |
275 |
|
|
*/ |
276 |
jsr166 |
1.14 |
public void testReset_NoBrokenBarrier() throws Exception { |
277 |
dl |
1.4 |
final CyclicBarrier c = new CyclicBarrier(3); |
278 |
jsr166 |
1.21 |
c.reset(); |
279 |
|
|
|
280 |
|
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
281 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
282 |
|
|
c.await(); |
283 |
|
|
}}); |
284 |
jsr166 |
1.21 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
285 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
286 |
|
|
c.await(); |
287 |
|
|
}}); |
288 |
|
|
|
289 |
|
|
c.await(); |
290 |
jsr166 |
1.21 |
awaitTermination(t1); |
291 |
|
|
awaitTermination(t2); |
292 |
dl |
1.4 |
} |
293 |
|
|
|
294 |
dl |
1.8 |
/** |
295 |
|
|
* All threads block while a barrier is broken. |
296 |
|
|
*/ |
297 |
jsr166 |
1.14 |
public void testReset_Leakage() throws InterruptedException { |
298 |
|
|
final CyclicBarrier c = new CyclicBarrier(2); |
299 |
|
|
final AtomicBoolean done = new AtomicBoolean(); |
300 |
jsr166 |
1.21 |
Thread t = newStartedThread(new CheckedRunnable() { |
301 |
jsr166 |
1.20 |
public void realRun() { |
302 |
|
|
while (!done.get()) { |
303 |
|
|
try { |
304 |
|
|
while (c.isBroken()) |
305 |
|
|
c.reset(); |
306 |
|
|
|
307 |
|
|
c.await(); |
308 |
|
|
shouldThrow(); |
309 |
dl |
1.8 |
} |
310 |
jsr166 |
1.20 |
catch (BrokenBarrierException ok) {} |
311 |
|
|
catch (InterruptedException ok) {} |
312 |
|
|
}}}); |
313 |
jsr166 |
1.9 |
|
314 |
jsr166 |
1.14 |
for (int i = 0; i < 4; i++) { |
315 |
jsr166 |
1.20 |
delay(timeoutMillis()); |
316 |
dl |
1.8 |
t.interrupt(); |
317 |
|
|
} |
318 |
jsr166 |
1.14 |
done.set(true); |
319 |
|
|
t.interrupt(); |
320 |
jsr166 |
1.20 |
awaitTermination(t); |
321 |
dl |
1.8 |
} |
322 |
|
|
|
323 |
|
|
/** |
324 |
|
|
* Reset of a non-broken barrier does not break barrier |
325 |
|
|
*/ |
326 |
jsr166 |
1.14 |
public void testResetWithoutBreakage() throws Exception { |
327 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
328 |
|
|
for (int i = 0; i < 3; i++) { |
329 |
jsr166 |
1.21 |
final CyclicBarrier start = new CyclicBarrier(3); |
330 |
|
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
331 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
332 |
|
|
start.await(); |
333 |
|
|
barrier.await(); |
334 |
|
|
}}); |
335 |
|
|
|
336 |
jsr166 |
1.21 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
337 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
338 |
|
|
start.await(); |
339 |
|
|
barrier.await(); |
340 |
|
|
}}); |
341 |
|
|
|
342 |
|
|
start.await(); |
343 |
|
|
barrier.await(); |
344 |
jsr166 |
1.21 |
awaitTermination(t1); |
345 |
|
|
awaitTermination(t2); |
346 |
jsr166 |
1.14 |
assertFalse(barrier.isBroken()); |
347 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
348 |
|
|
if (i == 1) barrier.reset(); |
349 |
|
|
assertFalse(barrier.isBroken()); |
350 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
351 |
dl |
1.8 |
} |
352 |
|
|
} |
353 |
jsr166 |
1.9 |
|
354 |
dl |
1.8 |
/** |
355 |
|
|
* Reset of a barrier after interruption reinitializes it. |
356 |
|
|
*/ |
357 |
jsr166 |
1.14 |
public void testResetAfterInterrupt() throws Exception { |
358 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
359 |
|
|
for (int i = 0; i < 2; i++) { |
360 |
jsr166 |
1.21 |
final CyclicBarrier start = new CyclicBarrier(3); |
361 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
362 |
|
|
public void realRun() throws Exception { |
363 |
|
|
start.await(); |
364 |
|
|
barrier.await(); |
365 |
|
|
}}; |
366 |
|
|
|
367 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
368 |
|
|
public void realRun() throws Exception { |
369 |
|
|
start.await(); |
370 |
|
|
barrier.await(); |
371 |
|
|
}}; |
372 |
|
|
|
373 |
|
|
t1.start(); |
374 |
|
|
t2.start(); |
375 |
|
|
start.await(); |
376 |
|
|
t1.interrupt(); |
377 |
jsr166 |
1.21 |
awaitTermination(t1); |
378 |
|
|
awaitTermination(t2); |
379 |
jsr166 |
1.14 |
assertTrue(barrier.isBroken()); |
380 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
381 |
|
|
barrier.reset(); |
382 |
|
|
assertFalse(barrier.isBroken()); |
383 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
384 |
dl |
1.8 |
} |
385 |
|
|
} |
386 |
jsr166 |
1.9 |
|
387 |
dl |
1.8 |
/** |
388 |
|
|
* Reset of a barrier after timeout reinitializes it. |
389 |
|
|
*/ |
390 |
jsr166 |
1.14 |
public void testResetAfterTimeout() throws Exception { |
391 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
392 |
|
|
for (int i = 0; i < 2; i++) { |
393 |
jsr166 |
1.21 |
assertEquals(0, barrier.getNumberWaiting()); |
394 |
|
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
395 |
|
|
public void realRun() throws Exception { |
396 |
|
|
try { |
397 |
|
|
barrier.await(); |
398 |
|
|
shouldThrow(); |
399 |
|
|
} catch (BrokenBarrierException success) {} |
400 |
|
|
}}); |
401 |
|
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
402 |
jsr166 |
1.14 |
public void realRun() throws Exception { |
403 |
jsr166 |
1.23 |
awaitNumberWaiting(barrier, 1); |
404 |
jsr166 |
1.21 |
long startTime = System.nanoTime(); |
405 |
|
|
try { |
406 |
|
|
barrier.await(timeoutMillis(), MILLISECONDS); |
407 |
|
|
shouldThrow(); |
408 |
|
|
} catch (TimeoutException success) {} |
409 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
410 |
|
|
}}); |
411 |
jsr166 |
1.14 |
|
412 |
jsr166 |
1.21 |
awaitTermination(t1); |
413 |
|
|
awaitTermination(t2); |
414 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
415 |
jsr166 |
1.14 |
assertTrue(barrier.isBroken()); |
416 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
417 |
|
|
barrier.reset(); |
418 |
|
|
assertFalse(barrier.isBroken()); |
419 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
420 |
dl |
1.8 |
} |
421 |
|
|
} |
422 |
|
|
|
423 |
|
|
/** |
424 |
|
|
* Reset of a barrier after a failed command reinitializes it. |
425 |
|
|
*/ |
426 |
jsr166 |
1.14 |
public void testResetAfterCommandException() throws Exception { |
427 |
|
|
final CyclicBarrier barrier = |
428 |
|
|
new CyclicBarrier(3, new Runnable() { |
429 |
|
|
public void run() { |
430 |
|
|
throw new NullPointerException(); }}); |
431 |
|
|
for (int i = 0; i < 2; i++) { |
432 |
jsr166 |
1.21 |
final CyclicBarrier start = new CyclicBarrier(3); |
433 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
434 |
|
|
public void realRun() throws Exception { |
435 |
|
|
start.await(); |
436 |
|
|
barrier.await(); |
437 |
|
|
}}; |
438 |
|
|
|
439 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
440 |
|
|
public void realRun() throws Exception { |
441 |
|
|
start.await(); |
442 |
|
|
barrier.await(); |
443 |
|
|
}}; |
444 |
|
|
|
445 |
|
|
t1.start(); |
446 |
|
|
t2.start(); |
447 |
|
|
start.await(); |
448 |
jsr166 |
1.23 |
awaitNumberWaiting(barrier, 2); |
449 |
jsr166 |
1.14 |
try { |
450 |
|
|
barrier.await(); |
451 |
|
|
shouldThrow(); |
452 |
|
|
} catch (NullPointerException success) {} |
453 |
jsr166 |
1.21 |
awaitTermination(t1); |
454 |
|
|
awaitTermination(t2); |
455 |
jsr166 |
1.14 |
assertTrue(barrier.isBroken()); |
456 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
457 |
|
|
barrier.reset(); |
458 |
|
|
assertFalse(barrier.isBroken()); |
459 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
460 |
dl |
1.8 |
} |
461 |
|
|
} |
462 |
dl |
1.1 |
} |