ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.27
Committed: Fri Dec 3 02:09:40 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +1 -2 lines
Log Message:
testAwaitAdvance6 small improvement

File Contents

# User Rev Content
1 dl 1.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
5     * Other contributors include John Vint
6     */
7    
8     import java.util.ArrayList;
9     import java.util.List;
10     import java.util.concurrent.atomic.AtomicInteger;
11 jsr166 1.16 import java.util.concurrent.atomic.AtomicBoolean;
12 dl 1.1 import java.util.concurrent.*;
13 jsr166 1.16 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 jsr166 1.19 import static java.util.concurrent.TimeUnit.NANOSECONDS;
15 dl 1.1 import junit.framework.Test;
16     import junit.framework.TestSuite;
17    
18     public class PhaserTest extends JSR166TestCase {
19    
20     public static void main(String[] args) {
21     junit.textui.TestRunner.run(suite());
22     }
23    
24     public static Test suite() {
25     return new TestSuite(PhaserTest.class);
26     }
27    
28 jsr166 1.21 private static final int maxParties = 65535;
29 jsr166 1.22
30 jsr166 1.19 /** Checks state of 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());
38     }
39    
40     /** Checks state of terminated phaser. */
41 jsr166 1.25 protected void assertTerminated(Phaser phaser,
42     int maxPhase, int parties, int unarrived) {
43 jsr166 1.19 assertTrue(phaser.isTerminated());
44 jsr166 1.25 int expectedPhase = maxPhase + Integer.MIN_VALUE;
45 dl 1.26 assertEquals(expectedPhase, phaser.getPhase());
46     assertEquals(parties, phaser.getRegisteredParties());
47 jsr166 1.25 assertEquals(expectedPhase, phaser.register());
48     assertEquals(expectedPhase, phaser.arrive());
49     assertEquals(expectedPhase, phaser.arriveAndDeregister());
50 jsr166 1.19 }
51    
52 jsr166 1.25 protected void assertTerminated(Phaser phaser, int maxPhase) {
53     assertTerminated(phaser, maxPhase, 0, 0);
54 jsr166 1.19 }
55    
56 dl 1.1 /**
57     * Empty constructor builds a new Phaser with no parent, no registered
58     * parties and initial phase number of 0
59     */
60 jsr166 1.21 public void testConstructorDefaultValues() {
61 dl 1.1 Phaser phaser = new Phaser();
62     assertNull(phaser.getParent());
63 jsr166 1.21 assertEquals(0, phaser.getRegisteredParties());
64 dl 1.1 assertEquals(0, phaser.getArrivedParties());
65 jsr166 1.21 assertEquals(0, phaser.getUnarrivedParties());
66 dl 1.1 assertEquals(0, phaser.getPhase());
67     }
68    
69     /**
70 jsr166 1.21 * Constructing with a negative number of parties throws
71     * IllegalArgumentException
72 dl 1.1 */
73 jsr166 1.21 public void testConstructorNegativeParties() {
74 dl 1.1 try {
75     new Phaser(-1);
76 jsr166 1.6 shouldThrow();
77 jsr166 1.8 } catch (IllegalArgumentException success) {}
78 dl 1.1 }
79    
80     /**
81 jsr166 1.21 * Constructing with a negative number of parties throws
82     * IllegalArgumentException
83 dl 1.1 */
84 jsr166 1.21 public void testConstructorNegativeParties2() {
85     try {
86     new Phaser(new Phaser(), -1);
87     shouldThrow();
88     } catch (IllegalArgumentException success) {}
89 dl 1.1 }
90    
91     /**
92 jsr166 1.21 * Constructing with a number of parties > 65535 throws
93     * IllegalArgumentException
94 dl 1.1 */
95 jsr166 1.21 public void testConstructorPartiesExceedsLimit() {
96     new Phaser(maxParties);
97 dl 1.1 try {
98 jsr166 1.21 new Phaser(maxParties + 1);
99     shouldThrow();
100     } catch (IllegalArgumentException success) {}
101    
102     new Phaser(new Phaser(), maxParties);
103     try {
104     new Phaser(new Phaser(), maxParties + 1);
105 dl 1.1 shouldThrow();
106 jsr166 1.8 } catch (IllegalArgumentException success) {}
107 dl 1.1 }
108    
109     /**
110 jsr166 1.21 * The parent provided to the constructor should be returned from
111     * a later call to getParent
112     */
113     public void testConstructor3() {
114     Phaser parent = new Phaser();
115     assertSame(parent, new Phaser(parent).getParent());
116     assertNull(new Phaser(null).getParent());
117     }
118    
119     /**
120 dl 1.1 * The parent being input into the parameter should equal the original
121     * parent when being returned
122     */
123     public void testConstructor5() {
124     Phaser parent = new Phaser();
125 jsr166 1.21 assertSame(parent, new Phaser(parent, 0).getParent());
126     assertNull(new Phaser(null, 0).getParent());
127 dl 1.1 }
128    
129     /**
130 jsr166 1.21 * register() will increment the number of unarrived parties by
131     * one and not affect its arrived parties
132 dl 1.1 */
133     public void testRegister1() {
134     Phaser phaser = new Phaser();
135 jsr166 1.19 assertState(phaser, 0, 0, 0);
136     assertEquals(0, phaser.register());
137     assertState(phaser, 0, 1, 1);
138 dl 1.1 }
139    
140     /**
141 jsr166 1.4 * Registering more than 65536 parties causes IllegalStateException
142 dl 1.1 */
143     public void testRegister2() {
144     Phaser phaser = new Phaser(0);
145 jsr166 1.19 assertState(phaser, 0, 0, 0);
146     assertEquals(0, phaser.bulkRegister(maxParties - 10));
147     assertState(phaser, 0, maxParties - 10, maxParties - 10);
148     for (int i = 0; i < 10; i++) {
149     assertState(phaser, 0, maxParties - 10 + i, maxParties - 10 + i);
150     assertEquals(0, phaser.register());
151 dl 1.1 }
152 jsr166 1.19 assertState(phaser, 0, maxParties, maxParties);
153 dl 1.1 try {
154     phaser.register();
155     shouldThrow();
156 jsr166 1.8 } catch (IllegalStateException success) {}
157 jsr166 1.21
158     try {
159     phaser.bulkRegister(Integer.MAX_VALUE);
160     shouldThrow();
161     } catch (IllegalStateException success) {}
162 dl 1.1 }
163    
164     /**
165     * register() correctly returns the current barrier phase number when
166     * invoked
167     */
168     public void testRegister3() {
169     Phaser phaser = new Phaser();
170     assertEquals(0, phaser.register());
171 jsr166 1.19 assertEquals(0, phaser.arrive());
172 dl 1.1 assertEquals(1, phaser.register());
173 jsr166 1.19 assertState(phaser, 1, 2, 2);
174 dl 1.1 }
175    
176     /**
177     * register causes the next arrive to not increment the phase rather retain
178     * the phase number
179     */
180     public void testRegister4() {
181     Phaser phaser = new Phaser(1);
182 jsr166 1.19 assertEquals(0, phaser.arrive());
183     assertEquals(1, phaser.register());
184     assertEquals(1, phaser.arrive());
185     assertState(phaser, 1, 2, 1);
186 dl 1.1 }
187    
188     /**
189     * Invoking bulkRegister with a negative parameter throws an
190 jsr166 1.4 * IllegalArgumentException
191 dl 1.1 */
192     public void testBulkRegister1() {
193     try {
194     new Phaser().bulkRegister(-1);
195     shouldThrow();
196 jsr166 1.8 } catch (IllegalArgumentException success) {}
197 dl 1.1 }
198    
199     /**
200     * bulkRegister should correctly record the number of unarrived parties with
201     * the number of parties being registered
202     */
203     public void testBulkRegister2() {
204     Phaser phaser = new Phaser();
205 jsr166 1.19 assertEquals(0, phaser.bulkRegister(20));
206     assertState(phaser, 0, 20, 20);
207 dl 1.1 }
208    
209     /**
210 jsr166 1.4 * Registering with a number of parties greater than or equal to 1<<16
211     * throws IllegalStateException.
212 dl 1.1 */
213     public void testBulkRegister3() {
214 jsr166 1.19 assertEquals(0, new Phaser().bulkRegister((1 << 16) - 1));
215    
216 dl 1.1 try {
217     new Phaser().bulkRegister(1 << 16);
218     shouldThrow();
219 jsr166 1.8 } catch (IllegalStateException success) {}
220 jsr166 1.19
221     try {
222     new Phaser(2).bulkRegister((1 << 16) - 2);
223     shouldThrow();
224     } catch (IllegalStateException success) {}
225 dl 1.1 }
226    
227     /**
228     * the phase number increments correctly when tripping the barrier
229     */
230     public void testPhaseIncrement1() {
231     for (int size = 1; size < nine; size++) {
232     final Phaser phaser = new Phaser(size);
233     for (int index = 0; index <= (1 << size); index++) {
234     int phase = phaser.arrive();
235     assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0);
236     }
237     }
238     }
239    
240     /**
241 jsr166 1.19 * arrive() on a registered phaser increments phase.
242 dl 1.1 */
243     public void testArrive1() {
244     Phaser phaser = new Phaser(1);
245 jsr166 1.19 assertState(phaser, 0, 1, 1);
246     assertEquals(0, phaser.arrive());
247     assertState(phaser, 1, 1, 1);
248     }
249    
250     /**
251     * arriveAndDeregister does not wait for others to arrive at barrier
252     */
253     public void testArriveAndDeregister() throws InterruptedException {
254     final Phaser phaser = new Phaser(1);
255     for (int i = 0; i < 10; i++) {
256     assertState(phaser, 0, 1, 1);
257     assertEquals(0, phaser.register());
258     assertState(phaser, 0, 2, 2);
259     assertEquals(0, phaser.arriveAndDeregister());
260     assertState(phaser, 0, 1, 1);
261     }
262     assertEquals(0, phaser.arriveAndDeregister());
263 jsr166 1.25 assertTerminated(phaser, 1);
264 dl 1.1 }
265    
266     /**
267 jsr166 1.7 * arriveAndDeregister does not wait for others to arrive at barrier
268 dl 1.1 */
269 jsr166 1.7 public void testArrive2() throws InterruptedException {
270 jsr166 1.19 final Phaser phaser = new Phaser();
271     assertEquals(0, phaser.register());
272 jsr166 1.7 List<Thread> threads = new ArrayList<Thread>();
273 jsr166 1.19 for (int i = 0; i < 10; i++) {
274     assertEquals(0, phaser.register());
275 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
276 jsr166 1.9 public void realRun() throws InterruptedException {
277 jsr166 1.19 assertEquals(0, phaser.arriveAndDeregister());
278 jsr166 1.7 }}));
279 jsr166 1.19 }
280 dl 1.1
281 jsr166 1.7 for (Thread thread : threads)
282 jsr166 1.19 awaitTermination(thread, LONG_DELAY_MS);
283     assertState(phaser, 0, 1, 1);
284     assertEquals(0, phaser.arrive());
285     assertState(phaser, 1, 1, 1);
286 dl 1.1 }
287    
288     /**
289 jsr166 1.3 * arrive() returns a negative number if the Phaser is terminated
290 dl 1.1 */
291     public void testArrive3() {
292     Phaser phaser = new Phaser(1);
293     phaser.forceTermination();
294 jsr166 1.25 assertTerminated(phaser, 0, 1, 1);
295 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
296 dl 1.1 assertTrue(phaser.arrive() < 0);
297 jsr166 1.19 assertTrue(phaser.register() < 0);
298     assertTrue(phaser.arriveAndDeregister() < 0);
299     assertTrue(phaser.awaitAdvance(1) < 0);
300     assertTrue(phaser.getPhase() < 0);
301 dl 1.1 }
302    
303     /**
304     * arriveAndDeregister() throws IllegalStateException if number of
305 jsr166 1.3 * registered or unarrived parties would become negative
306 dl 1.1 */
307     public void testArriveAndDeregister1() {
308     try {
309     Phaser phaser = new Phaser();
310     phaser.arriveAndDeregister();
311     shouldThrow();
312 jsr166 1.8 } catch (IllegalStateException success) {}
313 dl 1.1 }
314    
315     /**
316 jsr166 1.19 * arriveAndDeregister reduces the number of arrived parties
317 dl 1.1 */
318 jsr166 1.19 public void testArriveAndDeregister2() {
319 dl 1.1 final Phaser phaser = new Phaser(1);
320 jsr166 1.19 assertEquals(0, phaser.register());
321     assertEquals(0, phaser.arrive());
322     assertState(phaser, 0, 2, 1);
323     assertEquals(0, phaser.arriveAndDeregister());
324     assertState(phaser, 1, 1, 1);
325 dl 1.1 }
326    
327     /**
328 jsr166 1.19 * arriveAndDeregister arrives at the barrier on a phaser with a parent and
329 dl 1.1 * when a deregistration occurs and causes the phaser to have zero parties
330     * its parent will be deregistered as well
331     */
332 jsr166 1.19 public void testArriveAndDeregister3() {
333 dl 1.1 Phaser parent = new Phaser();
334 jsr166 1.19 Phaser child = new Phaser(parent);
335     assertState(child, 0, 0, 0);
336 dl 1.23 assertState(parent, 0, 0, 0);
337 jsr166 1.19 assertEquals(0, child.register());
338     assertState(child, 0, 1, 1);
339     assertState(parent, 0, 1, 1);
340     assertEquals(0, child.arriveAndDeregister());
341 jsr166 1.25 assertTerminated(child, 1);
342     assertTerminated(parent, 1);
343 dl 1.1 }
344    
345     /**
346     * arriveAndDeregister deregisters one party from its parent when
347 jsr166 1.19 * the number of parties of child is zero after deregistration
348 dl 1.1 */
349 jsr166 1.19 public void testArriveAndDeregister4() {
350 dl 1.1 Phaser parent = new Phaser();
351 jsr166 1.19 Phaser child = new Phaser(parent);
352     assertEquals(0, parent.register());
353     assertEquals(0, child.register());
354     assertState(child, 0, 1, 1);
355     assertState(parent, 0, 2, 2);
356     assertEquals(0, child.arriveAndDeregister());
357     assertState(child, 0, 0, 0);
358     assertState(parent, 0, 1, 1);
359 dl 1.1 }
360    
361     /**
362     * arriveAndDeregister deregisters one party from its parent when
363     * the number of parties of root is nonzero after deregistration.
364     */
365     public void testArriveAndDeregister5() {
366 jsr166 1.19 Phaser root = new Phaser();
367     Phaser parent = new Phaser(root);
368 dl 1.1 Phaser child = new Phaser(parent);
369 dl 1.23 assertState(root, 0, 0, 0);
370     assertState(parent, 0, 0, 0);
371 jsr166 1.19 assertState(child, 0, 0, 0);
372     assertEquals(0, child.register());
373     assertState(root, 0, 1, 1);
374     assertState(parent, 0, 1, 1);
375     assertState(child, 0, 1, 1);
376     assertEquals(0, child.arriveAndDeregister());
377 jsr166 1.25 assertTerminated(child, 1);
378     assertTerminated(parent, 1);
379     assertTerminated(root, 1);
380 dl 1.1 }
381    
382     /**
383     * arriveAndDeregister returns the phase in which it leaves the
384     * phaser in after deregistration
385     */
386 jsr166 1.7 public void testArriveAndDeregister6() throws InterruptedException {
387 dl 1.1 final Phaser phaser = new Phaser(2);
388 jsr166 1.7 Thread t = newStartedThread(new CheckedRunnable() {
389 jsr166 1.9 public void realRun() {
390 jsr166 1.19 assertEquals(0, phaser.arrive());
391 jsr166 1.7 }});
392 jsr166 1.19 assertEquals(1, phaser.arriveAndAwaitAdvance());
393     assertState(phaser, 1, 2, 2);
394     assertEquals(1, phaser.arriveAndDeregister());
395     assertState(phaser, 1, 1, 1);
396     assertEquals(1, phaser.arriveAndDeregister());
397 jsr166 1.25 assertTerminated(phaser, 2);
398 jsr166 1.19 awaitTermination(t, SHORT_DELAY_MS);
399 dl 1.1 }
400    
401     /**
402     * awaitAdvance succeeds upon advance
403     */
404     public void testAwaitAdvance1() {
405     final Phaser phaser = new Phaser(1);
406 jsr166 1.19 assertEquals(0, phaser.arrive());
407     assertEquals(1, phaser.awaitAdvance(0));
408 dl 1.1 }
409    
410     /**
411     * awaitAdvance with a negative parameter will return without affecting the
412     * phaser
413     */
414     public void testAwaitAdvance2() {
415 jsr166 1.6 Phaser phaser = new Phaser();
416 jsr166 1.19 assertTrue(phaser.awaitAdvance(-1) < 0);
417     assertState(phaser, 0, 0, 0);
418 dl 1.1 }
419    
420     /**
421 jsr166 1.20 * awaitAdvance continues waiting if interrupted before waiting
422 dl 1.1 */
423 jsr166 1.20 public void testAwaitAdvanceAfterInterrupt() throws InterruptedException {
424 dl 1.1 final Phaser phaser = new Phaser();
425 jsr166 1.19 assertEquals(0, phaser.register());
426 jsr166 1.17 final CountDownLatch threadStarted = new CountDownLatch(1);
427 dl 1.1
428 jsr166 1.7 Thread t = newStartedThread(new CheckedRunnable() {
429 jsr166 1.9 public void realRun() throws InterruptedException {
430 jsr166 1.20 Thread.currentThread().interrupt();
431 jsr166 1.19 assertEquals(0, phaser.register());
432 jsr166 1.20 assertEquals(0, phaser.arrive());
433 jsr166 1.17 threadStarted.countDown();
434 jsr166 1.20 assertTrue(Thread.currentThread().isInterrupted());
435     assertEquals(1, phaser.awaitAdvance(0));
436 jsr166 1.17 assertTrue(Thread.currentThread().isInterrupted());
437 jsr166 1.5 }});
438 jsr166 1.20
439 jsr166 1.17 assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
440 jsr166 1.20 waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
441     assertEquals(0, phaser.arrive());
442     awaitTermination(t, SMALL_DELAY_MS);
443    
444     Thread.currentThread().interrupt();
445     assertEquals(1, phaser.awaitAdvance(0));
446     assertTrue(Thread.interrupted());
447     }
448    
449     /**
450     * awaitAdvance continues waiting if interrupted while waiting
451     */
452     public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException {
453     final Phaser phaser = new Phaser();
454     assertEquals(0, phaser.register());
455     final CountDownLatch threadStarted = new CountDownLatch(1);
456    
457     Thread t = newStartedThread(new CheckedRunnable() {
458     public void realRun() throws InterruptedException {
459     assertEquals(0, phaser.register());
460     assertEquals(0, phaser.arrive());
461     threadStarted.countDown();
462     assertFalse(Thread.currentThread().isInterrupted());
463     assertEquals(1, phaser.awaitAdvance(0));
464     assertTrue(Thread.currentThread().isInterrupted());
465     }});
466    
467     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
468     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
469 jsr166 1.7 t.interrupt();
470 jsr166 1.19 assertEquals(0, phaser.arrive());
471 jsr166 1.17 awaitTermination(t, SMALL_DELAY_MS);
472 jsr166 1.20
473     Thread.currentThread().interrupt();
474     assertEquals(1, phaser.awaitAdvance(0));
475     assertTrue(Thread.interrupted());
476     }
477    
478     /**
479     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
480     */
481     public void testArriveAndAwaitAdvanceAfterInterrupt()
482     throws InterruptedException {
483     final Phaser phaser = new Phaser();
484     assertEquals(0, phaser.register());
485     final CountDownLatch threadStarted = new CountDownLatch(1);
486    
487     Thread t = newStartedThread(new CheckedRunnable() {
488     public void realRun() throws InterruptedException {
489     Thread.currentThread().interrupt();
490     assertEquals(0, phaser.register());
491     threadStarted.countDown();
492     assertTrue(Thread.currentThread().isInterrupted());
493     assertEquals(1, phaser.arriveAndAwaitAdvance());
494     assertTrue(Thread.currentThread().isInterrupted());
495     }});
496    
497     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
498     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
499     Thread.currentThread().interrupt();
500     assertEquals(1, phaser.arriveAndAwaitAdvance());
501     assertTrue(Thread.interrupted());
502     awaitTermination(t, SMALL_DELAY_MS);
503     }
504    
505     /**
506     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
507     */
508     public void testArriveAndAwaitAdvanceBeforeInterrupt()
509     throws InterruptedException {
510     final Phaser phaser = new Phaser();
511     assertEquals(0, phaser.register());
512     final CountDownLatch threadStarted = new CountDownLatch(1);
513    
514     Thread t = newStartedThread(new CheckedRunnable() {
515     public void realRun() throws InterruptedException {
516     assertEquals(0, phaser.register());
517     threadStarted.countDown();
518     assertFalse(Thread.currentThread().isInterrupted());
519     assertEquals(1, phaser.arriveAndAwaitAdvance());
520     assertTrue(Thread.currentThread().isInterrupted());
521     }});
522    
523     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
524     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
525     t.interrupt();
526     Thread.currentThread().interrupt();
527     assertEquals(1, phaser.arriveAndAwaitAdvance());
528     assertTrue(Thread.interrupted());
529     awaitTermination(t, SMALL_DELAY_MS);
530 dl 1.1 }
531    
532     /**
533     * awaitAdvance atomically waits for all parties within the same phase to
534     * complete before continuing
535     */
536 jsr166 1.5 public void testAwaitAdvance4() throws InterruptedException {
537     final Phaser phaser = new Phaser(4);
538 jsr166 1.19 final AtomicInteger count = new AtomicInteger(0);
539 jsr166 1.5 List<Thread> threads = new ArrayList<Thread>();
540 jsr166 1.19 for (int i = 0; i < 4; i++)
541 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
542 jsr166 1.9 public void realRun() {
543 jsr166 1.19 for (int k = 0; k < 3; k++) {
544     assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
545     count.incrementAndGet();
546     assertEquals(2*k+1, phaser.arrive());
547     assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
548     assertEquals(count.get(), 4*(k+1));
549     }}}));
550    
551 jsr166 1.5 for (Thread thread : threads)
552 jsr166 1.19 awaitTermination(thread, MEDIUM_DELAY_MS);
553 dl 1.1 }
554    
555     /**
556     * awaitAdvance returns the current phase
557     */
558 jsr166 1.7 public void testAwaitAdvance5() throws InterruptedException {
559 dl 1.1 final Phaser phaser = new Phaser(1);
560 jsr166 1.19 assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
561     assertEquals(1, phaser.getPhase());
562     assertEquals(1, phaser.register());
563 jsr166 1.7 List<Thread> threads = new ArrayList<Thread>();
564     for (int i = 0; i < 8; i++) {
565 jsr166 1.18 final CountDownLatch latch = new CountDownLatch(1);
566     final boolean goesFirst = ((i & 1) == 0);
567 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
568 jsr166 1.18 public void realRun() throws InterruptedException {
569     if (goesFirst)
570     latch.countDown();
571     else
572     assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
573 dl 1.1 phaser.arrive();
574 jsr166 1.7 }}));
575 jsr166 1.18 if (goesFirst)
576     assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
577     else
578     latch.countDown();
579 jsr166 1.19 assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
580     assertEquals(i + 2, phaser.getPhase());
581 dl 1.1 }
582 jsr166 1.7 for (Thread thread : threads)
583 jsr166 1.18 awaitTermination(thread, SMALL_DELAY_MS);
584 dl 1.1 }
585    
586     /**
587     * awaitAdvance returns when the phaser is externally terminated
588     */
589 jsr166 1.7 public void testAwaitAdvance6() throws InterruptedException {
590 dl 1.1 final Phaser phaser = new Phaser(3);
591 jsr166 1.18 final CountDownLatch threadsStarted = new CountDownLatch(2);
592     final List<Thread> threads = new ArrayList<Thread>();
593     for (int i = 0; i < 2; i++) {
594     Runnable r = new CheckedRunnable() {
595     public void realRun() {
596 jsr166 1.19 assertEquals(0, phaser.arrive());
597 jsr166 1.18 threadsStarted.countDown();
598 jsr166 1.19 assertTrue(phaser.awaitAdvance(0) < 0);
599 jsr166 1.18 assertTrue(phaser.isTerminated());
600 jsr166 1.19 assertTrue(phaser.getPhase() < 0);
601 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
602 jsr166 1.19 assertEquals(3, phaser.getRegisteredParties());
603 jsr166 1.18 }};
604     threads.add(newStartedThread(r));
605     }
606     threadsStarted.await();
607 dl 1.1 phaser.forceTermination();
608 jsr166 1.27 assertTrue(phaser.isTerminated());
609 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
610 jsr166 1.18 for (Thread thread : threads)
611     awaitTermination(thread, SMALL_DELAY_MS);
612 jsr166 1.19 assertEquals(3, phaser.getRegisteredParties());
613 dl 1.1 }
614    
615     /**
616     * arriveAndAwaitAdvance throws IllegalStateException with no
617     * unarrived parties
618     */
619     public void testArriveAndAwaitAdvance1() {
620     try {
621     Phaser phaser = new Phaser();
622     phaser.arriveAndAwaitAdvance();
623     shouldThrow();
624 jsr166 1.8 } catch (IllegalStateException success) {}
625 dl 1.1 }
626    
627     /**
628     * arriveAndAwaitAdvance waits for all threads to arrive, the
629     * number of arrived parties is the same number that is accounted
630     * for when the main thread awaitsAdvance
631     */
632 jsr166 1.7 public void testArriveAndAwaitAdvance3() throws InterruptedException {
633 dl 1.1 final Phaser phaser = new Phaser(1);
634 jsr166 1.19 final int THREADS = 3;
635     final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
636 jsr166 1.7 final List<Thread> threads = new ArrayList<Thread>();
637 jsr166 1.19 for (int i = 0; i < THREADS; i++)
638 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
639 jsr166 1.19 public void realRun() throws InterruptedException {
640     assertEquals(0, phaser.register());
641     threadsStarted.countDown();
642     assertEquals(1, phaser.arriveAndAwaitAdvance());
643     }}));
644    
645     assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
646     long t0 = System.nanoTime();
647     while (phaser.getArrivedParties() < THREADS)
648     Thread.yield();
649     assertEquals(THREADS, phaser.getArrivedParties());
650     assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS);
651     for (Thread thread : threads)
652     assertTrue(thread.isAlive());
653     assertState(phaser, 0, THREADS + 1, 1);
654 dl 1.12 phaser.arriveAndAwaitAdvance();
655 jsr166 1.7 for (Thread thread : threads)
656 jsr166 1.19 awaitTermination(thread, SMALL_DELAY_MS);
657     assertState(phaser, 1, THREADS + 1, THREADS + 1);
658 dl 1.1 }
659    
660     }