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; |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
< |
* register() correctly returns the current barrier phase number when |
169 |
< |
* invoked |
168 |
> |
* register() correctly returns the current barrier phase number |
169 |
> |
* when invoked |
170 |
|
*/ |
171 |
|
public void testRegister3() { |
172 |
|
Phaser phaser = new Phaser(); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
< |
* register causes the next arrive to not increment the phase rather retain |
181 |
< |
* the phase number |
180 |
> |
* register causes the next arrive to not increment the phase |
181 |
> |
* rather retain the phase number |
182 |
|
*/ |
183 |
|
public void testRegister4() { |
184 |
|
Phaser phaser = new Phaser(1); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
+ |
* register on a subphaser that is currently empty succeeds, even |
193 |
+ |
* in the presence of another non-empty subphaser |
194 |
+ |
*/ |
195 |
+ |
public void testRegisterEmptySubPhaser() { |
196 |
+ |
Phaser root = new Phaser(); |
197 |
+ |
Phaser child1 = new Phaser(root, 1); |
198 |
+ |
Phaser child2 = new Phaser(root, 0); |
199 |
+ |
assertEquals(0, child2.register()); |
200 |
+ |
assertEquals(0, child2.arriveAndDeregister()); |
201 |
+ |
assertEquals(0, child2.register()); |
202 |
+ |
assertEquals(0, child2.arriveAndDeregister()); |
203 |
+ |
assertState(child2, 0, 0, 0); |
204 |
+ |
} |
205 |
+ |
|
206 |
+ |
/** |
207 |
|
* Invoking bulkRegister with a negative parameter throws an |
208 |
|
* IllegalArgumentException |
209 |
|
*/ |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
< |
* bulkRegister should correctly record the number of unarrived parties with |
219 |
< |
* the number of parties being registered |
218 |
> |
* bulkRegister should correctly record the number of unarrived |
219 |
> |
* parties with the number of parties being registered |
220 |
|
*/ |
221 |
|
public void testBulkRegister2() { |
222 |
|
Phaser phaser = new Phaser(); |
270 |
|
/** |
271 |
|
* arriveAndDeregister does not wait for others to arrive at barrier |
272 |
|
*/ |
273 |
< |
public void testArriveAndDeregister() throws InterruptedException { |
273 |
> |
public void testArriveAndDeregister() { |
274 |
|
final Phaser phaser = new Phaser(1); |
275 |
|
for (int i = 0; i < 10; i++) { |
276 |
|
assertState(phaser, 0, 1, 1); |
286 |
|
/** |
287 |
|
* arriveAndDeregister does not wait for others to arrive at barrier |
288 |
|
*/ |
289 |
< |
public void testArrive2() throws InterruptedException { |
289 |
> |
public void testArrive2() { |
290 |
|
final Phaser phaser = new Phaser(); |
291 |
|
assertEquals(0, phaser.register()); |
292 |
|
List<Thread> threads = new ArrayList<Thread>(); |
293 |
|
for (int i = 0; i < 10; i++) { |
294 |
|
assertEquals(0, phaser.register()); |
295 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
296 |
< |
public void realRun() throws InterruptedException { |
296 |
> |
public void realRun() { |
297 |
|
assertEquals(0, phaser.arriveAndDeregister()); |
298 |
|
}})); |
299 |
|
} |
300 |
|
|
301 |
|
for (Thread thread : threads) |
302 |
< |
awaitTermination(thread, LONG_DELAY_MS); |
302 |
> |
awaitTermination(thread); |
303 |
|
assertState(phaser, 0, 1, 1); |
304 |
|
assertEquals(0, phaser.arrive()); |
305 |
|
assertState(phaser, 1, 1, 1); |
403 |
|
* arriveAndDeregister returns the phase in which it leaves the |
404 |
|
* phaser in after deregistration |
405 |
|
*/ |
406 |
< |
public void testArriveAndDeregister6() throws InterruptedException { |
406 |
> |
public void testArriveAndDeregister6() { |
407 |
|
final Phaser phaser = new Phaser(2); |
408 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
409 |
|
public void realRun() { |
415 |
|
assertState(phaser, 1, 1, 1); |
416 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
417 |
|
assertTerminated(phaser, 2); |
418 |
< |
awaitTermination(t, SHORT_DELAY_MS); |
418 |
> |
awaitTermination(t); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
+ |
* awaitAdvanceInterruptibly blocks interruptibly |
442 |
+ |
*/ |
443 |
+ |
public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException { |
444 |
+ |
final Phaser phaser = new Phaser(1); |
445 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
446 |
+ |
|
447 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
448 |
+ |
public void realRun() { |
449 |
+ |
Thread.currentThread().interrupt(); |
450 |
+ |
try { |
451 |
+ |
phaser.awaitAdvanceInterruptibly(0); |
452 |
+ |
shouldThrow(); |
453 |
+ |
} catch (InterruptedException success) {} |
454 |
+ |
assertFalse(Thread.interrupted()); |
455 |
+ |
|
456 |
+ |
pleaseInterrupt.countDown(); |
457 |
+ |
try { |
458 |
+ |
phaser.awaitAdvanceInterruptibly(0); |
459 |
+ |
shouldThrow(); |
460 |
+ |
} catch (InterruptedException success) {} |
461 |
+ |
assertFalse(Thread.interrupted()); |
462 |
+ |
}}); |
463 |
+ |
|
464 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
465 |
+ |
public void realRun() throws TimeoutException { |
466 |
+ |
Thread.currentThread().interrupt(); |
467 |
+ |
try { |
468 |
+ |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
469 |
+ |
shouldThrow(); |
470 |
+ |
} catch (InterruptedException success) {} |
471 |
+ |
assertFalse(Thread.interrupted()); |
472 |
+ |
|
473 |
+ |
pleaseInterrupt.countDown(); |
474 |
+ |
try { |
475 |
+ |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
476 |
+ |
shouldThrow(); |
477 |
+ |
} catch (InterruptedException success) {} |
478 |
+ |
assertFalse(Thread.interrupted()); |
479 |
+ |
}}); |
480 |
+ |
|
481 |
+ |
await(pleaseInterrupt); |
482 |
+ |
assertState(phaser, 0, 1, 1); |
483 |
+ |
assertThreadsStayAlive(t1, t2); |
484 |
+ |
t1.interrupt(); |
485 |
+ |
t2.interrupt(); |
486 |
+ |
awaitTermination(t1); |
487 |
+ |
awaitTermination(t2); |
488 |
+ |
assertState(phaser, 0, 1, 1); |
489 |
+ |
assertEquals(0, phaser.arrive()); |
490 |
+ |
assertState(phaser, 1, 1, 1); |
491 |
+ |
} |
492 |
+ |
|
493 |
+ |
/** |
494 |
|
* awaitAdvance continues waiting if interrupted before waiting |
495 |
|
*/ |
496 |
< |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
496 |
> |
public void testAwaitAdvanceAfterInterrupt() { |
497 |
|
final Phaser phaser = new Phaser(); |
498 |
|
assertEquals(0, phaser.register()); |
499 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
499 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
500 |
|
|
501 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
502 |
< |
public void realRun() throws InterruptedException { |
502 |
> |
public void realRun() { |
503 |
|
Thread.currentThread().interrupt(); |
504 |
|
assertEquals(0, phaser.register()); |
505 |
|
assertEquals(0, phaser.arrive()); |
506 |
< |
threadStarted.countDown(); |
506 |
> |
pleaseArrive.countDown(); |
507 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
508 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
509 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
509 |
> |
assertTrue(Thread.interrupted()); |
510 |
|
}}); |
511 |
|
|
512 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
513 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
512 |
> |
await(pleaseArrive); |
513 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
514 |
|
assertEquals(0, phaser.arrive()); |
515 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
515 |
> |
awaitTermination(t); |
516 |
|
|
517 |
|
Thread.currentThread().interrupt(); |
518 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
< |
* awaitAdvance continues waiting if interrupted while waiting |
523 |
> |
* awaitAdvance continues waiting if interrupted while waiting |
524 |
|
*/ |
525 |
< |
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
525 |
> |
public void testAwaitAdvanceBeforeInterrupt() { |
526 |
|
final Phaser phaser = new Phaser(); |
527 |
|
assertEquals(0, phaser.register()); |
528 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
528 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
529 |
|
|
530 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
531 |
< |
public void realRun() throws InterruptedException { |
531 |
> |
public void realRun() { |
532 |
|
assertEquals(0, phaser.register()); |
533 |
|
assertEquals(0, phaser.arrive()); |
465 |
– |
threadStarted.countDown(); |
534 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
535 |
+ |
pleaseArrive.countDown(); |
536 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
537 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
537 |
> |
assertTrue(Thread.interrupted()); |
538 |
|
}}); |
539 |
|
|
540 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
541 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
540 |
> |
await(pleaseArrive); |
541 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
542 |
|
t.interrupt(); |
543 |
|
assertEquals(0, phaser.arrive()); |
544 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
544 |
> |
awaitTermination(t); |
545 |
|
|
546 |
|
Thread.currentThread().interrupt(); |
547 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
551 |
|
/** |
552 |
|
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
553 |
|
*/ |
554 |
< |
public void testArriveAndAwaitAdvanceAfterInterrupt() |
486 |
< |
throws InterruptedException { |
554 |
> |
public void testArriveAndAwaitAdvanceAfterInterrupt() { |
555 |
|
final Phaser phaser = new Phaser(); |
556 |
|
assertEquals(0, phaser.register()); |
557 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
557 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
558 |
|
|
559 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
560 |
< |
public void realRun() throws InterruptedException { |
560 |
> |
public void realRun() { |
561 |
|
Thread.currentThread().interrupt(); |
562 |
|
assertEquals(0, phaser.register()); |
563 |
< |
threadStarted.countDown(); |
563 |
> |
pleaseInterrupt.countDown(); |
564 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
565 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
566 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
567 |
|
}}); |
568 |
|
|
569 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
570 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
569 |
> |
await(pleaseInterrupt); |
570 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
571 |
|
Thread.currentThread().interrupt(); |
572 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
573 |
|
assertTrue(Thread.interrupted()); |
574 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
574 |
> |
awaitTermination(t); |
575 |
|
} |
576 |
|
|
577 |
|
/** |
578 |
|
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
579 |
|
*/ |
580 |
< |
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
513 |
< |
throws InterruptedException { |
580 |
> |
public void testArriveAndAwaitAdvanceBeforeInterrupt() { |
581 |
|
final Phaser phaser = new Phaser(); |
582 |
|
assertEquals(0, phaser.register()); |
583 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
583 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
584 |
|
|
585 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
586 |
< |
public void realRun() throws InterruptedException { |
586 |
> |
public void realRun() { |
587 |
|
assertEquals(0, phaser.register()); |
521 |
– |
threadStarted.countDown(); |
588 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
589 |
+ |
pleaseInterrupt.countDown(); |
590 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
591 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
592 |
|
}}); |
593 |
|
|
594 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
595 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
594 |
> |
await(pleaseInterrupt); |
595 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
596 |
|
t.interrupt(); |
597 |
|
Thread.currentThread().interrupt(); |
598 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
599 |
|
assertTrue(Thread.interrupted()); |
600 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
600 |
> |
awaitTermination(t); |
601 |
|
} |
602 |
|
|
603 |
|
/** |
604 |
|
* awaitAdvance atomically waits for all parties within the same phase to |
605 |
|
* complete before continuing |
606 |
|
*/ |
607 |
< |
public void testAwaitAdvance4() throws InterruptedException { |
607 |
> |
public void testAwaitAdvance4() { |
608 |
|
final Phaser phaser = new Phaser(4); |
609 |
|
final AtomicInteger count = new AtomicInteger(0); |
610 |
|
List<Thread> threads = new ArrayList<Thread>(); |
620 |
|
}}})); |
621 |
|
|
622 |
|
for (Thread thread : threads) |
623 |
< |
awaitTermination(thread, MEDIUM_DELAY_MS); |
623 |
> |
awaitTermination(thread); |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
|
* awaitAdvance returns the current phase |
628 |
|
*/ |
629 |
< |
public void testAwaitAdvance5() throws InterruptedException { |
629 |
> |
public void testAwaitAdvance5() { |
630 |
|
final Phaser phaser = new Phaser(1); |
631 |
|
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
632 |
|
assertEquals(1, phaser.getPhase()); |
636 |
|
final CountDownLatch latch = new CountDownLatch(1); |
637 |
|
final boolean goesFirst = ((i & 1) == 0); |
638 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
639 |
< |
public void realRun() throws InterruptedException { |
639 |
> |
public void realRun() { |
640 |
|
if (goesFirst) |
641 |
|
latch.countDown(); |
642 |
|
else |
643 |
< |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
643 |
> |
await(latch); |
644 |
|
phaser.arrive(); |
645 |
|
}})); |
646 |
|
if (goesFirst) |
647 |
< |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
647 |
> |
await(latch); |
648 |
|
else |
649 |
|
latch.countDown(); |
650 |
|
assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive())); |
651 |
|
assertEquals(i + 2, phaser.getPhase()); |
652 |
|
} |
653 |
|
for (Thread thread : threads) |
654 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
654 |
> |
awaitTermination(thread); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
704 |
|
/** |
705 |
|
* awaitAdvance returns when the phaser is externally terminated |
706 |
|
*/ |
707 |
< |
public void testAwaitAdvance6() throws InterruptedException { |
707 |
> |
public void testAwaitAdvance6() { |
708 |
|
final Phaser phaser = new Phaser(3); |
709 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
709 |
> |
final CountDownLatch pleaseForceTermination = new CountDownLatch(2); |
710 |
|
final List<Thread> threads = new ArrayList<Thread>(); |
711 |
|
for (int i = 0; i < 2; i++) { |
712 |
|
Runnable r = new CheckedRunnable() { |
713 |
|
public void realRun() { |
714 |
|
assertEquals(0, phaser.arrive()); |
715 |
< |
threadsStarted.countDown(); |
715 |
> |
pleaseForceTermination.countDown(); |
716 |
|
assertTrue(phaser.awaitAdvance(0) < 0); |
717 |
|
assertTrue(phaser.isTerminated()); |
718 |
|
assertTrue(phaser.getPhase() < 0); |
721 |
|
}}; |
722 |
|
threads.add(newStartedThread(r)); |
723 |
|
} |
724 |
< |
threadsStarted.await(); |
724 |
> |
await(pleaseForceTermination); |
725 |
|
phaser.forceTermination(); |
726 |
|
assertTrue(phaser.isTerminated()); |
727 |
|
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
728 |
|
for (Thread thread : threads) |
729 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
729 |
> |
awaitTermination(thread); |
730 |
|
assertEquals(3, phaser.getRegisteredParties()); |
731 |
|
} |
732 |
|
|
747 |
|
* number of arrived parties is the same number that is accounted |
748 |
|
* for when the main thread awaitsAdvance |
749 |
|
*/ |
750 |
< |
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
750 |
> |
public void testArriveAndAwaitAdvance3() { |
751 |
|
final Phaser phaser = new Phaser(1); |
752 |
|
final int THREADS = 3; |
753 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
753 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(THREADS); |
754 |
|
final List<Thread> threads = new ArrayList<Thread>(); |
755 |
|
for (int i = 0; i < THREADS; i++) |
756 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
757 |
< |
public void realRun() throws InterruptedException { |
757 |
> |
public void realRun() { |
758 |
|
assertEquals(0, phaser.register()); |
759 |
< |
threadsStarted.countDown(); |
759 |
> |
pleaseArrive.countDown(); |
760 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
761 |
|
}})); |
762 |
|
|
763 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
764 |
< |
long t0 = System.nanoTime(); |
763 |
> |
await(pleaseArrive); |
764 |
> |
long startTime = System.nanoTime(); |
765 |
|
while (phaser.getArrivedParties() < THREADS) |
766 |
|
Thread.yield(); |
767 |
|
assertEquals(THREADS, phaser.getArrivedParties()); |
768 |
< |
assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS); |
768 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
769 |
> |
for (Thread thread : threads) |
770 |
> |
waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS); |
771 |
|
for (Thread thread : threads) |
772 |
|
assertTrue(thread.isAlive()); |
773 |
|
assertState(phaser, 0, THREADS + 1, 1); |
774 |
|
phaser.arriveAndAwaitAdvance(); |
775 |
|
for (Thread thread : threads) |
776 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
776 |
> |
awaitTermination(thread); |
777 |
|
assertState(phaser, 1, THREADS + 1, THREADS + 1); |
778 |
|
} |
779 |
|
|