ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
Revision: 1.32
Committed: Tue Oct 13 21:07:54 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +8 -6 lines
Log Message:
testAwaitQuiescence*: fix 1/billion failure; getActiveThreadCount will eventually become 0 after isQuiescent reports true, but not necessarily immediately

File Contents

# User Rev Content
1 jsr166 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/publicdomain/zero/1.0/
5     */
6    
7 jsr166 1.29 import static java.util.concurrent.TimeUnit.MILLISECONDS;
8     import static java.util.concurrent.TimeUnit.SECONDS;
9    
10     import java.util.HashSet;
11 dl 1.4 import java.util.concurrent.CancellationException;
12 jsr166 1.29 import java.util.concurrent.CountedCompleter;
13 dl 1.4 import java.util.concurrent.ExecutionException;
14 jsr166 1.1 import java.util.concurrent.ForkJoinPool;
15     import java.util.concurrent.ForkJoinTask;
16 dl 1.4 import java.util.concurrent.RecursiveAction;
17     import java.util.concurrent.TimeoutException;
18 jsr166 1.29
19     import junit.framework.Test;
20     import junit.framework.TestSuite;
21 jsr166 1.1
22     public class ForkJoinPool8Test extends JSR166TestCase {
23     public static void main(String[] args) {
24 jsr166 1.30 main(suite(), args);
25 jsr166 1.1 }
26    
27     public static Test suite() {
28     return new TestSuite(ForkJoinPool8Test.class);
29     }
30    
31     /**
32     * Common pool exists and has expected parallelism.
33     */
34     public void testCommonPoolParallelism() {
35     assertEquals(ForkJoinPool.getCommonPoolParallelism(),
36     ForkJoinPool.commonPool().getParallelism());
37     }
38 dl 1.4
39     /**
40     * Common pool cannot be shut down
41     */
42     public void testCommonPoolShutDown() {
43     assertFalse(ForkJoinPool.commonPool().isShutdown());
44     assertFalse(ForkJoinPool.commonPool().isTerminating());
45     assertFalse(ForkJoinPool.commonPool().isTerminated());
46     ForkJoinPool.commonPool().shutdown();
47     assertFalse(ForkJoinPool.commonPool().isShutdown());
48     assertFalse(ForkJoinPool.commonPool().isTerminating());
49     assertFalse(ForkJoinPool.commonPool().isTerminated());
50     ForkJoinPool.commonPool().shutdownNow();
51     assertFalse(ForkJoinPool.commonPool().isShutdown());
52     assertFalse(ForkJoinPool.commonPool().isTerminating());
53     assertFalse(ForkJoinPool.commonPool().isTerminated());
54     }
55    
56     /*
57     * All of the following test methods are adaptations of those for
58     * RecursiveAction and CountedCompleter, but with all actions
59     * executed in the common pool, generally implicitly via
60     * checkInvoke.
61 jsr166 1.5 */
62 dl 1.4
63     private void checkInvoke(ForkJoinTask a) {
64     checkNotDone(a);
65     assertNull(a.invoke());
66     checkCompletedNormally(a);
67     }
68    
69     void checkNotDone(ForkJoinTask a) {
70     assertFalse(a.isDone());
71     assertFalse(a.isCompletedNormally());
72     assertFalse(a.isCompletedAbnormally());
73     assertFalse(a.isCancelled());
74     assertNull(a.getException());
75     assertNull(a.getRawResult());
76    
77     if (! ForkJoinTask.inForkJoinPool()) {
78     Thread.currentThread().interrupt();
79     try {
80     a.get();
81     shouldThrow();
82     } catch (InterruptedException success) {
83     } catch (Throwable fail) { threadUnexpectedException(fail); }
84    
85     Thread.currentThread().interrupt();
86     try {
87     a.get(5L, SECONDS);
88     shouldThrow();
89     } catch (InterruptedException success) {
90     } catch (Throwable fail) { threadUnexpectedException(fail); }
91     }
92    
93     try {
94     a.get(0L, SECONDS);
95     shouldThrow();
96     } catch (TimeoutException success) {
97     } catch (Throwable fail) { threadUnexpectedException(fail); }
98     }
99    
100     void checkCompletedNormally(ForkJoinTask a) {
101     assertTrue(a.isDone());
102     assertFalse(a.isCancelled());
103     assertTrue(a.isCompletedNormally());
104     assertFalse(a.isCompletedAbnormally());
105     assertNull(a.getException());
106     assertNull(a.getRawResult());
107     assertNull(a.join());
108     assertFalse(a.cancel(false));
109     assertFalse(a.cancel(true));
110     try {
111     assertNull(a.get());
112     } catch (Throwable fail) { threadUnexpectedException(fail); }
113     try {
114     assertNull(a.get(5L, SECONDS));
115     } catch (Throwable fail) { threadUnexpectedException(fail); }
116     }
117    
118     void checkCancelled(ForkJoinTask a) {
119     assertTrue(a.isDone());
120     assertTrue(a.isCancelled());
121     assertFalse(a.isCompletedNormally());
122     assertTrue(a.isCompletedAbnormally());
123     assertTrue(a.getException() instanceof CancellationException);
124     assertNull(a.getRawResult());
125    
126     try {
127     a.join();
128     shouldThrow();
129     } catch (CancellationException success) {
130     } catch (Throwable fail) { threadUnexpectedException(fail); }
131    
132     try {
133     a.get();
134     shouldThrow();
135     } catch (CancellationException success) {
136     } catch (Throwable fail) { threadUnexpectedException(fail); }
137    
138     try {
139     a.get(5L, SECONDS);
140     shouldThrow();
141     } catch (CancellationException success) {
142     } catch (Throwable fail) { threadUnexpectedException(fail); }
143     }
144    
145     void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
146     assertTrue(a.isDone());
147     assertFalse(a.isCancelled());
148     assertFalse(a.isCompletedNormally());
149     assertTrue(a.isCompletedAbnormally());
150     assertSame(t.getClass(), a.getException().getClass());
151     assertNull(a.getRawResult());
152     assertFalse(a.cancel(false));
153     assertFalse(a.cancel(true));
154    
155     try {
156     a.join();
157     shouldThrow();
158     } catch (Throwable expected) {
159     assertSame(expected.getClass(), t.getClass());
160     }
161    
162     try {
163     a.get();
164     shouldThrow();
165     } catch (ExecutionException success) {
166     assertSame(t.getClass(), success.getCause().getClass());
167     } catch (Throwable fail) { threadUnexpectedException(fail); }
168    
169     try {
170     a.get(5L, SECONDS);
171     shouldThrow();
172     } catch (ExecutionException success) {
173     assertSame(t.getClass(), success.getCause().getClass());
174     } catch (Throwable fail) { threadUnexpectedException(fail); }
175     }
176    
177     public static final class FJException extends RuntimeException {
178     public FJException() { super(); }
179     public FJException(Throwable cause) { super(cause); }
180     }
181    
182     // A simple recursive action for testing
183     final class FibAction extends CheckedRecursiveAction {
184     final int number;
185     int result;
186     FibAction(int n) { number = n; }
187 jsr166 1.9 protected void realCompute() {
188 dl 1.4 int n = number;
189     if (n <= 1)
190     result = n;
191     else {
192     FibAction f1 = new FibAction(n - 1);
193     FibAction f2 = new FibAction(n - 2);
194     invokeAll(f1, f2);
195     result = f1.result + f2.result;
196     }
197     }
198     }
199    
200     // A recursive action failing in base case
201     static final class FailingFibAction extends RecursiveAction {
202     final int number;
203     int result;
204     FailingFibAction(int n) { number = n; }
205     public void compute() {
206     int n = number;
207     if (n <= 1)
208     throw new FJException();
209     else {
210     FailingFibAction f1 = new FailingFibAction(n - 1);
211     FailingFibAction f2 = new FailingFibAction(n - 2);
212     invokeAll(f1, f2);
213     result = f1.result + f2.result;
214     }
215     }
216     }
217    
218     /**
219     * invoke returns when task completes normally.
220     * isCompletedAbnormally and isCancelled return false for normally
221     * completed tasks. getRawResult of a RecursiveAction returns null;
222     */
223     public void testInvoke() {
224     RecursiveAction a = new CheckedRecursiveAction() {
225 jsr166 1.9 protected void realCompute() {
226 dl 1.4 FibAction f = new FibAction(8);
227     assertNull(f.invoke());
228     assertEquals(21, f.result);
229     checkCompletedNormally(f);
230     }};
231     checkInvoke(a);
232     }
233    
234     /**
235     * quietlyInvoke task returns when task completes normally.
236     * isCompletedAbnormally and isCancelled return false for normally
237     * completed tasks
238     */
239     public void testQuietlyInvoke() {
240     RecursiveAction a = new CheckedRecursiveAction() {
241 jsr166 1.9 protected void realCompute() {
242 dl 1.4 FibAction f = new FibAction(8);
243     f.quietlyInvoke();
244     assertEquals(21, f.result);
245     checkCompletedNormally(f);
246     }};
247     checkInvoke(a);
248     }
249    
250     /**
251     * join of a forked task returns when task completes
252     */
253     public void testForkJoin() {
254     RecursiveAction a = new CheckedRecursiveAction() {
255 jsr166 1.9 protected void realCompute() {
256 dl 1.4 FibAction f = new FibAction(8);
257     assertSame(f, f.fork());
258     assertNull(f.join());
259     assertEquals(21, f.result);
260     checkCompletedNormally(f);
261     }};
262     checkInvoke(a);
263     }
264    
265     /**
266     * join/quietlyJoin of a forked task succeeds in the presence of interrupts
267     */
268     public void testJoinIgnoresInterrupts() {
269     RecursiveAction a = new CheckedRecursiveAction() {
270 jsr166 1.9 protected void realCompute() {
271 dl 1.4 FibAction f = new FibAction(8);
272     final Thread myself = Thread.currentThread();
273    
274     // test join()
275     assertSame(f, f.fork());
276     myself.interrupt();
277     assertTrue(myself.isInterrupted());
278     assertNull(f.join());
279     Thread.interrupted();
280     assertEquals(21, f.result);
281     checkCompletedNormally(f);
282    
283     f = new FibAction(8);
284     f.cancel(true);
285     assertSame(f, f.fork());
286     myself.interrupt();
287     assertTrue(myself.isInterrupted());
288     try {
289     f.join();
290     shouldThrow();
291     } catch (CancellationException success) {
292     Thread.interrupted();
293     checkCancelled(f);
294     }
295    
296     f = new FibAction(8);
297     f.completeExceptionally(new FJException());
298     assertSame(f, f.fork());
299     myself.interrupt();
300     assertTrue(myself.isInterrupted());
301     try {
302     f.join();
303     shouldThrow();
304     } catch (FJException success) {
305     Thread.interrupted();
306     checkCompletedAbnormally(f, success);
307     }
308    
309     // test quietlyJoin()
310     f = new FibAction(8);
311     assertSame(f, f.fork());
312     myself.interrupt();
313     assertTrue(myself.isInterrupted());
314     f.quietlyJoin();
315     Thread.interrupted();
316     assertEquals(21, f.result);
317     checkCompletedNormally(f);
318    
319     f = new FibAction(8);
320     f.cancel(true);
321     assertSame(f, f.fork());
322     myself.interrupt();
323     assertTrue(myself.isInterrupted());
324     f.quietlyJoin();
325     Thread.interrupted();
326     checkCancelled(f);
327    
328     f = new FibAction(8);
329     f.completeExceptionally(new FJException());
330     assertSame(f, f.fork());
331     myself.interrupt();
332     assertTrue(myself.isInterrupted());
333     f.quietlyJoin();
334     Thread.interrupted();
335     checkCompletedAbnormally(f, f.getException());
336     }};
337     checkInvoke(a);
338     a.reinitialize();
339     checkInvoke(a);
340     }
341    
342     /**
343     * get of a forked task returns when task completes
344     */
345     public void testForkGet() {
346     RecursiveAction a = new CheckedRecursiveAction() {
347 jsr166 1.9 protected void realCompute() throws Exception {
348 dl 1.4 FibAction f = new FibAction(8);
349     assertSame(f, f.fork());
350     assertNull(f.get());
351     assertEquals(21, f.result);
352     checkCompletedNormally(f);
353     }};
354     checkInvoke(a);
355     }
356    
357     /**
358     * timed get of a forked task returns when task completes
359     */
360     public void testForkTimedGet() {
361     RecursiveAction a = new CheckedRecursiveAction() {
362 jsr166 1.9 protected void realCompute() throws Exception {
363 dl 1.4 FibAction f = new FibAction(8);
364     assertSame(f, f.fork());
365     assertNull(f.get(5L, SECONDS));
366     assertEquals(21, f.result);
367     checkCompletedNormally(f);
368     }};
369     checkInvoke(a);
370     }
371    
372     /**
373     * timed get with null time unit throws NPE
374     */
375     public void testForkTimedGetNPE() {
376     RecursiveAction a = new CheckedRecursiveAction() {
377 jsr166 1.9 protected void realCompute() throws Exception {
378 dl 1.4 FibAction f = new FibAction(8);
379     assertSame(f, f.fork());
380     try {
381     f.get(5L, null);
382     shouldThrow();
383     } catch (NullPointerException success) {}
384     }};
385     checkInvoke(a);
386     }
387    
388     /**
389     * quietlyJoin of a forked task returns when task completes
390     */
391     public void testForkQuietlyJoin() {
392     RecursiveAction a = new CheckedRecursiveAction() {
393 jsr166 1.9 protected void realCompute() {
394 dl 1.4 FibAction f = new FibAction(8);
395     assertSame(f, f.fork());
396     f.quietlyJoin();
397     assertEquals(21, f.result);
398     checkCompletedNormally(f);
399     }};
400     checkInvoke(a);
401     }
402    
403     /**
404     * invoke task throws exception when task completes abnormally
405     */
406     public void testAbnormalInvoke() {
407     RecursiveAction a = new CheckedRecursiveAction() {
408 jsr166 1.9 protected void realCompute() {
409 dl 1.4 FailingFibAction f = new FailingFibAction(8);
410     try {
411     f.invoke();
412     shouldThrow();
413     } catch (FJException success) {
414     checkCompletedAbnormally(f, success);
415     }
416     }};
417     checkInvoke(a);
418     }
419    
420     /**
421     * quietlyInvoke task returns when task completes abnormally
422     */
423     public void testAbnormalQuietlyInvoke() {
424     RecursiveAction a = new CheckedRecursiveAction() {
425 jsr166 1.9 protected void realCompute() {
426 dl 1.4 FailingFibAction f = new FailingFibAction(8);
427     f.quietlyInvoke();
428     assertTrue(f.getException() instanceof FJException);
429     checkCompletedAbnormally(f, f.getException());
430     }};
431     checkInvoke(a);
432     }
433    
434     /**
435     * join of a forked task throws exception when task completes abnormally
436     */
437     public void testAbnormalForkJoin() {
438     RecursiveAction a = new CheckedRecursiveAction() {
439 jsr166 1.9 protected void realCompute() {
440 dl 1.4 FailingFibAction f = new FailingFibAction(8);
441     assertSame(f, f.fork());
442     try {
443     f.join();
444     shouldThrow();
445     } catch (FJException success) {
446     checkCompletedAbnormally(f, success);
447     }
448     }};
449     checkInvoke(a);
450     }
451    
452     /**
453     * get of a forked task throws exception when task completes abnormally
454     */
455     public void testAbnormalForkGet() {
456     RecursiveAction a = new CheckedRecursiveAction() {
457 jsr166 1.9 protected void realCompute() throws Exception {
458 dl 1.4 FailingFibAction f = new FailingFibAction(8);
459     assertSame(f, f.fork());
460     try {
461     f.get();
462     shouldThrow();
463     } catch (ExecutionException success) {
464     Throwable cause = success.getCause();
465     assertTrue(cause instanceof FJException);
466     checkCompletedAbnormally(f, cause);
467     }
468     }};
469     checkInvoke(a);
470     }
471    
472     /**
473     * timed get of a forked task throws exception when task completes abnormally
474     */
475     public void testAbnormalForkTimedGet() {
476     RecursiveAction a = new CheckedRecursiveAction() {
477 jsr166 1.9 protected void realCompute() throws Exception {
478 dl 1.4 FailingFibAction f = new FailingFibAction(8);
479     assertSame(f, f.fork());
480     try {
481 jsr166 1.28 f.get(5L, SECONDS);
482 dl 1.4 shouldThrow();
483     } catch (ExecutionException success) {
484     Throwable cause = success.getCause();
485     assertTrue(cause instanceof FJException);
486     checkCompletedAbnormally(f, cause);
487     }
488     }};
489     checkInvoke(a);
490     }
491    
492     /**
493     * quietlyJoin of a forked task returns when task completes abnormally
494     */
495     public void testAbnormalForkQuietlyJoin() {
496     RecursiveAction a = new CheckedRecursiveAction() {
497 jsr166 1.9 protected void realCompute() {
498 dl 1.4 FailingFibAction f = new FailingFibAction(8);
499     assertSame(f, f.fork());
500     f.quietlyJoin();
501     assertTrue(f.getException() instanceof FJException);
502     checkCompletedAbnormally(f, f.getException());
503     }};
504     checkInvoke(a);
505     }
506    
507     /**
508     * invoke task throws exception when task cancelled
509     */
510     public void testCancelledInvoke() {
511     RecursiveAction a = new CheckedRecursiveAction() {
512 jsr166 1.9 protected void realCompute() {
513 dl 1.4 FibAction f = new FibAction(8);
514     assertTrue(f.cancel(true));
515     try {
516     f.invoke();
517     shouldThrow();
518     } catch (CancellationException success) {
519     checkCancelled(f);
520     }
521     }};
522     checkInvoke(a);
523     }
524    
525     /**
526     * join of a forked task throws exception when task cancelled
527     */
528     public void testCancelledForkJoin() {
529     RecursiveAction a = new CheckedRecursiveAction() {
530 jsr166 1.9 protected void realCompute() {
531 dl 1.4 FibAction f = new FibAction(8);
532     assertTrue(f.cancel(true));
533     assertSame(f, f.fork());
534     try {
535     f.join();
536     shouldThrow();
537     } catch (CancellationException success) {
538     checkCancelled(f);
539     }
540     }};
541     checkInvoke(a);
542     }
543    
544     /**
545     * get of a forked task throws exception when task cancelled
546     */
547     public void testCancelledForkGet() {
548     RecursiveAction a = new CheckedRecursiveAction() {
549 jsr166 1.9 protected void realCompute() throws Exception {
550 dl 1.4 FibAction f = new FibAction(8);
551     assertTrue(f.cancel(true));
552     assertSame(f, f.fork());
553     try {
554     f.get();
555     shouldThrow();
556     } catch (CancellationException success) {
557     checkCancelled(f);
558     }
559     }};
560     checkInvoke(a);
561     }
562    
563     /**
564     * timed get of a forked task throws exception when task cancelled
565     */
566     public void testCancelledForkTimedGet() {
567     RecursiveAction a = new CheckedRecursiveAction() {
568 jsr166 1.9 protected void realCompute() throws Exception {
569 dl 1.4 FibAction f = new FibAction(8);
570     assertTrue(f.cancel(true));
571     assertSame(f, f.fork());
572     try {
573     f.get(5L, SECONDS);
574     shouldThrow();
575     } catch (CancellationException success) {
576     checkCancelled(f);
577     }
578     }};
579     checkInvoke(a);
580     }
581    
582     /**
583     * quietlyJoin of a forked task returns when task cancelled
584     */
585     public void testCancelledForkQuietlyJoin() {
586     RecursiveAction a = new CheckedRecursiveAction() {
587 jsr166 1.9 protected void realCompute() {
588 dl 1.4 FibAction f = new FibAction(8);
589     assertTrue(f.cancel(true));
590     assertSame(f, f.fork());
591     f.quietlyJoin();
592     checkCancelled(f);
593     }};
594     checkInvoke(a);
595     }
596    
597     /**
598     * inForkJoinPool of non-FJ task returns false
599     */
600     public void testInForkJoinPool2() {
601     RecursiveAction a = new CheckedRecursiveAction() {
602 jsr166 1.9 protected void realCompute() {
603 dl 1.4 assertFalse(inForkJoinPool());
604     }};
605     assertNull(a.invoke());
606     }
607    
608     /**
609     * A reinitialized normally completed task may be re-invoked
610     */
611     public void testReinitialize() {
612     RecursiveAction a = new CheckedRecursiveAction() {
613 jsr166 1.9 protected void realCompute() {
614 dl 1.4 FibAction f = new FibAction(8);
615     checkNotDone(f);
616    
617     for (int i = 0; i < 3; i++) {
618     assertNull(f.invoke());
619     assertEquals(21, f.result);
620     checkCompletedNormally(f);
621     f.reinitialize();
622     checkNotDone(f);
623     }
624     }};
625     checkInvoke(a);
626     }
627    
628     /**
629     * A reinitialized abnormally completed task may be re-invoked
630     */
631     public void testReinitializeAbnormal() {
632     RecursiveAction a = new CheckedRecursiveAction() {
633 jsr166 1.9 protected void realCompute() {
634 dl 1.4 FailingFibAction f = new FailingFibAction(8);
635     checkNotDone(f);
636    
637     for (int i = 0; i < 3; i++) {
638     try {
639     f.invoke();
640     shouldThrow();
641     } catch (FJException success) {
642     checkCompletedAbnormally(f, success);
643     }
644     f.reinitialize();
645     checkNotDone(f);
646     }
647     }};
648     checkInvoke(a);
649     }
650    
651     /**
652     * invoke task throws exception after invoking completeExceptionally
653     */
654     public void testCompleteExceptionally() {
655     RecursiveAction a = new CheckedRecursiveAction() {
656 jsr166 1.9 protected void realCompute() {
657 dl 1.4 FibAction f = new FibAction(8);
658     f.completeExceptionally(new FJException());
659     try {
660     f.invoke();
661     shouldThrow();
662     } catch (FJException success) {
663     checkCompletedAbnormally(f, success);
664     }
665     }};
666     checkInvoke(a);
667     }
668    
669     /**
670     * invoke task suppresses execution invoking complete
671     */
672     public void testComplete() {
673     RecursiveAction a = new CheckedRecursiveAction() {
674 jsr166 1.9 protected void realCompute() {
675 dl 1.4 FibAction f = new FibAction(8);
676     f.complete(null);
677     assertNull(f.invoke());
678     assertEquals(0, f.result);
679     checkCompletedNormally(f);
680     }};
681     checkInvoke(a);
682     }
683    
684     /**
685     * invokeAll(t1, t2) invokes all task arguments
686     */
687     public void testInvokeAll2() {
688     RecursiveAction a = new CheckedRecursiveAction() {
689 jsr166 1.9 protected void realCompute() {
690 dl 1.4 FibAction f = new FibAction(8);
691     FibAction g = new FibAction(9);
692     invokeAll(f, g);
693     checkCompletedNormally(f);
694     assertEquals(21, f.result);
695     checkCompletedNormally(g);
696     assertEquals(34, g.result);
697     }};
698     checkInvoke(a);
699     }
700    
701     /**
702     * invokeAll(tasks) with 1 argument invokes task
703     */
704     public void testInvokeAll1() {
705     RecursiveAction a = new CheckedRecursiveAction() {
706 jsr166 1.9 protected void realCompute() {
707 dl 1.4 FibAction f = new FibAction(8);
708     invokeAll(f);
709     checkCompletedNormally(f);
710     assertEquals(21, f.result);
711     }};
712     checkInvoke(a);
713     }
714    
715     /**
716     * invokeAll(tasks) with > 2 argument invokes tasks
717     */
718     public void testInvokeAll3() {
719     RecursiveAction a = new CheckedRecursiveAction() {
720 jsr166 1.9 protected void realCompute() {
721 dl 1.4 FibAction f = new FibAction(8);
722     FibAction g = new FibAction(9);
723     FibAction h = new FibAction(7);
724     invokeAll(f, g, h);
725     assertTrue(f.isDone());
726     assertTrue(g.isDone());
727     assertTrue(h.isDone());
728     checkCompletedNormally(f);
729     assertEquals(21, f.result);
730     checkCompletedNormally(g);
731     assertEquals(34, g.result);
732     checkCompletedNormally(g);
733     assertEquals(13, h.result);
734     }};
735     checkInvoke(a);
736     }
737    
738     /**
739     * invokeAll(collection) invokes all tasks in the collection
740     */
741     public void testInvokeAllCollection() {
742     RecursiveAction a = new CheckedRecursiveAction() {
743 jsr166 1.9 protected void realCompute() {
744 dl 1.4 FibAction f = new FibAction(8);
745     FibAction g = new FibAction(9);
746     FibAction h = new FibAction(7);
747     HashSet set = new HashSet();
748     set.add(f);
749     set.add(g);
750     set.add(h);
751     invokeAll(set);
752     assertTrue(f.isDone());
753     assertTrue(g.isDone());
754     assertTrue(h.isDone());
755     checkCompletedNormally(f);
756     assertEquals(21, f.result);
757     checkCompletedNormally(g);
758     assertEquals(34, g.result);
759     checkCompletedNormally(g);
760     assertEquals(13, h.result);
761     }};
762     checkInvoke(a);
763     }
764    
765     /**
766     * invokeAll(tasks) with any null task throws NPE
767     */
768     public void testInvokeAllNPE() {
769     RecursiveAction a = new CheckedRecursiveAction() {
770 jsr166 1.9 protected void realCompute() {
771 dl 1.4 FibAction f = new FibAction(8);
772     FibAction g = new FibAction(9);
773     FibAction h = null;
774     try {
775     invokeAll(f, g, h);
776     shouldThrow();
777     } catch (NullPointerException success) {}
778     }};
779     checkInvoke(a);
780     }
781    
782     /**
783     * invokeAll(t1, t2) throw exception if any task does
784     */
785     public void testAbnormalInvokeAll2() {
786     RecursiveAction a = new CheckedRecursiveAction() {
787 jsr166 1.9 protected void realCompute() {
788 dl 1.4 FibAction f = new FibAction(8);
789     FailingFibAction g = new FailingFibAction(9);
790     try {
791     invokeAll(f, g);
792     shouldThrow();
793     } catch (FJException success) {
794     checkCompletedAbnormally(g, success);
795     }
796     }};
797     checkInvoke(a);
798     }
799    
800     /**
801     * invokeAll(tasks) with 1 argument throws exception if task does
802     */
803     public void testAbnormalInvokeAll1() {
804     RecursiveAction a = new CheckedRecursiveAction() {
805 jsr166 1.9 protected void realCompute() {
806 dl 1.4 FailingFibAction g = new FailingFibAction(9);
807     try {
808     invokeAll(g);
809     shouldThrow();
810     } catch (FJException success) {
811     checkCompletedAbnormally(g, success);
812     }
813     }};
814     checkInvoke(a);
815     }
816    
817     /**
818     * invokeAll(tasks) with > 2 argument throws exception if any task does
819     */
820     public void testAbnormalInvokeAll3() {
821     RecursiveAction a = new CheckedRecursiveAction() {
822 jsr166 1.9 protected void realCompute() {
823 dl 1.4 FibAction f = new FibAction(8);
824     FailingFibAction g = new FailingFibAction(9);
825     FibAction h = new FibAction(7);
826     try {
827     invokeAll(f, g, h);
828     shouldThrow();
829     } catch (FJException success) {
830     checkCompletedAbnormally(g, success);
831     }
832     }};
833     checkInvoke(a);
834     }
835    
836     /**
837     * invokeAll(collection) throws exception if any task does
838     */
839     public void testAbnormalInvokeAllCollection() {
840     RecursiveAction a = new CheckedRecursiveAction() {
841 jsr166 1.9 protected void realCompute() {
842 dl 1.4 FailingFibAction f = new FailingFibAction(8);
843     FibAction g = new FibAction(9);
844     FibAction h = new FibAction(7);
845     HashSet set = new HashSet();
846     set.add(f);
847     set.add(g);
848     set.add(h);
849     try {
850     invokeAll(set);
851     shouldThrow();
852     } catch (FJException success) {
853     checkCompletedAbnormally(f, success);
854     }
855     }};
856     checkInvoke(a);
857     }
858    
859     // CountedCompleter versions
860    
861 jsr166 1.5 abstract static class CCF extends CountedCompleter {
862 dl 1.4 int number;
863     int rnumber;
864    
865     public CCF(CountedCompleter parent, int n) {
866     super(parent, 1);
867     this.number = n;
868     }
869    
870     public final void compute() {
871     CountedCompleter p;
872     CCF f = this;
873     int n = number;
874     while (n >= 2) {
875     new RCCF(f, n - 2).fork();
876     f = new LCCF(f, --n);
877     }
878     f.number = n;
879     f.onCompletion(f);
880     if ((p = f.getCompleter()) != null)
881     p.tryComplete();
882 jsr166 1.5 else
883     f.quietlyComplete();
884 dl 1.4 }
885     }
886    
887     static final class LCCF extends CCF {
888     public LCCF(CountedCompleter parent, int n) {
889     super(parent, n);
890     }
891     public final void onCompletion(CountedCompleter caller) {
892     CCF p = (CCF)getCompleter();
893     int n = number + rnumber;
894     if (p != null)
895     p.number = n;
896     else
897     number = n;
898     }
899     }
900     static final class RCCF extends CCF {
901     public RCCF(CountedCompleter parent, int n) {
902     super(parent, n);
903     }
904     public final void onCompletion(CountedCompleter caller) {
905     CCF p = (CCF)getCompleter();
906     int n = number + rnumber;
907     if (p != null)
908     p.rnumber = n;
909     else
910     number = n;
911     }
912     }
913    
914     // Version of CCF with forced failure in left completions
915 jsr166 1.5 abstract static class FailingCCF extends CountedCompleter {
916 dl 1.4 int number;
917     int rnumber;
918    
919     public FailingCCF(CountedCompleter parent, int n) {
920     super(parent, 1);
921     this.number = n;
922     }
923    
924     public final void compute() {
925     CountedCompleter p;
926     FailingCCF f = this;
927     int n = number;
928     while (n >= 2) {
929     new RFCCF(f, n - 2).fork();
930     f = new LFCCF(f, --n);
931     }
932     f.number = n;
933     f.onCompletion(f);
934     if ((p = f.getCompleter()) != null)
935     p.tryComplete();
936 jsr166 1.5 else
937     f.quietlyComplete();
938 dl 1.4 }
939     }
940    
941     static final class LFCCF extends FailingCCF {
942     public LFCCF(CountedCompleter parent, int n) {
943     super(parent, n);
944     }
945     public final void onCompletion(CountedCompleter caller) {
946     FailingCCF p = (FailingCCF)getCompleter();
947     int n = number + rnumber;
948     if (p != null)
949     p.number = n;
950     else
951     number = n;
952     }
953     }
954     static final class RFCCF extends FailingCCF {
955     public RFCCF(CountedCompleter parent, int n) {
956     super(parent, n);
957     }
958     public final void onCompletion(CountedCompleter caller) {
959     completeExceptionally(new FJException());
960     }
961     }
962 jsr166 1.5
963 dl 1.4 /**
964     * invoke returns when task completes normally.
965     * isCompletedAbnormally and isCancelled return false for normally
966     * completed tasks; getRawResult returns null.
967     */
968     public void testInvokeCC() {
969 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
970     protected void realCompute() {
971 dl 1.4 CCF f = new LCCF(null, 8);
972     assertNull(f.invoke());
973     assertEquals(21, f.number);
974     checkCompletedNormally(f);
975     }};
976     checkInvoke(a);
977     }
978    
979     /**
980     * quietlyInvoke task returns when task completes normally.
981     * isCompletedAbnormally and isCancelled return false for normally
982     * completed tasks
983     */
984     public void testQuietlyInvokeCC() {
985 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
986     protected void realCompute() {
987 dl 1.4 CCF f = new LCCF(null, 8);
988     f.quietlyInvoke();
989     assertEquals(21, f.number);
990     checkCompletedNormally(f);
991     }};
992     checkInvoke(a);
993     }
994    
995     /**
996     * join of a forked task returns when task completes
997     */
998     public void testForkJoinCC() {
999 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1000     protected void realCompute() {
1001 dl 1.4 CCF f = new LCCF(null, 8);
1002     assertSame(f, f.fork());
1003     assertNull(f.join());
1004     assertEquals(21, f.number);
1005     checkCompletedNormally(f);
1006     }};
1007     checkInvoke(a);
1008     }
1009    
1010     /**
1011     * get of a forked task returns when task completes
1012     */
1013     public void testForkGetCC() {
1014 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1015     protected void realCompute() throws Exception {
1016 dl 1.4 CCF f = new LCCF(null, 8);
1017     assertSame(f, f.fork());
1018     assertNull(f.get());
1019     assertEquals(21, f.number);
1020     checkCompletedNormally(f);
1021     }};
1022     checkInvoke(a);
1023     }
1024    
1025     /**
1026     * timed get of a forked task returns when task completes
1027     */
1028     public void testForkTimedGetCC() {
1029 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1030     protected void realCompute() throws Exception {
1031 dl 1.4 CCF f = new LCCF(null, 8);
1032     assertSame(f, f.fork());
1033     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1034     assertEquals(21, f.number);
1035     checkCompletedNormally(f);
1036     }};
1037     checkInvoke(a);
1038     }
1039    
1040     /**
1041     * timed get with null time unit throws NPE
1042     */
1043     public void testForkTimedGetNPECC() {
1044 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1045     protected void realCompute() throws Exception {
1046 dl 1.4 CCF f = new LCCF(null, 8);
1047     assertSame(f, f.fork());
1048     try {
1049     f.get(5L, null);
1050     shouldThrow();
1051     } catch (NullPointerException success) {}
1052     }};
1053     checkInvoke(a);
1054     }
1055    
1056     /**
1057     * quietlyJoin of a forked task returns when task completes
1058     */
1059     public void testForkQuietlyJoinCC() {
1060 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1061     protected void realCompute() {
1062 dl 1.4 CCF f = new LCCF(null, 8);
1063     assertSame(f, f.fork());
1064     f.quietlyJoin();
1065     assertEquals(21, f.number);
1066     checkCompletedNormally(f);
1067     }};
1068     checkInvoke(a);
1069     }
1070    
1071     /**
1072     * invoke task throws exception when task completes abnormally
1073     */
1074     public void testAbnormalInvokeCC() {
1075 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1076     protected void realCompute() {
1077 dl 1.4 FailingCCF f = new LFCCF(null, 8);
1078     try {
1079     f.invoke();
1080     shouldThrow();
1081     } catch (FJException success) {
1082     checkCompletedAbnormally(f, success);
1083     }
1084     }};
1085     checkInvoke(a);
1086     }
1087    
1088     /**
1089     * quietlyInvoke task returns when task completes abnormally
1090     */
1091     public void testAbnormalQuietlyInvokeCC() {
1092 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1093     protected void realCompute() {
1094 dl 1.4 FailingCCF f = new LFCCF(null, 8);
1095     f.quietlyInvoke();
1096     assertTrue(f.getException() instanceof FJException);
1097     checkCompletedAbnormally(f, f.getException());
1098     }};
1099     checkInvoke(a);
1100     }
1101    
1102     /**
1103     * join of a forked task throws exception when task completes abnormally
1104     */
1105     public void testAbnormalForkJoinCC() {
1106 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1107     protected void realCompute() {
1108 dl 1.4 FailingCCF f = new LFCCF(null, 8);
1109     assertSame(f, f.fork());
1110     try {
1111     f.join();
1112     shouldThrow();
1113     } catch (FJException success) {
1114     checkCompletedAbnormally(f, success);
1115     }
1116     }};
1117     checkInvoke(a);
1118     }
1119    
1120     /**
1121     * get of a forked task throws exception when task completes abnormally
1122     */
1123     public void testAbnormalForkGetCC() {
1124 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1125     protected void realCompute() throws Exception {
1126 dl 1.4 FailingCCF f = new LFCCF(null, 8);
1127     assertSame(f, f.fork());
1128     try {
1129     f.get();
1130     shouldThrow();
1131     } catch (ExecutionException success) {
1132     Throwable cause = success.getCause();
1133     assertTrue(cause instanceof FJException);
1134     checkCompletedAbnormally(f, cause);
1135     }
1136     }};
1137     checkInvoke(a);
1138     }
1139    
1140     /**
1141     * timed get of a forked task throws exception when task completes abnormally
1142     */
1143     public void testAbnormalForkTimedGetCC() {
1144 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1145     protected void realCompute() throws Exception {
1146 dl 1.4 FailingCCF f = new LFCCF(null, 8);
1147     assertSame(f, f.fork());
1148     try {
1149     f.get(LONG_DELAY_MS, MILLISECONDS);
1150     shouldThrow();
1151     } catch (ExecutionException success) {
1152     Throwable cause = success.getCause();
1153     assertTrue(cause instanceof FJException);
1154     checkCompletedAbnormally(f, cause);
1155     }
1156     }};
1157     checkInvoke(a);
1158     }
1159    
1160     /**
1161     * quietlyJoin of a forked task returns when task completes abnormally
1162     */
1163     public void testAbnormalForkQuietlyJoinCC() {
1164 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1165     protected void realCompute() {
1166 dl 1.4 FailingCCF f = new LFCCF(null, 8);
1167     assertSame(f, f.fork());
1168     f.quietlyJoin();
1169     assertTrue(f.getException() instanceof FJException);
1170     checkCompletedAbnormally(f, f.getException());
1171     }};
1172     checkInvoke(a);
1173     }
1174    
1175     /**
1176     * invoke task throws exception when task cancelled
1177     */
1178     public void testCancelledInvokeCC() {
1179 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1180     protected void realCompute() {
1181 dl 1.4 CCF f = new LCCF(null, 8);
1182     assertTrue(f.cancel(true));
1183     try {
1184     f.invoke();
1185     shouldThrow();
1186     } catch (CancellationException success) {
1187     checkCancelled(f);
1188     }
1189     }};
1190     checkInvoke(a);
1191     }
1192    
1193     /**
1194     * join of a forked task throws exception when task cancelled
1195     */
1196     public void testCancelledForkJoinCC() {
1197 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1198     protected void realCompute() {
1199 dl 1.4 CCF f = new LCCF(null, 8);
1200     assertTrue(f.cancel(true));
1201     assertSame(f, f.fork());
1202     try {
1203     f.join();
1204     shouldThrow();
1205     } catch (CancellationException success) {
1206     checkCancelled(f);
1207     }
1208     }};
1209     checkInvoke(a);
1210     }
1211    
1212     /**
1213     * get of a forked task throws exception when task cancelled
1214     */
1215     public void testCancelledForkGetCC() {
1216 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1217     protected void realCompute() throws Exception {
1218 dl 1.4 CCF f = new LCCF(null, 8);
1219     assertTrue(f.cancel(true));
1220     assertSame(f, f.fork());
1221     try {
1222     f.get();
1223     shouldThrow();
1224     } catch (CancellationException success) {
1225     checkCancelled(f);
1226     }
1227     }};
1228     checkInvoke(a);
1229     }
1230    
1231     /**
1232     * timed get of a forked task throws exception when task cancelled
1233     */
1234     public void testCancelledForkTimedGetCC() throws Exception {
1235 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1236     protected void realCompute() throws Exception {
1237 dl 1.4 CCF f = new LCCF(null, 8);
1238     assertTrue(f.cancel(true));
1239     assertSame(f, f.fork());
1240     try {
1241     f.get(LONG_DELAY_MS, MILLISECONDS);
1242     shouldThrow();
1243     } catch (CancellationException success) {
1244     checkCancelled(f);
1245     }
1246     }};
1247     checkInvoke(a);
1248     }
1249    
1250     /**
1251     * quietlyJoin of a forked task returns when task cancelled
1252     */
1253     public void testCancelledForkQuietlyJoinCC() {
1254 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1255     protected void realCompute() {
1256 dl 1.4 CCF f = new LCCF(null, 8);
1257     assertTrue(f.cancel(true));
1258     assertSame(f, f.fork());
1259     f.quietlyJoin();
1260     checkCancelled(f);
1261     }};
1262     checkInvoke(a);
1263     }
1264    
1265     /**
1266     * getPool of non-FJ task returns null
1267     */
1268     public void testGetPool2CC() {
1269 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1270     protected void realCompute() {
1271 dl 1.4 assertNull(getPool());
1272     }};
1273     assertNull(a.invoke());
1274     }
1275    
1276     /**
1277     * inForkJoinPool of non-FJ task returns false
1278     */
1279     public void testInForkJoinPool2CC() {
1280 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1281     protected void realCompute() {
1282 dl 1.4 assertFalse(inForkJoinPool());
1283     }};
1284     assertNull(a.invoke());
1285     }
1286    
1287     /**
1288     * setRawResult(null) succeeds
1289     */
1290     public void testSetRawResultCC() {
1291 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1292     protected void realCompute() {
1293 dl 1.4 setRawResult(null);
1294     assertNull(getRawResult());
1295     }};
1296     assertNull(a.invoke());
1297     }
1298    
1299     /**
1300     * invoke task throws exception after invoking completeExceptionally
1301     */
1302     public void testCompleteExceptionally2CC() {
1303 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1304     protected void realCompute() {
1305 dl 1.4 CCF f = new LCCF(null, 8);
1306     f.completeExceptionally(new FJException());
1307     try {
1308     f.invoke();
1309     shouldThrow();
1310     } catch (FJException success) {
1311     checkCompletedAbnormally(f, success);
1312     }
1313     }};
1314     checkInvoke(a);
1315     }
1316    
1317     /**
1318     * invokeAll(t1, t2) invokes all task arguments
1319     */
1320     public void testInvokeAll2CC() {
1321 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1322     protected void realCompute() {
1323 dl 1.4 CCF f = new LCCF(null, 8);
1324     CCF g = new LCCF(null, 9);
1325     invokeAll(f, g);
1326     assertEquals(21, f.number);
1327     assertEquals(34, g.number);
1328     checkCompletedNormally(f);
1329     checkCompletedNormally(g);
1330     }};
1331     checkInvoke(a);
1332     }
1333    
1334     /**
1335     * invokeAll(tasks) with 1 argument invokes task
1336     */
1337     public void testInvokeAll1CC() {
1338 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1339     protected void realCompute() {
1340 dl 1.4 CCF f = new LCCF(null, 8);
1341     invokeAll(f);
1342     checkCompletedNormally(f);
1343     assertEquals(21, f.number);
1344     }};
1345     checkInvoke(a);
1346     }
1347    
1348     /**
1349     * invokeAll(tasks) with > 2 argument invokes tasks
1350     */
1351     public void testInvokeAll3CC() {
1352 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1353     protected void realCompute() {
1354 dl 1.4 CCF f = new LCCF(null, 8);
1355     CCF g = new LCCF(null, 9);
1356     CCF h = new LCCF(null, 7);
1357     invokeAll(f, g, h);
1358     assertEquals(21, f.number);
1359     assertEquals(34, g.number);
1360     assertEquals(13, h.number);
1361     checkCompletedNormally(f);
1362     checkCompletedNormally(g);
1363     checkCompletedNormally(h);
1364     }};
1365     checkInvoke(a);
1366     }
1367    
1368     /**
1369     * invokeAll(collection) invokes all tasks in the collection
1370     */
1371     public void testInvokeAllCollectionCC() {
1372 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1373     protected void realCompute() {
1374 dl 1.4 CCF f = new LCCF(null, 8);
1375     CCF g = new LCCF(null, 9);
1376     CCF h = new LCCF(null, 7);
1377     HashSet set = new HashSet();
1378     set.add(f);
1379     set.add(g);
1380     set.add(h);
1381     invokeAll(set);
1382     assertEquals(21, f.number);
1383     assertEquals(34, g.number);
1384     assertEquals(13, h.number);
1385     checkCompletedNormally(f);
1386     checkCompletedNormally(g);
1387     checkCompletedNormally(h);
1388     }};
1389     checkInvoke(a);
1390     }
1391    
1392     /**
1393     * invokeAll(tasks) with any null task throws NPE
1394     */
1395     public void testInvokeAllNPECC() {
1396 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1397     protected void realCompute() {
1398 dl 1.4 CCF f = new LCCF(null, 8);
1399     CCF g = new LCCF(null, 9);
1400     CCF h = null;
1401     try {
1402     invokeAll(f, g, h);
1403     shouldThrow();
1404     } catch (NullPointerException success) {}
1405     }};
1406     checkInvoke(a);
1407     }
1408    
1409     /**
1410     * invokeAll(t1, t2) throw exception if any task does
1411     */
1412     public void testAbnormalInvokeAll2CC() {
1413 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1414     protected void realCompute() {
1415 dl 1.4 CCF f = new LCCF(null, 8);
1416     FailingCCF g = new LFCCF(null, 9);
1417     try {
1418     invokeAll(f, g);
1419     shouldThrow();
1420     } catch (FJException success) {
1421     checkCompletedAbnormally(g, success);
1422     }
1423     }};
1424     checkInvoke(a);
1425     }
1426    
1427     /**
1428     * invokeAll(tasks) with 1 argument throws exception if task does
1429     */
1430     public void testAbnormalInvokeAll1CC() {
1431 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1432     protected void realCompute() {
1433 dl 1.4 FailingCCF g = new LFCCF(null, 9);
1434     try {
1435     invokeAll(g);
1436     shouldThrow();
1437     } catch (FJException success) {
1438     checkCompletedAbnormally(g, success);
1439     }
1440     }};
1441     checkInvoke(a);
1442     }
1443    
1444     /**
1445     * invokeAll(tasks) with > 2 argument throws exception if any task does
1446     */
1447     public void testAbnormalInvokeAll3CC() {
1448 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1449     protected void realCompute() {
1450 dl 1.4 CCF f = new LCCF(null, 8);
1451     FailingCCF g = new LFCCF(null, 9);
1452     CCF h = new LCCF(null, 7);
1453     try {
1454     invokeAll(f, g, h);
1455     shouldThrow();
1456     } catch (FJException success) {
1457     checkCompletedAbnormally(g, success);
1458     }
1459     }};
1460     checkInvoke(a);
1461     }
1462    
1463     /**
1464 jsr166 1.14 * invokeAll(collection) throws exception if any task does
1465 dl 1.4 */
1466     public void testAbnormalInvokeAllCollectionCC() {
1467 jsr166 1.9 ForkJoinTask a = new CheckedRecursiveAction() {
1468     protected void realCompute() {
1469 dl 1.4 FailingCCF f = new LFCCF(null, 8);
1470     CCF g = new LCCF(null, 9);
1471     CCF h = new LCCF(null, 7);
1472     HashSet set = new HashSet();
1473     set.add(f);
1474     set.add(g);
1475     set.add(h);
1476     try {
1477     invokeAll(set);
1478     shouldThrow();
1479     } catch (FJException success) {
1480     checkCompletedAbnormally(f, success);
1481     }
1482     }};
1483     checkInvoke(a);
1484     }
1485    
1486 dl 1.11 /**
1487 jsr166 1.18 * awaitQuiescence by a worker is equivalent in effect to
1488 dl 1.11 * ForkJoinTask.helpQuiesce()
1489 jsr166 1.12 */
1490 jsr166 1.18 public void testAwaitQuiescence1() throws Exception {
1491 dl 1.11 final ForkJoinPool p = new ForkJoinPool();
1492 jsr166 1.31 try (PoolCleaner cleaner = cleaner(p)) {
1493 dl 1.11 final long startTime = System.nanoTime();
1494     assertTrue(p.isQuiescent());
1495     ForkJoinTask a = new CheckedRecursiveAction() {
1496 jsr166 1.16 protected void realCompute() {
1497     FibAction f = new FibAction(8);
1498     assertSame(f, f.fork());
1499 jsr166 1.22 assertSame(p, ForkJoinTask.getPool());
1500     boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
1501 jsr166 1.19 assertTrue(quiescent);
1502 jsr166 1.22 assertFalse(p.isQuiescent());
1503 jsr166 1.16 while (!f.isDone()) {
1504     assertFalse(p.getAsyncMode());
1505     assertFalse(p.isShutdown());
1506     assertFalse(p.isTerminating());
1507     assertFalse(p.isTerminated());
1508     Thread.yield();
1509     }
1510 jsr166 1.23 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1511 jsr166 1.16 assertFalse(p.isQuiescent());
1512     assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1513 jsr166 1.20 assertEquals(21, f.result);
1514 jsr166 1.16 }};
1515 dl 1.11 p.execute(a);
1516     while (!a.isDone() || !p.isQuiescent()) {
1517     assertFalse(p.getAsyncMode());
1518     assertFalse(p.isShutdown());
1519     assertFalse(p.isTerminating());
1520     assertFalse(p.isTerminated());
1521     Thread.yield();
1522     }
1523     assertEquals(0, p.getQueuedTaskCount());
1524     assertFalse(p.getAsyncMode());
1525     assertEquals(0, p.getQueuedSubmissionCount());
1526     assertFalse(p.hasQueuedSubmissions());
1527 jsr166 1.32 while (p.getActiveThreadCount() != 0
1528     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1529     Thread.yield();
1530 dl 1.11 assertFalse(p.isShutdown());
1531     assertFalse(p.isTerminating());
1532     assertFalse(p.isTerminated());
1533 jsr166 1.32 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1534 dl 1.11 }
1535     }
1536 jsr166 1.12
1537 dl 1.11 /**
1538 jsr166 1.18 * awaitQuiescence returns when pool isQuiescent() or the indicated
1539     * timeout elapsed
1540 jsr166 1.12 */
1541 jsr166 1.18 public void testAwaitQuiescence2() throws Exception {
1542 jsr166 1.27 /**
1543     * """It is possible to disable or limit the use of threads in the
1544     * common pool by setting the parallelism property to zero. However
1545     * doing so may cause unjoined tasks to never be executed."""
1546     */
1547     if ("0".equals(System.getProperty(
1548     "java.util.concurrent.ForkJoinPool.common.parallelism")))
1549     return;
1550 dl 1.11 final ForkJoinPool p = new ForkJoinPool();
1551 jsr166 1.31 try (PoolCleaner cleaner = cleaner(p)) {
1552 dl 1.11 assertTrue(p.isQuiescent());
1553 jsr166 1.26 final long startTime = System.nanoTime();
1554     ForkJoinTask a = new CheckedRecursiveAction() {
1555     protected void realCompute() {
1556     FibAction f = new FibAction(8);
1557     assertSame(f, f.fork());
1558     while (!f.isDone()
1559     && millisElapsedSince(startTime) < LONG_DELAY_MS) {
1560     assertFalse(p.getAsyncMode());
1561     assertFalse(p.isShutdown());
1562     assertFalse(p.isTerminating());
1563     assertFalse(p.isTerminated());
1564     Thread.yield();
1565     }
1566     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1567     assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1568     assertEquals(21, f.result);
1569     }};
1570     p.execute(a);
1571     assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1572     assertTrue(p.isQuiescent());
1573     assertTrue(a.isDone());
1574     assertEquals(0, p.getQueuedTaskCount());
1575     assertFalse(p.getAsyncMode());
1576     assertEquals(0, p.getQueuedSubmissionCount());
1577     assertFalse(p.hasQueuedSubmissions());
1578 jsr166 1.32 while (p.getActiveThreadCount() != 0
1579     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1580     Thread.yield();
1581 jsr166 1.26 assertFalse(p.isShutdown());
1582     assertFalse(p.isTerminating());
1583     assertFalse(p.isTerminated());
1584 jsr166 1.32 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1585 dl 1.11 }
1586     }
1587    
1588 jsr166 1.1 }