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 |
|
|
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 |
|
/** |