ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
Revision: 1.39
Committed: Mon Jan 8 03:56:32 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +1 -1 lines
Log Message:
fix "dangling" javadoc comments

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