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

# Content
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 import static java.util.concurrent.TimeUnit.MILLISECONDS;
8
9 import java.util.HashSet;
10 import java.util.concurrent.CancellationException;
11 import java.util.concurrent.CountedCompleter;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.ForkJoinPool;
14 import java.util.concurrent.ForkJoinTask;
15 import java.util.concurrent.RecursiveAction;
16 import java.util.concurrent.TimeoutException;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 public class ForkJoinPool8Test extends JSR166TestCase {
22 public static void main(String[] args) {
23 main(suite(), args);
24 }
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
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 */
61
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 a.get(randomTimeout(), randomTimeUnit());
87 shouldThrow();
88 } catch (InterruptedException success) {
89 } catch (Throwable fail) { threadUnexpectedException(fail); }
90 }
91
92 try {
93 a.get(randomExpiredTimeout(), randomTimeUnit());
94 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 assertNull(a.get(randomTimeout(), randomTimeUnit()));
112 } catch (Throwable fail) { threadUnexpectedException(fail); }
113 }
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 a.get(randomTimeout(), randomTimeUnit());
137 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 a.get(randomTimeout(), randomTimeUnit());
168 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 /** A simple recursive action for testing. */
180 final class FibAction extends CheckedRecursiveAction {
181 final int number;
182 int result;
183 FibAction(int n) { number = n; }
184 protected void realCompute() {
185 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 /** A recursive action failing in base case. */
198 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 protected void realCompute() {
223 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 protected void realCompute() {
239 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 protected void realCompute() {
253 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 protected void realCompute() {
268 FibAction f = new FibAction(8);
269 final Thread currentThread = Thread.currentThread();
270
271 // test join()
272 assertSame(f, f.fork());
273 currentThread.interrupt();
274 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 currentThread.interrupt();
283 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 currentThread.interrupt();
295 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 currentThread.interrupt();
307 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 currentThread.interrupt();
316 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 currentThread.interrupt();
324 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 protected void realCompute() throws Exception {
339 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 protected void realCompute() throws Exception {
354 FibAction f = new FibAction(8);
355 assertSame(f, f.fork());
356 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
357 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 protected void realCompute() throws Exception {
369 FibAction f = new FibAction(8);
370 assertSame(f, f.fork());
371 try {
372 f.get(randomTimeout(), null);
373 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 protected void realCompute() {
385 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 protected void realCompute() {
400 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 protected void realCompute() {
417 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 protected void realCompute() {
431 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 protected void realCompute() throws Exception {
449 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 protected void realCompute() throws Exception {
469 FailingFibAction f = new FailingFibAction(8);
470 assertSame(f, f.fork());
471 try {
472 f.get(LONG_DELAY_MS, MILLISECONDS);
473 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 protected void realCompute() {
489 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 protected void realCompute() {
504 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 protected void realCompute() {
522 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 protected void realCompute() throws Exception {
541 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 protected void realCompute() throws Exception {
560 FibAction f = new FibAction(8);
561 assertTrue(f.cancel(true));
562 assertSame(f, f.fork());
563 try {
564 f.get(LONG_DELAY_MS, MILLISECONDS);
565 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 protected void realCompute() {
579 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 protected void realCompute() {
594 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 protected void realCompute() {
605 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 protected void realCompute() {
625 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 protected void realCompute() {
648 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 protected void realCompute() {
666 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 protected void realCompute() {
681 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 protected void realCompute() {
698 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 protected void realCompute() {
712 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 protected void realCompute() {
735 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 protected void realCompute() {
762 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 protected void realCompute() {
779 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 protected void realCompute() {
797 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 protected void realCompute() {
814 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 protected void realCompute() {
833 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 abstract static class CCF extends CountedCompleter {
853 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 else
874 f.quietlyComplete();
875 }
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 /** Version of CCF with forced failure in left completions. */
906 abstract static class FailingCCF extends CountedCompleter {
907 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 else
928 f.quietlyComplete();
929 }
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
954 /**
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 ForkJoinTask a = new CheckedRecursiveAction() {
961 protected void realCompute() {
962 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 ForkJoinTask a = new CheckedRecursiveAction() {
977 protected void realCompute() {
978 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 ForkJoinTask a = new CheckedRecursiveAction() {
991 protected void realCompute() {
992 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 ForkJoinTask a = new CheckedRecursiveAction() {
1006 protected void realCompute() throws Exception {
1007 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 ForkJoinTask a = new CheckedRecursiveAction() {
1021 protected void realCompute() throws Exception {
1022 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 ForkJoinTask a = new CheckedRecursiveAction() {
1036 protected void realCompute() throws Exception {
1037 CCF f = new LCCF(null, 8);
1038 assertSame(f, f.fork());
1039 try {
1040 f.get(randomTimeout(), null);
1041 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 ForkJoinTask a = new CheckedRecursiveAction() {
1052 protected void realCompute() {
1053 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 ForkJoinTask a = new CheckedRecursiveAction() {
1067 protected void realCompute() {
1068 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 ForkJoinTask a = new CheckedRecursiveAction() {
1084 protected void realCompute() {
1085 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 ForkJoinTask a = new CheckedRecursiveAction() {
1098 protected void realCompute() {
1099 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 ForkJoinTask a = new CheckedRecursiveAction() {
1116 protected void realCompute() throws Exception {
1117 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 ForkJoinTask a = new CheckedRecursiveAction() {
1136 protected void realCompute() throws Exception {
1137 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 ForkJoinTask a = new CheckedRecursiveAction() {
1156 protected void realCompute() {
1157 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 ForkJoinTask a = new CheckedRecursiveAction() {
1171 protected void realCompute() {
1172 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 ForkJoinTask a = new CheckedRecursiveAction() {
1189 protected void realCompute() {
1190 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 ForkJoinTask a = new CheckedRecursiveAction() {
1208 protected void realCompute() throws Exception {
1209 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 ForkJoinTask a = new CheckedRecursiveAction() {
1227 protected void realCompute() throws Exception {
1228 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 ForkJoinTask a = new CheckedRecursiveAction() {
1246 protected void realCompute() {
1247 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 ForkJoinTask a = new CheckedRecursiveAction() {
1261 protected void realCompute() {
1262 assertNull(getPool());
1263 }};
1264 assertNull(a.invoke());
1265 }
1266
1267 /**
1268 * inForkJoinPool of non-FJ task returns false
1269 */
1270 public void testInForkJoinPool2CC() {
1271 ForkJoinTask a = new CheckedRecursiveAction() {
1272 protected void realCompute() {
1273 assertFalse(inForkJoinPool());
1274 }};
1275 assertNull(a.invoke());
1276 }
1277
1278 /**
1279 * setRawResult(null) succeeds
1280 */
1281 public void testSetRawResultCC() {
1282 ForkJoinTask a = new CheckedRecursiveAction() {
1283 protected void realCompute() {
1284 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 ForkJoinTask a = new CheckedRecursiveAction() {
1295 protected void realCompute() {
1296 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 ForkJoinTask a = new CheckedRecursiveAction() {
1313 protected void realCompute() {
1314 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 ForkJoinTask a = new CheckedRecursiveAction() {
1330 protected void realCompute() {
1331 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 ForkJoinTask a = new CheckedRecursiveAction() {
1344 protected void realCompute() {
1345 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 ForkJoinTask a = new CheckedRecursiveAction() {
1364 protected void realCompute() {
1365 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 ForkJoinTask a = new CheckedRecursiveAction() {
1388 protected void realCompute() {
1389 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 ForkJoinTask a = new CheckedRecursiveAction() {
1405 protected void realCompute() {
1406 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 ForkJoinTask a = new CheckedRecursiveAction() {
1423 protected void realCompute() {
1424 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 ForkJoinTask a = new CheckedRecursiveAction() {
1440 protected void realCompute() {
1441 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 * invokeAll(collection) throws exception if any task does
1456 */
1457 public void testAbnormalInvokeAllCollectionCC() {
1458 ForkJoinTask a = new CheckedRecursiveAction() {
1459 protected void realCompute() {
1460 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 /**
1478 * awaitQuiescence by a worker is equivalent in effect to
1479 * ForkJoinTask.helpQuiesce()
1480 */
1481 public void testAwaitQuiescence1() throws Exception {
1482 final ForkJoinPool p = new ForkJoinPool();
1483 try (PoolCleaner cleaner = cleaner(p)) {
1484 final long startTime = System.nanoTime();
1485 assertTrue(p.isQuiescent());
1486 ForkJoinTask a = new CheckedRecursiveAction() {
1487 protected void realCompute() {
1488 FibAction f = new FibAction(8);
1489 assertSame(f, f.fork());
1490 assertSame(p, ForkJoinTask.getPool());
1491 boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
1492 assertTrue(quiescent);
1493 assertFalse(p.isQuiescent());
1494 while (!f.isDone()) {
1495 assertFalse(p.getAsyncMode());
1496 assertFalse(p.isShutdown());
1497 assertFalse(p.isTerminating());
1498 assertFalse(p.isTerminated());
1499 Thread.yield();
1500 }
1501 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1502 assertFalse(p.isQuiescent());
1503 assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1504 assertEquals(21, f.result);
1505 }};
1506 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 while (p.getActiveThreadCount() != 0
1519 && millisElapsedSince(startTime) < LONG_DELAY_MS)
1520 Thread.yield();
1521 assertFalse(p.isShutdown());
1522 assertFalse(p.isTerminating());
1523 assertFalse(p.isTerminated());
1524 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1525 }
1526 }
1527
1528 /**
1529 * awaitQuiescence returns when pool isQuiescent() or the indicated
1530 * timeout elapsed
1531 */
1532 public void testAwaitQuiescence2() throws Exception {
1533 /*
1534 * """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 final ForkJoinPool p = new ForkJoinPool();
1542 try (PoolCleaner cleaner = cleaner(p)) {
1543 assertTrue(p.isQuiescent());
1544 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 while (p.getActiveThreadCount() != 0
1570 && millisElapsedSince(startTime) < LONG_DELAY_MS)
1571 Thread.yield();
1572 assertFalse(p.isShutdown());
1573 assertFalse(p.isTerminating());
1574 assertFalse(p.isTerminated());
1575 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1576 }
1577 }
1578
1579 }