377 |
|
phaser.register(); |
378 |
|
List<Thread> threads = new ArrayList<Thread>(); |
379 |
|
for (int i = 0; i < 8; i++) { |
380 |
+ |
final CountDownLatch latch = new CountDownLatch(1); |
381 |
+ |
final boolean goesFirst = ((i & 1) == 0); |
382 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
383 |
< |
public void realRun() { |
384 |
< |
sleepTillInterrupted(SHORT_DELAY_MS); |
383 |
> |
public void realRun() throws InterruptedException { |
384 |
> |
if (goesFirst) |
385 |
> |
latch.countDown(); |
386 |
> |
else |
387 |
> |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
388 |
|
phaser.arrive(); |
389 |
|
}})); |
390 |
+ |
if (goesFirst) |
391 |
+ |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
392 |
+ |
else |
393 |
+ |
latch.countDown(); |
394 |
|
phase = phaser.awaitAdvance(phaser.arrive()); |
395 |
|
assertEquals(phase, phaser.getPhase()); |
396 |
|
} |
397 |
|
for (Thread thread : threads) |
398 |
< |
thread.join(); |
398 |
> |
awaitTermination(thread, SMALL_DELAY_MS); |
399 |
|
} |
400 |
|
|
401 |
|
/** |
403 |
|
*/ |
404 |
|
public void testAwaitAdvance6() throws InterruptedException { |
405 |
|
final Phaser phaser = new Phaser(3); |
406 |
< |
/* |
407 |
< |
* Start new thread. This thread waits a small amount of time |
408 |
< |
* and waits for the other two parties to arrive. The party |
409 |
< |
* in the main thread arrives quickly so at best this thread |
410 |
< |
* waits for the second thread's party to arrive |
411 |
< |
*/ |
412 |
< |
Thread t1 = newStartedThread(new CheckedRunnable() { |
413 |
< |
public void realRun() { |
414 |
< |
sleepTillInterrupted(SMALL_DELAY_MS); |
415 |
< |
int phase = phaser.awaitAdvance(phaser.arrive()); |
416 |
< |
/* |
417 |
< |
* This point is reached when force termination is called in which phase = -1 |
418 |
< |
*/ |
419 |
< |
assertTrue(phase < 0); |
420 |
< |
assertTrue(phaser.isTerminated()); |
412 |
< |
}}); |
413 |
< |
/* |
414 |
< |
* This thread will cause the first thread run to wait, in doing so |
415 |
< |
* the main thread will force termination in which the first thread |
416 |
< |
* should exit peacefully as this one |
417 |
< |
*/ |
418 |
< |
Thread t2 = newStartedThread(new CheckedRunnable() { |
419 |
< |
public void realRun() { |
420 |
< |
sleepTillInterrupted(MEDIUM_DELAY_MS); |
421 |
< |
int p1 = phaser.arrive(); |
422 |
< |
int phase = phaser.awaitAdvance(p1); |
423 |
< |
assertTrue(phase < 0); |
424 |
< |
assertTrue(phaser.isTerminated()); |
425 |
< |
}}); |
426 |
< |
|
427 |
< |
phaser.arrive(); |
406 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
407 |
> |
final List<Thread> threads = new ArrayList<Thread>(); |
408 |
> |
for (int i = 0; i < 2; i++) { |
409 |
> |
Runnable r = new CheckedRunnable() { |
410 |
> |
public void realRun() { |
411 |
> |
int p1 = phaser.arrive(); |
412 |
> |
assertTrue(p1 >= 0); |
413 |
> |
threadsStarted.countDown(); |
414 |
> |
int phase = phaser.awaitAdvance(p1); |
415 |
> |
assertTrue(phase < 0); |
416 |
> |
assertTrue(phaser.isTerminated()); |
417 |
> |
}}; |
418 |
> |
threads.add(newStartedThread(r)); |
419 |
> |
} |
420 |
> |
threadsStarted.await(); |
421 |
|
phaser.forceTermination(); |
422 |
< |
t1.join(); |
423 |
< |
t2.join(); |
422 |
> |
for (Thread thread : threads) |
423 |
> |
awaitTermination(thread, SMALL_DELAY_MS); |
424 |
|
} |
425 |
|
|
426 |
|
/** |