27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
+ |
* 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 |
|
* Creating with negative parties throws IAE |
43 |
|
*/ |
44 |
|
public void testConstructor1() { |
198 |
|
}}); |
199 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
200 |
|
public void realRun() throws Exception { |
201 |
< |
while (c.getNumberWaiting() == 0) |
190 |
< |
Thread.yield(); |
201 |
> |
awaitNumberWaiting(c, 1); |
202 |
|
long startTime = System.nanoTime(); |
203 |
|
try { |
204 |
|
c.await(timeoutMillis(), MILLISECONDS); |
226 |
|
}}); |
227 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
228 |
|
public void realRun() throws Exception { |
229 |
< |
while (c.getNumberWaiting() == 0) |
219 |
< |
Thread.yield(); |
229 |
> |
awaitNumberWaiting(c, 1); |
230 |
|
long startTime = System.nanoTime(); |
231 |
|
try { |
232 |
|
c.await(timeoutMillis(), MILLISECONDS); |
244 |
|
* BrokenBarrierException |
245 |
|
*/ |
246 |
|
public void testReset_BrokenBarrier() throws InterruptedException { |
237 |
– |
Thread.interrupted(); // ensure current thread not interrupted |
247 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
248 |
|
final CountDownLatch pleaseReset = new CountDownLatch(2); |
249 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
260 |
|
t1.start(); |
261 |
|
t2.start(); |
262 |
|
await(pleaseReset); |
263 |
+ |
|
264 |
+ |
awaitNumberWaiting(c, 2); |
265 |
|
c.reset(); |
266 |
|
awaitTermination(t1); |
267 |
|
awaitTermination(t2); |
398 |
|
}}); |
399 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
400 |
|
public void realRun() throws Exception { |
401 |
< |
while (barrier.getNumberWaiting() == 0) |
391 |
< |
Thread.yield(); |
401 |
> |
awaitNumberWaiting(barrier, 1); |
402 |
|
long startTime = System.nanoTime(); |
403 |
|
try { |
404 |
|
barrier.await(timeoutMillis(), MILLISECONDS); |
443 |
|
t1.start(); |
444 |
|
t2.start(); |
445 |
|
start.await(); |
446 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
446 |
> |
awaitNumberWaiting(barrier, 2); |
447 |
|
try { |
448 |
|
barrier.await(); |
449 |
|
shouldThrow(); |