10 |
|
import java.util.List; |
11 |
|
import java.util.concurrent.Phaser; |
12 |
|
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.TimeoutException; |
14 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
16 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
255 |
|
/** |
256 |
|
* arriveAndDeregister does not wait for others to arrive at barrier |
257 |
|
*/ |
258 |
< |
public void testArriveAndDeregister() throws InterruptedException { |
258 |
> |
public void testArriveAndDeregister() { |
259 |
|
final Phaser phaser = new Phaser(1); |
260 |
|
for (int i = 0; i < 10; i++) { |
261 |
|
assertState(phaser, 0, 1, 1); |
271 |
|
/** |
272 |
|
* arriveAndDeregister does not wait for others to arrive at barrier |
273 |
|
*/ |
274 |
< |
public void testArrive2() throws InterruptedException { |
274 |
> |
public void testArrive2() { |
275 |
|
final Phaser phaser = new Phaser(); |
276 |
|
assertEquals(0, phaser.register()); |
277 |
|
List<Thread> threads = new ArrayList<Thread>(); |
278 |
|
for (int i = 0; i < 10; i++) { |
279 |
|
assertEquals(0, phaser.register()); |
280 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
281 |
< |
public void realRun() throws InterruptedException { |
281 |
> |
public void realRun() { |
282 |
|
assertEquals(0, phaser.arriveAndDeregister()); |
283 |
|
}})); |
284 |
|
} |
285 |
|
|
286 |
|
for (Thread thread : threads) |
287 |
< |
awaitTermination(thread, LONG_DELAY_MS); |
287 |
> |
awaitTermination(thread); |
288 |
|
assertState(phaser, 0, 1, 1); |
289 |
|
assertEquals(0, phaser.arrive()); |
290 |
|
assertState(phaser, 1, 1, 1); |
388 |
|
* arriveAndDeregister returns the phase in which it leaves the |
389 |
|
* phaser in after deregistration |
390 |
|
*/ |
391 |
< |
public void testArriveAndDeregister6() throws InterruptedException { |
391 |
> |
public void testArriveAndDeregister6() { |
392 |
|
final Phaser phaser = new Phaser(2); |
393 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
394 |
|
public void realRun() { |
400 |
|
assertState(phaser, 1, 1, 1); |
401 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
402 |
|
assertTerminated(phaser, 2); |
403 |
< |
awaitTermination(t, SHORT_DELAY_MS); |
403 |
> |
awaitTermination(t); |
404 |
|
} |
405 |
|
|
406 |
|
/** |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
+ |
* awaitAdvanceInterruptibly blocks interruptibly |
427 |
+ |
*/ |
428 |
+ |
public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException { |
429 |
+ |
final Phaser phaser = new Phaser(1); |
430 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
431 |
+ |
|
432 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
433 |
+ |
public void realRun() { |
434 |
+ |
Thread.currentThread().interrupt(); |
435 |
+ |
try { |
436 |
+ |
phaser.awaitAdvanceInterruptibly(0); |
437 |
+ |
shouldThrow(); |
438 |
+ |
} catch (InterruptedException success) {} |
439 |
+ |
assertFalse(Thread.interrupted()); |
440 |
+ |
|
441 |
+ |
pleaseInterrupt.countDown(); |
442 |
+ |
try { |
443 |
+ |
phaser.awaitAdvanceInterruptibly(0); |
444 |
+ |
shouldThrow(); |
445 |
+ |
} catch (InterruptedException success) {} |
446 |
+ |
assertFalse(Thread.interrupted()); |
447 |
+ |
}}); |
448 |
+ |
|
449 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
450 |
+ |
public void realRun() throws TimeoutException { |
451 |
+ |
Thread.currentThread().interrupt(); |
452 |
+ |
try { |
453 |
+ |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
454 |
+ |
shouldThrow(); |
455 |
+ |
} catch (InterruptedException success) {} |
456 |
+ |
assertFalse(Thread.interrupted()); |
457 |
+ |
|
458 |
+ |
pleaseInterrupt.countDown(); |
459 |
+ |
try { |
460 |
+ |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
461 |
+ |
shouldThrow(); |
462 |
+ |
} catch (InterruptedException success) {} |
463 |
+ |
assertFalse(Thread.interrupted()); |
464 |
+ |
}}); |
465 |
+ |
|
466 |
+ |
await(pleaseInterrupt); |
467 |
+ |
assertState(phaser, 0, 1, 1); |
468 |
+ |
assertThreadsStayAlive(t1, t2); |
469 |
+ |
t1.interrupt(); |
470 |
+ |
t2.interrupt(); |
471 |
+ |
awaitTermination(t1); |
472 |
+ |
awaitTermination(t2); |
473 |
+ |
assertState(phaser, 0, 1, 1); |
474 |
+ |
assertEquals(0, phaser.arrive()); |
475 |
+ |
assertState(phaser, 1, 1, 1); |
476 |
+ |
} |
477 |
+ |
|
478 |
+ |
/** |
479 |
|
* awaitAdvance continues waiting if interrupted before waiting |
480 |
|
*/ |
481 |
< |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
481 |
> |
public void testAwaitAdvanceAfterInterrupt() { |
482 |
|
final Phaser phaser = new Phaser(); |
483 |
|
assertEquals(0, phaser.register()); |
484 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
484 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
485 |
|
|
486 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
487 |
< |
public void realRun() throws InterruptedException { |
487 |
> |
public void realRun() { |
488 |
|
Thread.currentThread().interrupt(); |
489 |
|
assertEquals(0, phaser.register()); |
490 |
|
assertEquals(0, phaser.arrive()); |
491 |
< |
threadStarted.countDown(); |
491 |
> |
pleaseArrive.countDown(); |
492 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
493 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
494 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
494 |
> |
assertTrue(Thread.interrupted()); |
495 |
|
}}); |
496 |
|
|
497 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
498 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
497 |
> |
await(pleaseArrive); |
498 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
499 |
|
assertEquals(0, phaser.arrive()); |
500 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
500 |
> |
awaitTermination(t); |
501 |
|
|
502 |
|
Thread.currentThread().interrupt(); |
503 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
508 |
< |
* awaitAdvance continues waiting if interrupted while waiting |
508 |
> |
* awaitAdvance continues waiting if interrupted while waiting |
509 |
|
*/ |
510 |
< |
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
510 |
> |
public void testAwaitAdvanceBeforeInterrupt() { |
511 |
|
final Phaser phaser = new Phaser(); |
512 |
|
assertEquals(0, phaser.register()); |
513 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
513 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
514 |
|
|
515 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
516 |
< |
public void realRun() throws InterruptedException { |
516 |
> |
public void realRun() { |
517 |
|
assertEquals(0, phaser.register()); |
518 |
|
assertEquals(0, phaser.arrive()); |
465 |
– |
threadStarted.countDown(); |
519 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
520 |
+ |
pleaseArrive.countDown(); |
521 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
522 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
522 |
> |
assertTrue(Thread.interrupted()); |
523 |
|
}}); |
524 |
|
|
525 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
526 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
525 |
> |
await(pleaseArrive); |
526 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
527 |
|
t.interrupt(); |
528 |
|
assertEquals(0, phaser.arrive()); |
529 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
529 |
> |
awaitTermination(t); |
530 |
|
|
531 |
|
Thread.currentThread().interrupt(); |
532 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
536 |
|
/** |
537 |
|
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
538 |
|
*/ |
539 |
< |
public void testArriveAndAwaitAdvanceAfterInterrupt() |
486 |
< |
throws InterruptedException { |
539 |
> |
public void testArriveAndAwaitAdvanceAfterInterrupt() { |
540 |
|
final Phaser phaser = new Phaser(); |
541 |
|
assertEquals(0, phaser.register()); |
542 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
542 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
543 |
|
|
544 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
545 |
< |
public void realRun() throws InterruptedException { |
545 |
> |
public void realRun() { |
546 |
|
Thread.currentThread().interrupt(); |
547 |
|
assertEquals(0, phaser.register()); |
548 |
< |
threadStarted.countDown(); |
548 |
> |
pleaseInterrupt.countDown(); |
549 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
550 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
551 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
552 |
|
}}); |
553 |
|
|
554 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
555 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
554 |
> |
await(pleaseInterrupt); |
555 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
556 |
|
Thread.currentThread().interrupt(); |
557 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
558 |
|
assertTrue(Thread.interrupted()); |
559 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
559 |
> |
awaitTermination(t); |
560 |
|
} |
561 |
|
|
562 |
|
/** |
563 |
|
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
564 |
|
*/ |
565 |
< |
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
513 |
< |
throws InterruptedException { |
565 |
> |
public void testArriveAndAwaitAdvanceBeforeInterrupt() { |
566 |
|
final Phaser phaser = new Phaser(); |
567 |
|
assertEquals(0, phaser.register()); |
568 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
568 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
569 |
|
|
570 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
571 |
< |
public void realRun() throws InterruptedException { |
571 |
> |
public void realRun() { |
572 |
|
assertEquals(0, phaser.register()); |
521 |
– |
threadStarted.countDown(); |
573 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
574 |
+ |
pleaseInterrupt.countDown(); |
575 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
576 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
577 |
|
}}); |
578 |
|
|
579 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
580 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
579 |
> |
await(pleaseInterrupt); |
580 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
581 |
|
t.interrupt(); |
582 |
|
Thread.currentThread().interrupt(); |
583 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
584 |
|
assertTrue(Thread.interrupted()); |
585 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
585 |
> |
awaitTermination(t); |
586 |
|
} |
587 |
|
|
588 |
|
/** |
589 |
|
* awaitAdvance atomically waits for all parties within the same phase to |
590 |
|
* complete before continuing |
591 |
|
*/ |
592 |
< |
public void testAwaitAdvance4() throws InterruptedException { |
592 |
> |
public void testAwaitAdvance4() { |
593 |
|
final Phaser phaser = new Phaser(4); |
594 |
|
final AtomicInteger count = new AtomicInteger(0); |
595 |
|
List<Thread> threads = new ArrayList<Thread>(); |
605 |
|
}}})); |
606 |
|
|
607 |
|
for (Thread thread : threads) |
608 |
< |
awaitTermination(thread, MEDIUM_DELAY_MS); |
608 |
> |
awaitTermination(thread); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
|
* awaitAdvance returns the current phase |
613 |
|
*/ |
614 |
< |
public void testAwaitAdvance5() throws InterruptedException { |
614 |
> |
public void testAwaitAdvance5() { |
615 |
|
final Phaser phaser = new Phaser(1); |
616 |
|
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
617 |
|
assertEquals(1, phaser.getPhase()); |
621 |
|
final CountDownLatch latch = new CountDownLatch(1); |
622 |
|
final boolean goesFirst = ((i & 1) == 0); |
623 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
624 |
< |
public void realRun() throws InterruptedException { |
624 |
> |
public void realRun() { |
625 |
|
if (goesFirst) |
626 |
|
latch.countDown(); |
627 |
|
else |
628 |
< |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
628 |
> |
await(latch); |
629 |
|
phaser.arrive(); |
630 |
|
}})); |
631 |
|
if (goesFirst) |
632 |
< |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
632 |
> |
await(latch); |
633 |
|
else |
634 |
|
latch.countDown(); |
635 |
|
assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive())); |
636 |
|
assertEquals(i + 2, phaser.getPhase()); |
637 |
|
} |
638 |
|
for (Thread thread : threads) |
639 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
639 |
> |
awaitTermination(thread); |
640 |
|
} |
641 |
|
|
642 |
|
/** |
689 |
|
/** |
690 |
|
* awaitAdvance returns when the phaser is externally terminated |
691 |
|
*/ |
692 |
< |
public void testAwaitAdvance6() throws InterruptedException { |
692 |
> |
public void testAwaitAdvance6() { |
693 |
|
final Phaser phaser = new Phaser(3); |
694 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
694 |
> |
final CountDownLatch pleaseForceTermination = new CountDownLatch(2); |
695 |
|
final List<Thread> threads = new ArrayList<Thread>(); |
696 |
|
for (int i = 0; i < 2; i++) { |
697 |
|
Runnable r = new CheckedRunnable() { |
698 |
|
public void realRun() { |
699 |
|
assertEquals(0, phaser.arrive()); |
700 |
< |
threadsStarted.countDown(); |
700 |
> |
pleaseForceTermination.countDown(); |
701 |
|
assertTrue(phaser.awaitAdvance(0) < 0); |
702 |
|
assertTrue(phaser.isTerminated()); |
703 |
|
assertTrue(phaser.getPhase() < 0); |
706 |
|
}}; |
707 |
|
threads.add(newStartedThread(r)); |
708 |
|
} |
709 |
< |
threadsStarted.await(); |
709 |
> |
await(pleaseForceTermination); |
710 |
|
phaser.forceTermination(); |
711 |
|
assertTrue(phaser.isTerminated()); |
712 |
|
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
713 |
|
for (Thread thread : threads) |
714 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
714 |
> |
awaitTermination(thread); |
715 |
|
assertEquals(3, phaser.getRegisteredParties()); |
716 |
|
} |
717 |
|
|
732 |
|
* number of arrived parties is the same number that is accounted |
733 |
|
* for when the main thread awaitsAdvance |
734 |
|
*/ |
735 |
< |
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
735 |
> |
public void testArriveAndAwaitAdvance3() { |
736 |
|
final Phaser phaser = new Phaser(1); |
737 |
|
final int THREADS = 3; |
738 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
738 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(THREADS); |
739 |
|
final List<Thread> threads = new ArrayList<Thread>(); |
740 |
|
for (int i = 0; i < THREADS; i++) |
741 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
742 |
< |
public void realRun() throws InterruptedException { |
742 |
> |
public void realRun() { |
743 |
|
assertEquals(0, phaser.register()); |
744 |
< |
threadsStarted.countDown(); |
744 |
> |
pleaseArrive.countDown(); |
745 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
746 |
|
}})); |
747 |
|
|
748 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
749 |
< |
long t0 = System.nanoTime(); |
748 |
> |
await(pleaseArrive); |
749 |
> |
long startTime = System.nanoTime(); |
750 |
|
while (phaser.getArrivedParties() < THREADS) |
751 |
|
Thread.yield(); |
752 |
|
assertEquals(THREADS, phaser.getArrivedParties()); |
753 |
< |
assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS); |
753 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
754 |
> |
for (Thread thread : threads) |
755 |
> |
waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS); |
756 |
|
for (Thread thread : threads) |
757 |
|
assertTrue(thread.isAlive()); |
758 |
|
assertState(phaser, 0, THREADS + 1, 1); |
759 |
|
phaser.arriveAndAwaitAdvance(); |
760 |
|
for (Thread thread : threads) |
761 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
761 |
> |
awaitTermination(thread); |
762 |
|
assertState(phaser, 1, THREADS + 1, THREADS + 1); |
763 |
|
} |
764 |
|
|