5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
+ |
|
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.List; |
12 |
+ |
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.Phaser; |
14 |
+ |
import java.util.concurrent.TimeoutException; |
15 |
|
import java.util.concurrent.atomic.AtomicInteger; |
16 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
12 |
< |
import java.util.concurrent.*; |
13 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
16 |
> |
|
17 |
|
import junit.framework.Test; |
18 |
|
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class PhaserTest extends JSR166TestCase { |
21 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
|
26 |
|
public static Test suite() { |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* register() correctly returns the current barrier phase number when |
170 |
< |
* invoked |
169 |
> |
* register() correctly returns the current barrier phase number |
170 |
> |
* when invoked |
171 |
|
*/ |
172 |
|
public void testRegister3() { |
173 |
|
Phaser phaser = new Phaser(); |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* register causes the next arrive to not increment the phase rather retain |
182 |
< |
* the phase number |
181 |
> |
* register causes the next arrive to not increment the phase |
182 |
> |
* rather retain the phase number |
183 |
|
*/ |
184 |
|
public void testRegister4() { |
185 |
|
Phaser phaser = new Phaser(1); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
193 |
+ |
* register on a subphaser that is currently empty succeeds, even |
194 |
+ |
* in the presence of another non-empty subphaser |
195 |
+ |
*/ |
196 |
+ |
public void testRegisterEmptySubPhaser() { |
197 |
+ |
Phaser root = new Phaser(); |
198 |
+ |
Phaser child1 = new Phaser(root, 1); |
199 |
+ |
Phaser child2 = new Phaser(root, 0); |
200 |
+ |
assertEquals(0, child2.register()); |
201 |
+ |
assertState(root, 0, 2, 2); |
202 |
+ |
assertState(child1, 0, 1, 1); |
203 |
+ |
assertState(child2, 0, 1, 1); |
204 |
+ |
assertEquals(0, child2.arriveAndDeregister()); |
205 |
+ |
assertState(root, 0, 1, 1); |
206 |
+ |
assertState(child1, 0, 1, 1); |
207 |
+ |
assertState(child2, 0, 0, 0); |
208 |
+ |
assertEquals(0, child2.register()); |
209 |
+ |
assertEquals(0, child2.arriveAndDeregister()); |
210 |
+ |
assertState(root, 0, 1, 1); |
211 |
+ |
assertState(child1, 0, 1, 1); |
212 |
+ |
assertState(child2, 0, 0, 0); |
213 |
+ |
assertEquals(0, child1.arriveAndDeregister()); |
214 |
+ |
assertTerminated(root, 1); |
215 |
+ |
assertTerminated(child1, 1); |
216 |
+ |
assertTerminated(child2, 1); |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
/** |
220 |
|
* Invoking bulkRegister with a negative parameter throws an |
221 |
|
* IllegalArgumentException |
222 |
|
*/ |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* bulkRegister should correctly record the number of unarrived parties with |
232 |
< |
* the number of parties being registered |
231 |
> |
* bulkRegister should correctly record the number of unarrived |
232 |
> |
* parties with the number of parties being registered |
233 |
|
*/ |
234 |
|
public void testBulkRegister2() { |
235 |
|
Phaser phaser = new Phaser(); |
283 |
|
/** |
284 |
|
* arriveAndDeregister does not wait for others to arrive at barrier |
285 |
|
*/ |
286 |
< |
public void testArriveAndDeregister() throws InterruptedException { |
286 |
> |
public void testArriveAndDeregister() { |
287 |
|
final Phaser phaser = new Phaser(1); |
288 |
|
for (int i = 0; i < 10; i++) { |
289 |
|
assertState(phaser, 0, 1, 1); |
299 |
|
/** |
300 |
|
* arriveAndDeregister does not wait for others to arrive at barrier |
301 |
|
*/ |
302 |
< |
public void testArrive2() throws InterruptedException { |
302 |
> |
public void testArrive2() { |
303 |
|
final Phaser phaser = new Phaser(); |
304 |
|
assertEquals(0, phaser.register()); |
305 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
305 |
> |
List<Thread> threads = new ArrayList<>(); |
306 |
|
for (int i = 0; i < 10; i++) { |
307 |
|
assertEquals(0, phaser.register()); |
308 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
309 |
< |
public void realRun() throws InterruptedException { |
309 |
> |
public void realRun() { |
310 |
|
assertEquals(0, phaser.arriveAndDeregister()); |
311 |
|
}})); |
312 |
|
} |
313 |
|
|
314 |
|
for (Thread thread : threads) |
315 |
< |
awaitTermination(thread, LONG_DELAY_MS); |
315 |
> |
awaitTermination(thread); |
316 |
|
assertState(phaser, 0, 1, 1); |
317 |
|
assertEquals(0, phaser.arrive()); |
318 |
|
assertState(phaser, 1, 1, 1); |
338 |
|
* registered or unarrived parties would become negative |
339 |
|
*/ |
340 |
|
public void testArriveAndDeregister1() { |
341 |
+ |
Phaser phaser = new Phaser(); |
342 |
|
try { |
313 |
– |
Phaser phaser = new Phaser(); |
343 |
|
phaser.arriveAndDeregister(); |
344 |
|
shouldThrow(); |
345 |
|
} catch (IllegalStateException success) {} |
416 |
|
* arriveAndDeregister returns the phase in which it leaves the |
417 |
|
* phaser in after deregistration |
418 |
|
*/ |
419 |
< |
public void testArriveAndDeregister6() throws InterruptedException { |
419 |
> |
public void testArriveAndDeregister6() { |
420 |
|
final Phaser phaser = new Phaser(2); |
421 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
422 |
|
public void realRun() { |
428 |
|
assertState(phaser, 1, 1, 1); |
429 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
430 |
|
assertTerminated(phaser, 2); |
431 |
< |
awaitTermination(t, SHORT_DELAY_MS); |
431 |
> |
awaitTermination(t); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
+ |
* awaitAdvanceInterruptibly blocks interruptibly |
455 |
+ |
*/ |
456 |
+ |
public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException { |
457 |
+ |
final Phaser phaser = new Phaser(1); |
458 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
459 |
+ |
|
460 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
461 |
+ |
public void realRun() { |
462 |
+ |
Thread.currentThread().interrupt(); |
463 |
+ |
try { |
464 |
+ |
phaser.awaitAdvanceInterruptibly(0); |
465 |
+ |
shouldThrow(); |
466 |
+ |
} catch (InterruptedException success) {} |
467 |
+ |
assertFalse(Thread.interrupted()); |
468 |
+ |
|
469 |
+ |
pleaseInterrupt.countDown(); |
470 |
+ |
try { |
471 |
+ |
phaser.awaitAdvanceInterruptibly(0); |
472 |
+ |
shouldThrow(); |
473 |
+ |
} catch (InterruptedException success) {} |
474 |
+ |
assertFalse(Thread.interrupted()); |
475 |
+ |
}}); |
476 |
+ |
|
477 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
478 |
+ |
public void realRun() throws TimeoutException { |
479 |
+ |
Thread.currentThread().interrupt(); |
480 |
+ |
try { |
481 |
+ |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
482 |
+ |
shouldThrow(); |
483 |
+ |
} catch (InterruptedException success) {} |
484 |
+ |
assertFalse(Thread.interrupted()); |
485 |
+ |
|
486 |
+ |
pleaseInterrupt.countDown(); |
487 |
+ |
try { |
488 |
+ |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
489 |
+ |
shouldThrow(); |
490 |
+ |
} catch (InterruptedException success) {} |
491 |
+ |
assertFalse(Thread.interrupted()); |
492 |
+ |
}}); |
493 |
+ |
|
494 |
+ |
await(pleaseInterrupt); |
495 |
+ |
assertState(phaser, 0, 1, 1); |
496 |
+ |
assertThreadsStayAlive(t1, t2); |
497 |
+ |
t1.interrupt(); |
498 |
+ |
t2.interrupt(); |
499 |
+ |
awaitTermination(t1); |
500 |
+ |
awaitTermination(t2); |
501 |
+ |
assertState(phaser, 0, 1, 1); |
502 |
+ |
assertEquals(0, phaser.arrive()); |
503 |
+ |
assertState(phaser, 1, 1, 1); |
504 |
+ |
} |
505 |
+ |
|
506 |
+ |
/** |
507 |
|
* awaitAdvance continues waiting if interrupted before waiting |
508 |
|
*/ |
509 |
< |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
509 |
> |
public void testAwaitAdvanceAfterInterrupt() { |
510 |
|
final Phaser phaser = new Phaser(); |
511 |
|
assertEquals(0, phaser.register()); |
512 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
512 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
513 |
|
|
514 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
515 |
< |
public void realRun() throws InterruptedException { |
515 |
> |
public void realRun() { |
516 |
|
Thread.currentThread().interrupt(); |
517 |
|
assertEquals(0, phaser.register()); |
518 |
|
assertEquals(0, phaser.arrive()); |
519 |
< |
threadStarted.countDown(); |
519 |
> |
pleaseArrive.countDown(); |
520 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
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 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
527 |
|
assertEquals(0, phaser.arrive()); |
528 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
528 |
> |
awaitTermination(t); |
529 |
|
|
530 |
|
Thread.currentThread().interrupt(); |
531 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
533 |
|
} |
534 |
|
|
535 |
|
/** |
536 |
< |
* awaitAdvance continues waiting if interrupted while waiting |
536 |
> |
* awaitAdvance continues waiting if interrupted while waiting |
537 |
|
*/ |
538 |
< |
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
538 |
> |
public void testAwaitAdvanceBeforeInterrupt() { |
539 |
|
final Phaser phaser = new Phaser(); |
540 |
|
assertEquals(0, phaser.register()); |
541 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
541 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
542 |
|
|
543 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
544 |
< |
public void realRun() throws InterruptedException { |
544 |
> |
public void realRun() { |
545 |
|
assertEquals(0, phaser.register()); |
546 |
|
assertEquals(0, phaser.arrive()); |
465 |
– |
threadStarted.countDown(); |
547 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
548 |
+ |
pleaseArrive.countDown(); |
549 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
550 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
550 |
> |
assertTrue(Thread.interrupted()); |
551 |
|
}}); |
552 |
|
|
553 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
554 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
553 |
> |
await(pleaseArrive); |
554 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
555 |
|
t.interrupt(); |
556 |
|
assertEquals(0, phaser.arrive()); |
557 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
557 |
> |
awaitTermination(t); |
558 |
|
|
559 |
|
Thread.currentThread().interrupt(); |
560 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
564 |
|
/** |
565 |
|
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
566 |
|
*/ |
567 |
< |
public void testArriveAndAwaitAdvanceAfterInterrupt() |
486 |
< |
throws InterruptedException { |
567 |
> |
public void testArriveAndAwaitAdvanceAfterInterrupt() { |
568 |
|
final Phaser phaser = new Phaser(); |
569 |
|
assertEquals(0, phaser.register()); |
570 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
570 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
571 |
|
|
572 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
573 |
< |
public void realRun() throws InterruptedException { |
573 |
> |
public void realRun() { |
574 |
|
Thread.currentThread().interrupt(); |
575 |
|
assertEquals(0, phaser.register()); |
576 |
< |
threadStarted.countDown(); |
576 |
> |
pleaseArrive.countDown(); |
577 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
578 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
579 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
579 |
> |
assertTrue(Thread.interrupted()); |
580 |
|
}}); |
581 |
|
|
582 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
583 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
582 |
> |
await(pleaseArrive); |
583 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
584 |
|
Thread.currentThread().interrupt(); |
585 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
586 |
|
assertTrue(Thread.interrupted()); |
587 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
587 |
> |
awaitTermination(t); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
|
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
592 |
|
*/ |
593 |
< |
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
513 |
< |
throws InterruptedException { |
593 |
> |
public void testArriveAndAwaitAdvanceBeforeInterrupt() { |
594 |
|
final Phaser phaser = new Phaser(); |
595 |
|
assertEquals(0, phaser.register()); |
596 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
596 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
597 |
|
|
598 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
599 |
< |
public void realRun() throws InterruptedException { |
599 |
> |
public void realRun() { |
600 |
|
assertEquals(0, phaser.register()); |
521 |
– |
threadStarted.countDown(); |
601 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
602 |
+ |
pleaseInterrupt.countDown(); |
603 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
604 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
604 |
> |
assertTrue(Thread.interrupted()); |
605 |
|
}}); |
606 |
|
|
607 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
608 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
607 |
> |
await(pleaseInterrupt); |
608 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
609 |
|
t.interrupt(); |
610 |
|
Thread.currentThread().interrupt(); |
611 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
612 |
|
assertTrue(Thread.interrupted()); |
613 |
< |
awaitTermination(t, SMALL_DELAY_MS); |
613 |
> |
awaitTermination(t); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
|
* awaitAdvance atomically waits for all parties within the same phase to |
618 |
|
* complete before continuing |
619 |
|
*/ |
620 |
< |
public void testAwaitAdvance4() throws InterruptedException { |
620 |
> |
public void testAwaitAdvance4() { |
621 |
|
final Phaser phaser = new Phaser(4); |
622 |
|
final AtomicInteger count = new AtomicInteger(0); |
623 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
623 |
> |
List<Thread> threads = new ArrayList<>(); |
624 |
|
for (int i = 0; i < 4; i++) |
625 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
626 |
|
public void realRun() { |
627 |
|
for (int k = 0; k < 3; k++) { |
628 |
< |
assertEquals(2*k+1, phaser.arriveAndAwaitAdvance()); |
628 |
> |
assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance()); |
629 |
|
count.incrementAndGet(); |
630 |
< |
assertEquals(2*k+1, phaser.arrive()); |
631 |
< |
assertEquals(2*k+2, phaser.awaitAdvance(2*k+1)); |
632 |
< |
assertEquals(count.get(), 4*(k+1)); |
630 |
> |
assertEquals(2 * k + 1, phaser.arrive()); |
631 |
> |
assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1)); |
632 |
> |
assertEquals(4 * (k + 1), count.get()); |
633 |
|
}}})); |
634 |
|
|
635 |
|
for (Thread thread : threads) |
636 |
< |
awaitTermination(thread, MEDIUM_DELAY_MS); |
636 |
> |
awaitTermination(thread); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
|
* awaitAdvance returns the current phase |
641 |
|
*/ |
642 |
< |
public void testAwaitAdvance5() throws InterruptedException { |
642 |
> |
public void testAwaitAdvance5() { |
643 |
|
final Phaser phaser = new Phaser(1); |
644 |
|
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
645 |
|
assertEquals(1, phaser.getPhase()); |
646 |
|
assertEquals(1, phaser.register()); |
647 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
647 |
> |
List<Thread> threads = new ArrayList<>(); |
648 |
|
for (int i = 0; i < 8; i++) { |
649 |
|
final CountDownLatch latch = new CountDownLatch(1); |
650 |
|
final boolean goesFirst = ((i & 1) == 0); |
651 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
652 |
< |
public void realRun() throws InterruptedException { |
652 |
> |
public void realRun() { |
653 |
|
if (goesFirst) |
654 |
|
latch.countDown(); |
655 |
|
else |
656 |
< |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
656 |
> |
await(latch); |
657 |
|
phaser.arrive(); |
658 |
|
}})); |
659 |
|
if (goesFirst) |
660 |
< |
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
660 |
> |
await(latch); |
661 |
|
else |
662 |
|
latch.countDown(); |
663 |
|
assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive())); |
664 |
|
assertEquals(i + 2, phaser.getPhase()); |
665 |
|
} |
666 |
|
for (Thread thread : threads) |
667 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
667 |
> |
awaitTermination(thread); |
668 |
|
} |
669 |
|
|
670 |
|
/** |
672 |
|
*/ |
673 |
|
public void testAwaitAdvanceTieredPhaser() throws Exception { |
674 |
|
final Phaser parent = new Phaser(); |
675 |
< |
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
676 |
< |
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
675 |
> |
final List<Phaser> zeroPartyChildren = new ArrayList<>(3); |
676 |
> |
final List<Phaser> onePartyChildren = new ArrayList<>(3); |
677 |
|
for (int i = 0; i < 3; i++) { |
678 |
|
zeroPartyChildren.add(new Phaser(parent, 0)); |
679 |
|
onePartyChildren.add(new Phaser(parent, 1)); |
680 |
|
} |
681 |
< |
final List<Phaser> phasers = new ArrayList<Phaser>(); |
681 |
> |
final List<Phaser> phasers = new ArrayList<>(); |
682 |
|
phasers.addAll(zeroPartyChildren); |
683 |
|
phasers.addAll(onePartyChildren); |
684 |
|
phasers.add(parent); |
685 |
|
for (Phaser phaser : phasers) { |
686 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
687 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
688 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
688 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
689 |
|
} |
690 |
|
|
691 |
|
for (Phaser child : onePartyChildren) |
693 |
|
for (Phaser phaser : phasers) { |
694 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
695 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
696 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
696 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
697 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
698 |
|
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
699 |
< |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
699 |
> |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); |
700 |
|
} |
701 |
|
|
702 |
|
for (Phaser child : onePartyChildren) |
704 |
|
for (Phaser phaser : phasers) { |
705 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
706 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
707 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
707 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
708 |
|
assertEquals(2, phaser.awaitAdvance(0)); |
709 |
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
710 |
< |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
710 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); |
711 |
|
assertEquals(2, phaser.awaitAdvance(1)); |
712 |
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
713 |
< |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
713 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS)); |
714 |
|
} |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* awaitAdvance returns when the phaser is externally terminated |
719 |
|
*/ |
720 |
< |
public void testAwaitAdvance6() throws InterruptedException { |
720 |
> |
public void testAwaitAdvance6() { |
721 |
|
final Phaser phaser = new Phaser(3); |
722 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
723 |
< |
final List<Thread> threads = new ArrayList<Thread>(); |
722 |
> |
final CountDownLatch pleaseForceTermination = new CountDownLatch(2); |
723 |
> |
final List<Thread> threads = new ArrayList<>(); |
724 |
|
for (int i = 0; i < 2; i++) { |
725 |
|
Runnable r = new CheckedRunnable() { |
726 |
|
public void realRun() { |
727 |
|
assertEquals(0, phaser.arrive()); |
728 |
< |
threadsStarted.countDown(); |
728 |
> |
pleaseForceTermination.countDown(); |
729 |
|
assertTrue(phaser.awaitAdvance(0) < 0); |
730 |
|
assertTrue(phaser.isTerminated()); |
731 |
|
assertTrue(phaser.getPhase() < 0); |
734 |
|
}}; |
735 |
|
threads.add(newStartedThread(r)); |
736 |
|
} |
737 |
< |
threadsStarted.await(); |
737 |
> |
await(pleaseForceTermination); |
738 |
|
phaser.forceTermination(); |
739 |
|
assertTrue(phaser.isTerminated()); |
740 |
|
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
741 |
|
for (Thread thread : threads) |
742 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
742 |
> |
awaitTermination(thread); |
743 |
|
assertEquals(3, phaser.getRegisteredParties()); |
744 |
|
} |
745 |
|
|
748 |
|
* unarrived parties |
749 |
|
*/ |
750 |
|
public void testArriveAndAwaitAdvance1() { |
751 |
+ |
Phaser phaser = new Phaser(); |
752 |
|
try { |
672 |
– |
Phaser phaser = new Phaser(); |
753 |
|
phaser.arriveAndAwaitAdvance(); |
754 |
|
shouldThrow(); |
755 |
|
} catch (IllegalStateException success) {} |
760 |
|
* number of arrived parties is the same number that is accounted |
761 |
|
* for when the main thread awaitsAdvance |
762 |
|
*/ |
763 |
< |
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
763 |
> |
public void testArriveAndAwaitAdvance3() { |
764 |
|
final Phaser phaser = new Phaser(1); |
765 |
|
final int THREADS = 3; |
766 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
767 |
< |
final List<Thread> threads = new ArrayList<Thread>(); |
766 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(THREADS); |
767 |
> |
final List<Thread> threads = new ArrayList<>(); |
768 |
|
for (int i = 0; i < THREADS; i++) |
769 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
770 |
< |
public void realRun() throws InterruptedException { |
770 |
> |
public void realRun() { |
771 |
|
assertEquals(0, phaser.register()); |
772 |
< |
threadsStarted.countDown(); |
772 |
> |
pleaseArrive.countDown(); |
773 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
774 |
|
}})); |
775 |
|
|
776 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
777 |
< |
long t0 = System.nanoTime(); |
776 |
> |
await(pleaseArrive); |
777 |
> |
long startTime = System.nanoTime(); |
778 |
|
while (phaser.getArrivedParties() < THREADS) |
779 |
|
Thread.yield(); |
780 |
|
assertEquals(THREADS, phaser.getArrivedParties()); |
781 |
< |
assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS); |
781 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
782 |
> |
for (Thread thread : threads) |
783 |
> |
assertThreadBlocks(thread, Thread.State.WAITING); |
784 |
|
for (Thread thread : threads) |
785 |
|
assertTrue(thread.isAlive()); |
786 |
|
assertState(phaser, 0, THREADS + 1, 1); |
787 |
|
phaser.arriveAndAwaitAdvance(); |
788 |
|
for (Thread thread : threads) |
789 |
< |
awaitTermination(thread, SMALL_DELAY_MS); |
789 |
> |
awaitTermination(thread); |
790 |
|
assertState(phaser, 1, THREADS + 1, THREADS + 1); |
791 |
|
} |
792 |
|
|