1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* A 1-party barrier triggers properly |
56 |
> |
* A 1-party barrier triggers after single await |
57 |
|
*/ |
58 |
|
public void testSingleParty() { |
59 |
|
try { |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* A 2-party/thread barrier triggers properly |
92 |
> |
* A 2-party/thread barrier triggers after both threads invoke await |
93 |
|
*/ |
94 |
|
public void testTwoParties() { |
95 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
166 |
|
Thread t1 = new Thread(new Runnable() { |
167 |
|
public void run() { |
168 |
|
try { |
169 |
< |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
169 |
> |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
170 |
|
threadShouldThrow(); |
171 |
|
} catch(InterruptedException success){ |
172 |
|
} catch(Exception b){ |
177 |
|
Thread t2 = new Thread(new Runnable() { |
178 |
|
public void run() { |
179 |
|
try { |
180 |
< |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
180 |
> |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
181 |
|
threadShouldThrow(); |
182 |
|
} catch(BrokenBarrierException success){ |
183 |
|
} catch(Exception i){ |
220 |
|
} catch(InterruptedException e){ |
221 |
|
unexpectedException(); |
222 |
|
} |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
/** |
226 |
+ |
* A timeout in one party causes others waiting in timed await to |
227 |
+ |
* throw BrokenBarrierException |
228 |
+ |
*/ |
229 |
+ |
public void testAwait4_Timeout_BrokenBarrier() { |
230 |
+ |
final CyclicBarrier c = new CyclicBarrier(3); |
231 |
+ |
Thread t1 = new Thread(new Runnable() { |
232 |
+ |
public void run() { |
233 |
+ |
try { |
234 |
+ |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
235 |
+ |
threadShouldThrow(); |
236 |
+ |
} catch(TimeoutException success){ |
237 |
+ |
} catch(Exception b){ |
238 |
+ |
threadUnexpectedException(); |
239 |
+ |
} |
240 |
+ |
} |
241 |
+ |
}); |
242 |
+ |
Thread t2 = new Thread(new Runnable() { |
243 |
+ |
public void run() { |
244 |
+ |
try { |
245 |
+ |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
246 |
+ |
threadShouldThrow(); |
247 |
+ |
} catch(BrokenBarrierException success){ |
248 |
+ |
} catch(Exception i){ |
249 |
+ |
threadUnexpectedException(); |
250 |
+ |
} |
251 |
+ |
} |
252 |
+ |
}); |
253 |
+ |
try { |
254 |
+ |
t1.start(); |
255 |
+ |
t2.start(); |
256 |
+ |
t1.join(); |
257 |
+ |
t2.join(); |
258 |
+ |
} catch(InterruptedException e){ |
259 |
+ |
unexpectedException(); |
260 |
+ |
} |
261 |
+ |
} |
262 |
+ |
|
263 |
+ |
/** |
264 |
+ |
* A timeout in one party causes others waiting in await to |
265 |
+ |
* throw BrokenBarrierException |
266 |
+ |
*/ |
267 |
+ |
public void testAwait5_Timeout_BrokenBarrier() { |
268 |
+ |
final CyclicBarrier c = new CyclicBarrier(3); |
269 |
+ |
Thread t1 = new Thread(new Runnable() { |
270 |
+ |
public void run() { |
271 |
+ |
try { |
272 |
+ |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
273 |
+ |
threadShouldThrow(); |
274 |
+ |
} catch(TimeoutException success){ |
275 |
+ |
} catch(Exception b){ |
276 |
+ |
threadUnexpectedException(); |
277 |
+ |
} |
278 |
+ |
} |
279 |
+ |
}); |
280 |
+ |
Thread t2 = new Thread(new Runnable() { |
281 |
+ |
public void run() { |
282 |
+ |
try { |
283 |
+ |
c.await(); |
284 |
+ |
threadShouldThrow(); |
285 |
+ |
} catch(BrokenBarrierException success){ |
286 |
+ |
} catch(Exception i){ |
287 |
+ |
threadUnexpectedException(); |
288 |
+ |
} |
289 |
+ |
} |
290 |
+ |
}); |
291 |
+ |
try { |
292 |
+ |
t1.start(); |
293 |
+ |
t2.start(); |
294 |
+ |
t1.join(); |
295 |
+ |
t2.join(); |
296 |
+ |
} catch(InterruptedException e){ |
297 |
+ |
unexpectedException(); |
298 |
+ |
} |
299 |
|
} |
300 |
|
|
301 |
|
/** |