1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
+ |
import junit.framework.*; |
9 |
|
import java.util.ArrayList; |
10 |
|
import java.util.List; |
11 |
< |
import java.util.concurrent.atomic.AtomicInteger; |
12 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
12 |
< |
import java.util.concurrent.*; |
11 |
> |
import java.util.concurrent.Phaser; |
12 |
> |
import java.util.concurrent.CountDownLatch; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
< |
import junit.framework.Test; |
16 |
< |
import junit.framework.TestSuite; |
15 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
17 |
|
|
18 |
|
public class PhaserTest extends JSR166TestCase { |
19 |
|
|
25 |
|
return new TestSuite(PhaserTest.class); |
26 |
|
} |
27 |
|
|
28 |
< |
/** Checks state of phaser. */ |
28 |
> |
private static final int maxParties = 65535; |
29 |
> |
|
30 |
> |
/** Checks state of unterminated phaser. */ |
31 |
|
protected void assertState(Phaser phaser, |
32 |
|
int phase, int parties, int unarrived) { |
33 |
|
assertEquals(phase, phaser.getPhase()); |
34 |
|
assertEquals(parties, phaser.getRegisteredParties()); |
35 |
|
assertEquals(unarrived, phaser.getUnarrivedParties()); |
36 |
|
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
37 |
< |
assertTrue((phaser.getPhase() >= 0) ^ phaser.isTerminated()); |
37 |
> |
assertFalse(phaser.isTerminated()); |
38 |
|
} |
39 |
|
|
40 |
|
/** Checks state of terminated phaser. */ |
41 |
< |
protected void assertTerminated(Phaser phaser, int parties, int unarrived) { |
41 |
> |
protected void assertTerminated(Phaser phaser, int maxPhase, int parties) { |
42 |
|
assertTrue(phaser.isTerminated()); |
43 |
< |
assertTrue(phaser.getPhase() < 0); |
43 |
> |
int expectedPhase = maxPhase + Integer.MIN_VALUE; |
44 |
> |
assertEquals(expectedPhase, phaser.getPhase()); |
45 |
|
assertEquals(parties, phaser.getRegisteredParties()); |
46 |
< |
assertEquals(unarrived, phaser.getUnarrivedParties()); |
47 |
< |
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
46 |
> |
assertEquals(expectedPhase, phaser.register()); |
47 |
> |
assertEquals(expectedPhase, phaser.arrive()); |
48 |
> |
assertEquals(expectedPhase, phaser.arriveAndDeregister()); |
49 |
|
} |
50 |
|
|
51 |
< |
protected void assertTerminated(Phaser phaser) { |
52 |
< |
assertTerminated(phaser, 0, 0); |
51 |
> |
protected void assertTerminated(Phaser phaser, int maxPhase) { |
52 |
> |
assertTerminated(phaser, maxPhase, 0); |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
|
* Empty constructor builds a new Phaser with no parent, no registered |
57 |
|
* parties and initial phase number of 0 |
58 |
|
*/ |
59 |
< |
public void testConstructor1() { |
59 |
> |
public void testConstructorDefaultValues() { |
60 |
|
Phaser phaser = new Phaser(); |
61 |
|
assertNull(phaser.getParent()); |
62 |
+ |
assertEquals(0, phaser.getRegisteredParties()); |
63 |
|
assertEquals(0, phaser.getArrivedParties()); |
64 |
+ |
assertEquals(0, phaser.getUnarrivedParties()); |
65 |
|
assertEquals(0, phaser.getPhase()); |
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* A negative party number for the constructor throws illegal argument |
70 |
< |
* exception |
69 |
> |
* Constructing with a negative number of parties throws |
70 |
> |
* IllegalArgumentException |
71 |
|
*/ |
72 |
< |
public void testConstructor2() { |
72 |
> |
public void testConstructorNegativeParties() { |
73 |
|
try { |
74 |
|
new Phaser(-1); |
75 |
|
shouldThrow(); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* The parent being input into the constructor should equal the original |
81 |
< |
* parent when being returned |
80 |
> |
* Constructing with a negative number of parties throws |
81 |
> |
* IllegalArgumentException |
82 |
|
*/ |
83 |
< |
public void testConstructor3() { |
84 |
< |
Phaser parent = new Phaser(); |
85 |
< |
assertEquals(parent, new Phaser(parent).getParent()); |
83 |
> |
public void testConstructorNegativeParties2() { |
84 |
> |
try { |
85 |
> |
new Phaser(new Phaser(), -1); |
86 |
> |
shouldThrow(); |
87 |
> |
} catch (IllegalArgumentException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* A negative party number for the constructor throws illegal argument |
92 |
< |
* exception |
91 |
> |
* Constructing with a number of parties > 65535 throws |
92 |
> |
* IllegalArgumentException |
93 |
|
*/ |
94 |
< |
public void testConstructor4() { |
94 |
> |
public void testConstructorPartiesExceedsLimit() { |
95 |
> |
new Phaser(maxParties); |
96 |
|
try { |
97 |
< |
new Phaser(new Phaser(), -1); |
97 |
> |
new Phaser(maxParties + 1); |
98 |
> |
shouldThrow(); |
99 |
> |
} catch (IllegalArgumentException success) {} |
100 |
> |
|
101 |
> |
new Phaser(new Phaser(), maxParties); |
102 |
> |
try { |
103 |
> |
new Phaser(new Phaser(), maxParties + 1); |
104 |
|
shouldThrow(); |
105 |
|
} catch (IllegalArgumentException success) {} |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
+ |
* The parent provided to the constructor should be returned from |
110 |
+ |
* a later call to getParent |
111 |
+ |
*/ |
112 |
+ |
public void testConstructor3() { |
113 |
+ |
Phaser parent = new Phaser(); |
114 |
+ |
assertSame(parent, new Phaser(parent).getParent()); |
115 |
+ |
assertNull(new Phaser(null).getParent()); |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
/** |
119 |
|
* The parent being input into the parameter should equal the original |
120 |
|
* parent when being returned |
121 |
|
*/ |
122 |
|
public void testConstructor5() { |
123 |
|
Phaser parent = new Phaser(); |
124 |
< |
assertEquals(parent, new Phaser(parent, 0).getParent()); |
124 |
> |
assertSame(parent, new Phaser(parent, 0).getParent()); |
125 |
> |
assertNull(new Phaser(null, 0).getParent()); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
< |
* register() will increment the number of unarrived parties by one and not |
130 |
< |
* affect its arrived parties |
129 |
> |
* register() will increment the number of unarrived parties by |
130 |
> |
* one and not affect its arrived parties |
131 |
|
*/ |
132 |
|
public void testRegister1() { |
133 |
|
Phaser phaser = new Phaser(); |
141 |
|
*/ |
142 |
|
public void testRegister2() { |
143 |
|
Phaser phaser = new Phaser(0); |
118 |
– |
int maxParties = (1 << 16) - 1; |
144 |
|
assertState(phaser, 0, 0, 0); |
145 |
|
assertEquals(0, phaser.bulkRegister(maxParties - 10)); |
146 |
|
assertState(phaser, 0, maxParties - 10, maxParties - 10); |
153 |
|
phaser.register(); |
154 |
|
shouldThrow(); |
155 |
|
} catch (IllegalStateException success) {} |
156 |
+ |
|
157 |
+ |
try { |
158 |
+ |
phaser.bulkRegister(Integer.MAX_VALUE); |
159 |
+ |
shouldThrow(); |
160 |
+ |
} catch (IllegalStateException success) {} |
161 |
+ |
|
162 |
+ |
assertEquals(0, phaser.bulkRegister(0)); |
163 |
+ |
assertState(phaser, 0, maxParties, maxParties); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
204 |
|
*/ |
205 |
|
public void testBulkRegister2() { |
206 |
|
Phaser phaser = new Phaser(); |
207 |
+ |
assertEquals(0, phaser.bulkRegister(0)); |
208 |
+ |
assertState(phaser, 0, 0, 0); |
209 |
|
assertEquals(0, phaser.bulkRegister(20)); |
210 |
|
assertState(phaser, 0, 20, 20); |
211 |
|
} |
264 |
|
assertState(phaser, 0, 1, 1); |
265 |
|
} |
266 |
|
assertEquals(0, phaser.arriveAndDeregister()); |
267 |
< |
assertTerminated(phaser); |
267 |
> |
assertTerminated(phaser, 1); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
295 |
|
public void testArrive3() { |
296 |
|
Phaser phaser = new Phaser(1); |
297 |
|
phaser.forceTermination(); |
298 |
< |
assertTerminated(phaser, 1, 1); |
298 |
> |
assertTerminated(phaser, 0, 1); |
299 |
> |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
300 |
|
assertTrue(phaser.arrive() < 0); |
301 |
|
assertTrue(phaser.register() < 0); |
302 |
|
assertTrue(phaser.arriveAndDeregister() < 0); |
337 |
|
Phaser parent = new Phaser(); |
338 |
|
Phaser child = new Phaser(parent); |
339 |
|
assertState(child, 0, 0, 0); |
340 |
< |
assertState(parent, 0, 1, 1); |
340 |
> |
assertState(parent, 0, 0, 0); |
341 |
|
assertEquals(0, child.register()); |
342 |
|
assertState(child, 0, 1, 1); |
343 |
|
assertState(parent, 0, 1, 1); |
344 |
|
assertEquals(0, child.arriveAndDeregister()); |
345 |
< |
assertTerminated(child); |
346 |
< |
assertTerminated(parent); |
345 |
> |
assertTerminated(child, 1); |
346 |
> |
assertTerminated(parent, 1); |
347 |
|
} |
348 |
|
|
349 |
|
/** |
370 |
|
Phaser root = new Phaser(); |
371 |
|
Phaser parent = new Phaser(root); |
372 |
|
Phaser child = new Phaser(parent); |
373 |
< |
assertState(root, 0, 1, 1); |
374 |
< |
assertState(parent, 0, 1, 1); |
373 |
> |
assertState(root, 0, 0, 0); |
374 |
> |
assertState(parent, 0, 0, 0); |
375 |
|
assertState(child, 0, 0, 0); |
376 |
|
assertEquals(0, child.register()); |
377 |
|
assertState(root, 0, 1, 1); |
378 |
|
assertState(parent, 0, 1, 1); |
379 |
|
assertState(child, 0, 1, 1); |
380 |
|
assertEquals(0, child.arriveAndDeregister()); |
381 |
< |
assertTerminated(child); |
382 |
< |
assertTerminated(parent); |
383 |
< |
assertTerminated(root); |
381 |
> |
assertTerminated(child, 1); |
382 |
> |
assertTerminated(parent, 1); |
383 |
> |
assertTerminated(root, 1); |
384 |
|
} |
385 |
|
|
386 |
|
/** |
398 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
399 |
|
assertState(phaser, 1, 1, 1); |
400 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
401 |
< |
assertTerminated(phaser); |
401 |
> |
assertTerminated(phaser, 2); |
402 |
|
awaitTermination(t, SHORT_DELAY_MS); |
403 |
|
} |
404 |
|
|
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
< |
* awaitAdvance continues waiting if interrupted |
425 |
> |
* awaitAdvance continues waiting if interrupted before waiting |
426 |
> |
*/ |
427 |
> |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
428 |
> |
final Phaser phaser = new Phaser(); |
429 |
> |
assertEquals(0, phaser.register()); |
430 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
431 |
> |
|
432 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
433 |
> |
public void realRun() throws InterruptedException { |
434 |
> |
Thread.currentThread().interrupt(); |
435 |
> |
assertEquals(0, phaser.register()); |
436 |
> |
assertEquals(0, phaser.arrive()); |
437 |
> |
threadStarted.countDown(); |
438 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
439 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
440 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
441 |
> |
}}); |
442 |
> |
|
443 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
444 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
445 |
> |
assertEquals(0, phaser.arrive()); |
446 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
447 |
> |
|
448 |
> |
Thread.currentThread().interrupt(); |
449 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
450 |
> |
assertTrue(Thread.interrupted()); |
451 |
> |
} |
452 |
> |
|
453 |
> |
/** |
454 |
> |
* awaitAdvance continues waiting if interrupted while waiting |
455 |
|
*/ |
456 |
< |
public void testAwaitAdvance3() throws InterruptedException { |
456 |
> |
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
457 |
|
final Phaser phaser = new Phaser(); |
458 |
|
assertEquals(0, phaser.register()); |
459 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
461 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
462 |
|
public void realRun() throws InterruptedException { |
463 |
|
assertEquals(0, phaser.register()); |
464 |
+ |
assertEquals(0, phaser.arrive()); |
465 |
|
threadStarted.countDown(); |
466 |
< |
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
466 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
467 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
468 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
469 |
|
}}); |
470 |
+ |
|
471 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
472 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
473 |
|
t.interrupt(); |
474 |
|
assertEquals(0, phaser.arrive()); |
475 |
|
awaitTermination(t, SMALL_DELAY_MS); |
476 |
+ |
|
477 |
+ |
Thread.currentThread().interrupt(); |
478 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
479 |
+ |
assertTrue(Thread.interrupted()); |
480 |
+ |
} |
481 |
+ |
|
482 |
+ |
/** |
483 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
484 |
+ |
*/ |
485 |
+ |
public void testArriveAndAwaitAdvanceAfterInterrupt() |
486 |
+ |
throws InterruptedException { |
487 |
+ |
final Phaser phaser = new Phaser(); |
488 |
+ |
assertEquals(0, phaser.register()); |
489 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
490 |
+ |
|
491 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
492 |
+ |
public void realRun() throws InterruptedException { |
493 |
+ |
Thread.currentThread().interrupt(); |
494 |
+ |
assertEquals(0, phaser.register()); |
495 |
+ |
threadStarted.countDown(); |
496 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
497 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
498 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
499 |
+ |
}}); |
500 |
+ |
|
501 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
502 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
503 |
+ |
Thread.currentThread().interrupt(); |
504 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
505 |
+ |
assertTrue(Thread.interrupted()); |
506 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
507 |
+ |
} |
508 |
+ |
|
509 |
+ |
/** |
510 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
511 |
+ |
*/ |
512 |
+ |
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
513 |
+ |
throws InterruptedException { |
514 |
+ |
final Phaser phaser = new Phaser(); |
515 |
+ |
assertEquals(0, phaser.register()); |
516 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
517 |
+ |
|
518 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
519 |
+ |
public void realRun() throws InterruptedException { |
520 |
+ |
assertEquals(0, phaser.register()); |
521 |
+ |
threadStarted.countDown(); |
522 |
+ |
assertFalse(Thread.currentThread().isInterrupted()); |
523 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
524 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
525 |
+ |
}}); |
526 |
+ |
|
527 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
528 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
529 |
+ |
t.interrupt(); |
530 |
+ |
Thread.currentThread().interrupt(); |
531 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
532 |
+ |
assertTrue(Thread.interrupted()); |
533 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
534 |
|
} |
535 |
|
|
536 |
|
/** |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
+ |
* awaitAdvance returns the current phase in child phasers |
592 |
+ |
*/ |
593 |
+ |
public void testAwaitAdvanceTieredPhaser() throws Exception { |
594 |
+ |
final Phaser parent = new Phaser(); |
595 |
+ |
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
596 |
+ |
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
597 |
+ |
for (int i = 0; i < 3; i++) { |
598 |
+ |
zeroPartyChildren.add(new Phaser(parent, 0)); |
599 |
+ |
onePartyChildren.add(new Phaser(parent, 1)); |
600 |
+ |
} |
601 |
+ |
final List<Phaser> phasers = new ArrayList<Phaser>(); |
602 |
+ |
phasers.addAll(zeroPartyChildren); |
603 |
+ |
phasers.addAll(onePartyChildren); |
604 |
+ |
phasers.add(parent); |
605 |
+ |
for (Phaser phaser : phasers) { |
606 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
607 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
608 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
609 |
+ |
} |
610 |
+ |
|
611 |
+ |
for (Phaser child : onePartyChildren) |
612 |
+ |
assertEquals(0, child.arrive()); |
613 |
+ |
for (Phaser phaser : phasers) { |
614 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
615 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
616 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
617 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
618 |
+ |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
619 |
+ |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
620 |
+ |
} |
621 |
+ |
|
622 |
+ |
for (Phaser child : onePartyChildren) |
623 |
+ |
assertEquals(1, child.arrive()); |
624 |
+ |
for (Phaser phaser : phasers) { |
625 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
626 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
627 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
628 |
+ |
assertEquals(2, phaser.awaitAdvance(0)); |
629 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
630 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
631 |
+ |
assertEquals(2, phaser.awaitAdvance(1)); |
632 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
633 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
634 |
+ |
} |
635 |
+ |
} |
636 |
+ |
|
637 |
+ |
/** |
638 |
|
* awaitAdvance returns when the phaser is externally terminated |
639 |
|
*/ |
640 |
|
public void testAwaitAdvance6() throws InterruptedException { |
649 |
|
assertTrue(phaser.awaitAdvance(0) < 0); |
650 |
|
assertTrue(phaser.isTerminated()); |
651 |
|
assertTrue(phaser.getPhase() < 0); |
652 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
653 |
|
assertEquals(3, phaser.getRegisteredParties()); |
654 |
|
}}; |
655 |
|
threads.add(newStartedThread(r)); |
656 |
|
} |
657 |
|
threadsStarted.await(); |
658 |
|
phaser.forceTermination(); |
659 |
+ |
assertTrue(phaser.isTerminated()); |
660 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
661 |
|
for (Thread thread : threads) |
662 |
|
awaitTermination(thread, SMALL_DELAY_MS); |
486 |
– |
assertTrue(phaser.isTerminated()); |
487 |
– |
assertTrue(phaser.getPhase() < 0); |
663 |
|
assertEquals(3, phaser.getRegisteredParties()); |
664 |
|
} |
665 |
|
|
676 |
|
} |
677 |
|
|
678 |
|
/** |
504 |
– |
* Interrupted arriveAndAwaitAdvance does not throw InterruptedException |
505 |
– |
*/ |
506 |
– |
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
507 |
– |
final Phaser phaser = new Phaser(2); |
508 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
509 |
– |
final AtomicBoolean advanced = new AtomicBoolean(false); |
510 |
– |
final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false); |
511 |
– |
Thread t = newStartedThread(new CheckedRunnable() { |
512 |
– |
public void realRun() throws InterruptedException { |
513 |
– |
threadStarted.countDown(); |
514 |
– |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
515 |
– |
assertState(phaser, 1, 2, 2); |
516 |
– |
advanced.set(true); |
517 |
– |
assertTrue(Thread.currentThread().isInterrupted()); |
518 |
– |
while (!checkedInterruptStatus.get()) |
519 |
– |
Thread.yield(); |
520 |
– |
}}); |
521 |
– |
|
522 |
– |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
523 |
– |
t.interrupt(); |
524 |
– |
assertEquals(0, phaser.arrive()); |
525 |
– |
while (!advanced.get()) |
526 |
– |
Thread.yield(); |
527 |
– |
assertTrue(t.isInterrupted()); |
528 |
– |
checkedInterruptStatus.set(true); |
529 |
– |
awaitTermination(t, SMALL_DELAY_MS); |
530 |
– |
} |
531 |
– |
|
532 |
– |
/** |
679 |
|
* arriveAndAwaitAdvance waits for all threads to arrive, the |
680 |
|
* number of arrived parties is the same number that is accounted |
681 |
|
* for when the main thread awaitsAdvance |