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