8 |
|
import java.util.ArrayList; |
9 |
|
import java.util.List; |
10 |
|
import java.util.concurrent.atomic.AtomicInteger; |
11 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import junit.framework.Test; |
15 |
|
import junit.framework.TestSuite; |
16 |
|
|
330 |
|
public void testAwaitAdvance3() throws InterruptedException { |
331 |
|
final Phaser phaser = new Phaser(); |
332 |
|
phaser.register(); |
333 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
334 |
|
|
335 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
336 |
|
public void realRun() throws InterruptedException { |
337 |
|
phaser.register(); |
338 |
< |
sleepTillInterrupted(LONG_DELAY_MS); |
338 |
> |
threadStarted.countDown(); |
339 |
|
phaser.awaitAdvance(phaser.arrive()); |
340 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
341 |
|
}}); |
342 |
< |
Thread.sleep(SMALL_DELAY_MS); |
342 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
343 |
|
t.interrupt(); |
340 |
– |
Thread.sleep(SMALL_DELAY_MS); |
344 |
|
phaser.arrive(); |
345 |
< |
assertFalse(t.isInterrupted()); |
343 |
< |
t.join(); |
345 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
447 |
|
*/ |
448 |
|
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
449 |
|
final Phaser phaser = new Phaser(2); |
450 |
< |
Thread th = newStartedThread(new CheckedRunnable() { |
451 |
< |
public void realRun() { |
450 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
451 |
> |
final AtomicBoolean advanced = new AtomicBoolean(false); |
452 |
> |
final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false); |
453 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
454 |
> |
public void realRun() throws InterruptedException { |
455 |
> |
threadStarted.countDown(); |
456 |
|
phaser.arriveAndAwaitAdvance(); |
457 |
+ |
advanced.set(true); |
458 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
459 |
+ |
while (!checkedInterruptStatus.get()) |
460 |
+ |
Thread.yield(); |
461 |
|
}}); |
462 |
|
|
463 |
< |
Thread.sleep(SMALL_DELAY_MS); |
464 |
< |
th.interrupt(); |
455 |
< |
Thread.sleep(SMALL_DELAY_MS); |
463 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
464 |
> |
t.interrupt(); |
465 |
|
phaser.arrive(); |
466 |
< |
assertFalse(th.isInterrupted()); |
467 |
< |
th.join(); |
466 |
> |
while (!advanced.get()) |
467 |
> |
Thread.yield(); |
468 |
> |
assertTrue(t.isInterrupted()); |
469 |
> |
checkedInterruptStatus.set(true); |
470 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
471 |
|
} |
472 |
|
|
473 |
|
/** |