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 |
|
|
* http://creativecommons.org/licenses/publicdomain |
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 |
|
|
import java.util.concurrent.*; |
12 |
dl |
1.8 |
import java.util.concurrent.locks.*; |
13 |
|
|
import java.util.concurrent.atomic.*; |
14 |
jsr166 |
1.13 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 |
dl |
1.1 |
|
16 |
jsr166 |
1.11 |
public class CyclicBarrierTest extends JSR166TestCase { |
17 |
dl |
1.1 |
public static void main(String[] args) { |
18 |
jsr166 |
1.12 |
junit.textui.TestRunner.run (suite()); |
19 |
dl |
1.1 |
} |
20 |
|
|
public static Test suite() { |
21 |
jsr166 |
1.12 |
return new TestSuite(CyclicBarrierTest.class); |
22 |
dl |
1.1 |
} |
23 |
dl |
1.3 |
|
24 |
|
|
private volatile int countAction; |
25 |
|
|
private class MyAction implements Runnable { |
26 |
|
|
public void run() { ++countAction; } |
27 |
|
|
} |
28 |
jsr166 |
1.9 |
|
29 |
dl |
1.3 |
/** |
30 |
dl |
1.4 |
* Creating with negative parties throws IAE |
31 |
dl |
1.3 |
*/ |
32 |
|
|
public void testConstructor1() { |
33 |
|
|
try { |
34 |
dl |
1.1 |
new CyclicBarrier(-1, (Runnable)null); |
35 |
dl |
1.3 |
shouldThrow(); |
36 |
jsr166 |
1.11 |
} catch (IllegalArgumentException e) {} |
37 |
dl |
1.1 |
} |
38 |
|
|
|
39 |
dl |
1.3 |
/** |
40 |
dl |
1.4 |
* Creating with negative parties and no action throws IAE |
41 |
dl |
1.3 |
*/ |
42 |
|
|
public void testConstructor2() { |
43 |
|
|
try { |
44 |
dl |
1.1 |
new CyclicBarrier(-1); |
45 |
dl |
1.3 |
shouldThrow(); |
46 |
jsr166 |
1.11 |
} catch (IllegalArgumentException e) {} |
47 |
dl |
1.1 |
} |
48 |
|
|
|
49 |
dl |
1.3 |
/** |
50 |
dl |
1.4 |
* getParties returns the number of parties given in constructor |
51 |
dl |
1.3 |
*/ |
52 |
dl |
1.4 |
public void testGetParties() { |
53 |
dl |
1.1 |
CyclicBarrier b = new CyclicBarrier(2); |
54 |
jsr166 |
1.12 |
assertEquals(2, b.getParties()); |
55 |
dl |
1.1 |
assertEquals(0, b.getNumberWaiting()); |
56 |
|
|
} |
57 |
|
|
|
58 |
dl |
1.3 |
/** |
59 |
dl |
1.5 |
* A 1-party barrier triggers after single await |
60 |
dl |
1.3 |
*/ |
61 |
dl |
1.1 |
public void testSingleParty() { |
62 |
|
|
try { |
63 |
|
|
CyclicBarrier b = new CyclicBarrier(1); |
64 |
|
|
assertEquals(1, b.getParties()); |
65 |
|
|
assertEquals(0, b.getNumberWaiting()); |
66 |
|
|
b.await(); |
67 |
|
|
b.await(); |
68 |
|
|
assertEquals(0, b.getNumberWaiting()); |
69 |
|
|
} |
70 |
jsr166 |
1.10 |
catch (Exception e) { |
71 |
dl |
1.3 |
unexpectedException(); |
72 |
dl |
1.1 |
} |
73 |
|
|
} |
74 |
jsr166 |
1.9 |
|
75 |
dl |
1.3 |
/** |
76 |
dl |
1.4 |
* The supplied barrier action is run at barrier |
77 |
dl |
1.3 |
*/ |
78 |
dl |
1.1 |
public void testBarrierAction() { |
79 |
|
|
try { |
80 |
|
|
countAction = 0; |
81 |
|
|
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
82 |
|
|
assertEquals(1, b.getParties()); |
83 |
|
|
assertEquals(0, b.getNumberWaiting()); |
84 |
|
|
b.await(); |
85 |
|
|
b.await(); |
86 |
|
|
assertEquals(0, b.getNumberWaiting()); |
87 |
|
|
assertEquals(countAction, 2); |
88 |
|
|
} |
89 |
jsr166 |
1.10 |
catch (Exception e) { |
90 |
dl |
1.3 |
unexpectedException(); |
91 |
dl |
1.1 |
} |
92 |
|
|
} |
93 |
|
|
|
94 |
dl |
1.3 |
/** |
95 |
dl |
1.5 |
* A 2-party/thread barrier triggers after both threads invoke await |
96 |
dl |
1.3 |
*/ |
97 |
|
|
public void testTwoParties() { |
98 |
dl |
1.1 |
final CyclicBarrier b = new CyclicBarrier(2); |
99 |
jsr166 |
1.12 |
Thread t = new Thread(new Runnable() { |
100 |
|
|
public void run() { |
101 |
dl |
1.1 |
try { |
102 |
|
|
b.await(); |
103 |
|
|
b.await(); |
104 |
|
|
b.await(); |
105 |
|
|
b.await(); |
106 |
jsr166 |
1.11 |
} catch (Exception e) { |
107 |
dl |
1.3 |
threadUnexpectedException(); |
108 |
dl |
1.1 |
}}}); |
109 |
|
|
|
110 |
|
|
try { |
111 |
|
|
t.start(); |
112 |
|
|
b.await(); |
113 |
|
|
b.await(); |
114 |
|
|
b.await(); |
115 |
|
|
b.await(); |
116 |
|
|
t.join(); |
117 |
jsr166 |
1.11 |
} catch (Exception e) { |
118 |
dl |
1.3 |
unexpectedException(); |
119 |
dl |
1.1 |
} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
dl |
1.3 |
/** |
124 |
dl |
1.4 |
* An interruption in one party causes others waiting in await to |
125 |
|
|
* throw BrokenBarrierException |
126 |
dl |
1.3 |
*/ |
127 |
|
|
public void testAwait1_Interrupted_BrokenBarrier() { |
128 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(3); |
129 |
|
|
Thread t1 = new Thread(new Runnable() { |
130 |
dl |
1.3 |
public void run() { |
131 |
|
|
try { |
132 |
dl |
1.1 |
c.await(); |
133 |
dl |
1.3 |
threadShouldThrow(); |
134 |
jsr166 |
1.11 |
} catch (InterruptedException success) {} |
135 |
|
|
catch (Exception b) { |
136 |
dl |
1.4 |
threadUnexpectedException(); |
137 |
dl |
1.1 |
} |
138 |
|
|
} |
139 |
|
|
}); |
140 |
dl |
1.3 |
Thread t2 = new Thread(new Runnable() { |
141 |
|
|
public void run() { |
142 |
|
|
try { |
143 |
dl |
1.1 |
c.await(); |
144 |
jsr166 |
1.9 |
threadShouldThrow(); |
145 |
jsr166 |
1.11 |
} catch (BrokenBarrierException success) { |
146 |
|
|
} catch (Exception i) { |
147 |
dl |
1.4 |
threadUnexpectedException(); |
148 |
dl |
1.1 |
} |
149 |
|
|
} |
150 |
|
|
}); |
151 |
|
|
try { |
152 |
|
|
t1.start(); |
153 |
|
|
t2.start(); |
154 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
155 |
|
|
t1.interrupt(); |
156 |
jsr166 |
1.9 |
t1.join(); |
157 |
dl |
1.1 |
t2.join(); |
158 |
jsr166 |
1.11 |
} catch (InterruptedException e) { |
159 |
dl |
1.3 |
unexpectedException(); |
160 |
dl |
1.1 |
} |
161 |
|
|
} |
162 |
|
|
|
163 |
dl |
1.3 |
/** |
164 |
dl |
1.4 |
* An interruption in one party causes others waiting in timed await to |
165 |
|
|
* throw BrokenBarrierException |
166 |
dl |
1.3 |
*/ |
167 |
|
|
public void testAwait2_Interrupted_BrokenBarrier() { |
168 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
169 |
dl |
1.1 |
Thread t1 = new Thread(new Runnable() { |
170 |
dl |
1.3 |
public void run() { |
171 |
|
|
try { |
172 |
jsr166 |
1.13 |
c.await(LONG_DELAY_MS, MILLISECONDS); |
173 |
dl |
1.3 |
threadShouldThrow(); |
174 |
jsr166 |
1.11 |
} catch (InterruptedException success) { |
175 |
|
|
} catch (Exception b) { |
176 |
dl |
1.4 |
threadUnexpectedException(); |
177 |
dl |
1.1 |
} |
178 |
|
|
} |
179 |
|
|
}); |
180 |
dl |
1.3 |
Thread t2 = new Thread(new Runnable() { |
181 |
|
|
public void run() { |
182 |
|
|
try { |
183 |
jsr166 |
1.13 |
c.await(LONG_DELAY_MS, MILLISECONDS); |
184 |
jsr166 |
1.9 |
threadShouldThrow(); |
185 |
jsr166 |
1.11 |
} catch (BrokenBarrierException success) { |
186 |
|
|
} catch (Exception i) { |
187 |
dl |
1.4 |
threadUnexpectedException(); |
188 |
dl |
1.1 |
} |
189 |
|
|
} |
190 |
|
|
}); |
191 |
|
|
try { |
192 |
|
|
t1.start(); |
193 |
|
|
t2.start(); |
194 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
195 |
|
|
t1.interrupt(); |
196 |
jsr166 |
1.9 |
t1.join(); |
197 |
dl |
1.1 |
t2.join(); |
198 |
jsr166 |
1.11 |
} catch (InterruptedException e) { |
199 |
dl |
1.3 |
unexpectedException(); |
200 |
dl |
1.1 |
} |
201 |
|
|
} |
202 |
jsr166 |
1.9 |
|
203 |
dl |
1.3 |
/** |
204 |
dl |
1.4 |
* A timeout in timed await throws TimeoutException |
205 |
dl |
1.3 |
*/ |
206 |
|
|
public void testAwait3_TimeOutException() { |
207 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(2); |
208 |
|
|
Thread t = new Thread(new Runnable() { |
209 |
dl |
1.3 |
public void run() { |
210 |
|
|
try { |
211 |
jsr166 |
1.13 |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
212 |
dl |
1.3 |
threadShouldThrow(); |
213 |
jsr166 |
1.11 |
} catch (TimeoutException success) { |
214 |
|
|
} catch (Exception b) { |
215 |
dl |
1.4 |
threadUnexpectedException(); |
216 |
jsr166 |
1.9 |
|
217 |
dl |
1.1 |
} |
218 |
|
|
} |
219 |
|
|
}); |
220 |
|
|
try { |
221 |
|
|
t.start(); |
222 |
jsr166 |
1.9 |
t.join(); |
223 |
jsr166 |
1.11 |
} catch (InterruptedException e) { |
224 |
dl |
1.5 |
unexpectedException(); |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
/** |
229 |
|
|
* A timeout in one party causes others waiting in timed await to |
230 |
|
|
* throw BrokenBarrierException |
231 |
|
|
*/ |
232 |
|
|
public void testAwait4_Timeout_BrokenBarrier() { |
233 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
234 |
dl |
1.5 |
Thread t1 = new Thread(new Runnable() { |
235 |
|
|
public void run() { |
236 |
|
|
try { |
237 |
jsr166 |
1.13 |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
238 |
dl |
1.5 |
threadShouldThrow(); |
239 |
jsr166 |
1.11 |
} catch (TimeoutException success) { |
240 |
|
|
} catch (Exception b) { |
241 |
dl |
1.5 |
threadUnexpectedException(); |
242 |
|
|
} |
243 |
|
|
} |
244 |
|
|
}); |
245 |
|
|
Thread t2 = new Thread(new Runnable() { |
246 |
|
|
public void run() { |
247 |
|
|
try { |
248 |
jsr166 |
1.13 |
c.await(MEDIUM_DELAY_MS, MILLISECONDS); |
249 |
jsr166 |
1.9 |
threadShouldThrow(); |
250 |
jsr166 |
1.11 |
} catch (BrokenBarrierException success) { |
251 |
|
|
} catch (Exception i) { |
252 |
dl |
1.5 |
threadUnexpectedException(); |
253 |
|
|
} |
254 |
|
|
} |
255 |
|
|
}); |
256 |
|
|
try { |
257 |
|
|
t1.start(); |
258 |
|
|
t2.start(); |
259 |
jsr166 |
1.9 |
t1.join(); |
260 |
dl |
1.5 |
t2.join(); |
261 |
jsr166 |
1.11 |
} catch (InterruptedException e) { |
262 |
dl |
1.5 |
unexpectedException(); |
263 |
|
|
} |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
/** |
267 |
|
|
* A timeout in one party causes others waiting in await to |
268 |
|
|
* throw BrokenBarrierException |
269 |
|
|
*/ |
270 |
|
|
public void testAwait5_Timeout_BrokenBarrier() { |
271 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
272 |
dl |
1.5 |
Thread t1 = new Thread(new Runnable() { |
273 |
|
|
public void run() { |
274 |
|
|
try { |
275 |
jsr166 |
1.13 |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
276 |
dl |
1.5 |
threadShouldThrow(); |
277 |
jsr166 |
1.11 |
} catch (TimeoutException success) { |
278 |
|
|
} catch (Exception b) { |
279 |
dl |
1.5 |
threadUnexpectedException(); |
280 |
|
|
} |
281 |
|
|
} |
282 |
|
|
}); |
283 |
|
|
Thread t2 = new Thread(new Runnable() { |
284 |
|
|
public void run() { |
285 |
|
|
try { |
286 |
|
|
c.await(); |
287 |
jsr166 |
1.9 |
threadShouldThrow(); |
288 |
jsr166 |
1.11 |
} catch (BrokenBarrierException success) { |
289 |
|
|
} catch (Exception i) { |
290 |
dl |
1.5 |
threadUnexpectedException(); |
291 |
|
|
} |
292 |
|
|
} |
293 |
|
|
}); |
294 |
|
|
try { |
295 |
|
|
t1.start(); |
296 |
|
|
t2.start(); |
297 |
jsr166 |
1.9 |
t1.join(); |
298 |
dl |
1.5 |
t2.join(); |
299 |
jsr166 |
1.11 |
} catch (InterruptedException e) { |
300 |
dl |
1.3 |
unexpectedException(); |
301 |
dl |
1.1 |
} |
302 |
|
|
} |
303 |
jsr166 |
1.9 |
|
304 |
dl |
1.4 |
/** |
305 |
|
|
* A reset of an active barrier causes waiting threads to throw |
306 |
|
|
* BrokenBarrierException |
307 |
|
|
*/ |
308 |
|
|
public void testReset_BrokenBarrier() { |
309 |
|
|
final CyclicBarrier c = new CyclicBarrier(3); |
310 |
|
|
Thread t1 = new Thread(new Runnable() { |
311 |
|
|
public void run() { |
312 |
|
|
try { |
313 |
|
|
c.await(); |
314 |
|
|
threadShouldThrow(); |
315 |
jsr166 |
1.11 |
} catch (BrokenBarrierException success) {} |
316 |
|
|
catch (Exception b) { |
317 |
dl |
1.4 |
threadUnexpectedException(); |
318 |
|
|
} |
319 |
|
|
} |
320 |
|
|
}); |
321 |
|
|
Thread t2 = new Thread(new Runnable() { |
322 |
|
|
public void run() { |
323 |
|
|
try { |
324 |
|
|
c.await(); |
325 |
jsr166 |
1.9 |
threadShouldThrow(); |
326 |
jsr166 |
1.11 |
} catch (BrokenBarrierException success) { |
327 |
|
|
} catch (Exception i) { |
328 |
dl |
1.4 |
threadUnexpectedException(); |
329 |
|
|
} |
330 |
|
|
} |
331 |
|
|
}); |
332 |
|
|
try { |
333 |
|
|
t1.start(); |
334 |
|
|
t2.start(); |
335 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
336 |
|
|
c.reset(); |
337 |
jsr166 |
1.9 |
t1.join(); |
338 |
dl |
1.4 |
t2.join(); |
339 |
jsr166 |
1.11 |
} catch (InterruptedException e) { |
340 |
dl |
1.4 |
unexpectedException(); |
341 |
|
|
} |
342 |
|
|
} |
343 |
|
|
|
344 |
|
|
/** |
345 |
|
|
* A reset before threads enter barrier does not throw |
346 |
|
|
* BrokenBarrierException |
347 |
|
|
*/ |
348 |
|
|
public void testReset_NoBrokenBarrier() { |
349 |
|
|
final CyclicBarrier c = new CyclicBarrier(3); |
350 |
|
|
Thread t1 = new Thread(new Runnable() { |
351 |
|
|
public void run() { |
352 |
|
|
try { |
353 |
|
|
c.await(); |
354 |
jsr166 |
1.11 |
} catch (Exception b) { |
355 |
dl |
1.4 |
threadUnexpectedException(); |
356 |
|
|
} |
357 |
|
|
} |
358 |
|
|
}); |
359 |
|
|
Thread t2 = new Thread(new Runnable() { |
360 |
|
|
public void run() { |
361 |
|
|
try { |
362 |
|
|
c.await(); |
363 |
jsr166 |
1.11 |
} catch (Exception i) { |
364 |
dl |
1.4 |
threadUnexpectedException(); |
365 |
|
|
} |
366 |
|
|
} |
367 |
|
|
}); |
368 |
|
|
try { |
369 |
|
|
c.reset(); |
370 |
|
|
t1.start(); |
371 |
|
|
t2.start(); |
372 |
|
|
c.await(); |
373 |
jsr166 |
1.9 |
t1.join(); |
374 |
dl |
1.4 |
t2.join(); |
375 |
jsr166 |
1.11 |
} catch (Exception e) { |
376 |
dl |
1.4 |
unexpectedException(); |
377 |
|
|
} |
378 |
|
|
} |
379 |
|
|
|
380 |
dl |
1.8 |
/** |
381 |
|
|
* All threads block while a barrier is broken. |
382 |
|
|
*/ |
383 |
|
|
public void testReset_Leakage() { |
384 |
|
|
try { |
385 |
|
|
final CyclicBarrier c = new CyclicBarrier(2); |
386 |
|
|
final AtomicBoolean done = new AtomicBoolean(); |
387 |
|
|
Thread t = new Thread() { |
388 |
|
|
public void run() { |
389 |
|
|
while (!done.get()) { |
390 |
|
|
try { |
391 |
|
|
while (c.isBroken()) |
392 |
|
|
c.reset(); |
393 |
jsr166 |
1.9 |
|
394 |
dl |
1.8 |
c.await(); |
395 |
|
|
threadFail("await should not return"); |
396 |
|
|
} |
397 |
|
|
catch (BrokenBarrierException e) { |
398 |
|
|
} |
399 |
|
|
catch (InterruptedException ie) { |
400 |
|
|
} |
401 |
|
|
} |
402 |
|
|
} |
403 |
|
|
}; |
404 |
jsr166 |
1.9 |
|
405 |
dl |
1.8 |
t.start(); |
406 |
jsr166 |
1.10 |
for ( int i = 0; i < 4; i++) { |
407 |
dl |
1.8 |
Thread.sleep(SHORT_DELAY_MS); |
408 |
|
|
t.interrupt(); |
409 |
|
|
} |
410 |
|
|
done.set(true); |
411 |
|
|
t.interrupt(); |
412 |
|
|
} |
413 |
|
|
catch (Exception ex) { |
414 |
|
|
unexpectedException(); |
415 |
|
|
} |
416 |
|
|
} |
417 |
|
|
|
418 |
|
|
/** |
419 |
|
|
* Reset of a non-broken barrier does not break barrier |
420 |
|
|
*/ |
421 |
|
|
public void testResetWithoutBreakage() { |
422 |
|
|
try { |
423 |
|
|
final CyclicBarrier start = new CyclicBarrier(3); |
424 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
425 |
|
|
for (int i = 0; i < 3; i++) { |
426 |
|
|
Thread t1 = new Thread(new Runnable() { |
427 |
|
|
public void run() { |
428 |
|
|
try { start.await(); } |
429 |
jsr166 |
1.9 |
catch (Exception ie) { |
430 |
|
|
threadFail("start barrier"); |
431 |
dl |
1.8 |
} |
432 |
|
|
try { barrier.await(); } |
433 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
434 |
|
|
unexpectedException(); |
435 |
dl |
1.8 |
}}}); |
436 |
jsr166 |
1.9 |
|
437 |
dl |
1.8 |
Thread t2 = new Thread(new Runnable() { |
438 |
|
|
public void run() { |
439 |
|
|
try { start.await(); } |
440 |
jsr166 |
1.9 |
catch (Exception ie) { |
441 |
|
|
threadFail("start barrier"); |
442 |
dl |
1.8 |
} |
443 |
|
|
try { barrier.await(); } |
444 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
445 |
|
|
unexpectedException(); |
446 |
dl |
1.8 |
}}}); |
447 |
jsr166 |
1.9 |
|
448 |
|
|
|
449 |
dl |
1.8 |
t1.start(); |
450 |
|
|
t2.start(); |
451 |
|
|
try { start.await(); } |
452 |
|
|
catch (Exception ie) { threadFail("start barrier"); } |
453 |
|
|
barrier.await(); |
454 |
|
|
t1.join(); |
455 |
|
|
t2.join(); |
456 |
|
|
assertFalse(barrier.isBroken()); |
457 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
458 |
|
|
if (i == 1) barrier.reset(); |
459 |
|
|
assertFalse(barrier.isBroken()); |
460 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
461 |
|
|
} |
462 |
|
|
} |
463 |
|
|
catch (Exception ex) { |
464 |
|
|
unexpectedException(); |
465 |
|
|
} |
466 |
|
|
} |
467 |
jsr166 |
1.9 |
|
468 |
dl |
1.8 |
/** |
469 |
|
|
* Reset of a barrier after interruption reinitializes it. |
470 |
|
|
*/ |
471 |
|
|
public void testResetAfterInterrupt() { |
472 |
|
|
try { |
473 |
|
|
final CyclicBarrier start = new CyclicBarrier(3); |
474 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
475 |
|
|
for (int i = 0; i < 2; i++) { |
476 |
|
|
Thread t1 = new Thread(new Runnable() { |
477 |
|
|
public void run() { |
478 |
|
|
try { start.await(); } |
479 |
jsr166 |
1.9 |
catch (Exception ie) { |
480 |
|
|
threadFail("start barrier"); |
481 |
dl |
1.8 |
} |
482 |
|
|
try { barrier.await(); } |
483 |
jsr166 |
1.10 |
catch (InterruptedException ok) {} |
484 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
485 |
|
|
unexpectedException(); |
486 |
dl |
1.8 |
}}}); |
487 |
jsr166 |
1.9 |
|
488 |
dl |
1.8 |
Thread t2 = new Thread(new Runnable() { |
489 |
|
|
public void run() { |
490 |
|
|
try { start.await(); } |
491 |
jsr166 |
1.9 |
catch (Exception ie) { |
492 |
|
|
threadFail("start barrier"); |
493 |
dl |
1.8 |
} |
494 |
|
|
try { barrier.await(); } |
495 |
jsr166 |
1.10 |
catch (BrokenBarrierException ok) {} |
496 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
497 |
|
|
unexpectedException(); |
498 |
dl |
1.8 |
}}}); |
499 |
jsr166 |
1.9 |
|
500 |
dl |
1.8 |
t1.start(); |
501 |
|
|
t2.start(); |
502 |
|
|
try { start.await(); } |
503 |
|
|
catch (Exception ie) { threadFail("start barrier"); } |
504 |
|
|
t1.interrupt(); |
505 |
|
|
t1.join(); |
506 |
|
|
t2.join(); |
507 |
|
|
assertTrue(barrier.isBroken()); |
508 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
509 |
|
|
barrier.reset(); |
510 |
|
|
assertFalse(barrier.isBroken()); |
511 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
512 |
|
|
} |
513 |
|
|
} |
514 |
|
|
catch (Exception ex) { |
515 |
|
|
unexpectedException(); |
516 |
|
|
} |
517 |
|
|
} |
518 |
jsr166 |
1.9 |
|
519 |
dl |
1.8 |
/** |
520 |
|
|
* Reset of a barrier after timeout reinitializes it. |
521 |
|
|
*/ |
522 |
|
|
public void testResetAfterTimeout() { |
523 |
|
|
try { |
524 |
|
|
final CyclicBarrier start = new CyclicBarrier(3); |
525 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
526 |
|
|
for (int i = 0; i < 2; i++) { |
527 |
|
|
Thread t1 = new Thread(new Runnable() { |
528 |
|
|
public void run() { |
529 |
|
|
try { start.await(); } |
530 |
jsr166 |
1.9 |
catch (Exception ie) { |
531 |
|
|
threadFail("start barrier"); |
532 |
dl |
1.8 |
} |
533 |
jsr166 |
1.13 |
try { barrier.await(MEDIUM_DELAY_MS, MILLISECONDS); } |
534 |
jsr166 |
1.10 |
catch (TimeoutException ok) {} |
535 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
536 |
|
|
unexpectedException(); |
537 |
dl |
1.8 |
}}}); |
538 |
jsr166 |
1.9 |
|
539 |
dl |
1.8 |
Thread t2 = new Thread(new Runnable() { |
540 |
|
|
public void run() { |
541 |
|
|
try { start.await(); } |
542 |
jsr166 |
1.9 |
catch (Exception ie) { |
543 |
|
|
threadFail("start barrier"); |
544 |
dl |
1.8 |
} |
545 |
|
|
try { barrier.await(); } |
546 |
jsr166 |
1.10 |
catch (BrokenBarrierException ok) {} |
547 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
548 |
|
|
unexpectedException(); |
549 |
dl |
1.8 |
}}}); |
550 |
jsr166 |
1.9 |
|
551 |
dl |
1.8 |
t1.start(); |
552 |
|
|
t2.start(); |
553 |
|
|
try { start.await(); } |
554 |
|
|
catch (Exception ie) { threadFail("start barrier"); } |
555 |
|
|
t1.join(); |
556 |
|
|
t2.join(); |
557 |
|
|
assertTrue(barrier.isBroken()); |
558 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
559 |
|
|
barrier.reset(); |
560 |
|
|
assertFalse(barrier.isBroken()); |
561 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
562 |
|
|
} |
563 |
|
|
} |
564 |
|
|
catch (Exception ex) { |
565 |
|
|
unexpectedException(); |
566 |
|
|
} |
567 |
|
|
} |
568 |
|
|
|
569 |
jsr166 |
1.9 |
|
570 |
dl |
1.8 |
/** |
571 |
|
|
* Reset of a barrier after a failed command reinitializes it. |
572 |
|
|
*/ |
573 |
|
|
public void testResetAfterCommandException() { |
574 |
|
|
try { |
575 |
|
|
final CyclicBarrier start = new CyclicBarrier(3); |
576 |
jsr166 |
1.9 |
final CyclicBarrier barrier = |
577 |
dl |
1.8 |
new CyclicBarrier(3, new Runnable() { |
578 |
jsr166 |
1.9 |
public void run() { |
579 |
dl |
1.8 |
throw new NullPointerException(); }}); |
580 |
|
|
for (int i = 0; i < 2; i++) { |
581 |
|
|
Thread t1 = new Thread(new Runnable() { |
582 |
|
|
public void run() { |
583 |
|
|
try { start.await(); } |
584 |
jsr166 |
1.9 |
catch (Exception ie) { |
585 |
|
|
threadFail("start barrier"); |
586 |
dl |
1.8 |
} |
587 |
|
|
try { barrier.await(); } |
588 |
jsr166 |
1.10 |
catch (BrokenBarrierException ok) {} |
589 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
590 |
|
|
unexpectedException(); |
591 |
dl |
1.8 |
}}}); |
592 |
jsr166 |
1.9 |
|
593 |
dl |
1.8 |
Thread t2 = new Thread(new Runnable() { |
594 |
|
|
public void run() { |
595 |
|
|
try { start.await(); } |
596 |
jsr166 |
1.9 |
catch (Exception ie) { |
597 |
|
|
threadFail("start barrier"); |
598 |
dl |
1.8 |
} |
599 |
|
|
try { barrier.await(); } |
600 |
jsr166 |
1.10 |
catch (BrokenBarrierException ok) {} |
601 |
jsr166 |
1.9 |
catch (Throwable thrown) { |
602 |
|
|
unexpectedException(); |
603 |
dl |
1.8 |
}}}); |
604 |
jsr166 |
1.9 |
|
605 |
dl |
1.8 |
t1.start(); |
606 |
|
|
t2.start(); |
607 |
|
|
try { start.await(); } |
608 |
|
|
catch (Exception ie) { threadFail("start barrier"); } |
609 |
|
|
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
610 |
|
|
try { barrier.await(); } |
611 |
|
|
catch (Exception ok) { } |
612 |
|
|
t1.join(); |
613 |
|
|
t2.join(); |
614 |
|
|
assertTrue(barrier.isBroken()); |
615 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
616 |
|
|
barrier.reset(); |
617 |
|
|
assertFalse(barrier.isBroken()); |
618 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
619 |
|
|
} |
620 |
|
|
} |
621 |
|
|
catch (Exception ex) { |
622 |
|
|
unexpectedException(); |
623 |
|
|
} |
624 |
|
|
} |
625 |
dl |
1.1 |
} |