ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTask8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinTask8Test.java (file contents):
Revision 1.9 by jsr166, Sat Feb 7 17:43:38 2015 UTC vs.
Revision 1.28 by jsr166, Sat Mar 18 20:42:20 2017 UTC

# Line 7 | Line 7
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8   import static java.util.concurrent.TimeUnit.SECONDS;
9  
10 < import java.util.HashSet;
10 > import java.util.Arrays;
11 > import java.util.concurrent.CountDownLatch;
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
15 + import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
17   import java.util.concurrent.TimeoutException;
18  
# Line 34 | Line 36 | public class ForkJoinTask8Test extends J
36      static final short EXCEPTION_STATE = 1;
37  
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41  
42      public static Test suite() {
# Line 59 | Line 61 | public class ForkJoinTask8Test extends J
61                                  null, true);
62      }
63  
64 +    // Compute fib naively and efficiently
65 +    final int[] fib;
66 +    {
67 +        int[] fib = new int[10];
68 +        fib[0] = 0;
69 +        fib[1] = 1;
70 +        for (int i = 2; i < fib.length; i++)
71 +            fib[i] = fib[i - 1] + fib[i - 2];
72 +        this.fib = fib;
73 +    }
74 +
75      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
76 <        try {
76 >        try (PoolCleaner cleaner = cleaner(pool)) {
77              assertFalse(a.isDone());
78              assertFalse(a.isCompletedNormally());
79              assertFalse(a.isCompletedAbnormally());
# Line 76 | Line 89 | public class ForkJoinTask8Test extends J
89              assertFalse(a.isCancelled());
90              assertNull(a.getException());
91              assertNull(a.getRawResult());
79        } finally {
80            joinPool(pool);
92          }
93      }
94  
# Line 89 | Line 100 | public class ForkJoinTask8Test extends J
100          assertNull(a.getException());
101          assertNull(a.getRawResult());
102          if (a instanceof BinaryAsyncAction)
103 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE);
103 >            assertEquals(INITIAL_STATE,
104 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
105  
106          try {
107              a.get(0L, SECONDS);
# Line 110 | Line 122 | public class ForkJoinTask8Test extends J
122          assertNull(a.getException());
123          assertSame(expected, a.getRawResult());
124          if (a instanceof BinaryAsyncAction)
125 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE);
125 >            assertEquals(COMPLETE_STATE,
126 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
127  
128          {
129              Thread.currentThread().interrupt();
130 <            long t0 = System.nanoTime();
130 >            long startTime = System.nanoTime();
131              assertSame(expected, a.join());
132 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
132 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
133              Thread.interrupted();
134          }
135  
136          {
137              Thread.currentThread().interrupt();
138 <            long t0 = System.nanoTime();
138 >            long startTime = System.nanoTime();
139              a.quietlyJoin();        // should be no-op
140 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
140 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
141              Thread.interrupted();
142          }
143  
# Line 160 | Line 173 | public class ForkJoinTask8Test extends J
173          Thread.interrupted();
174  
175          {
176 <            long t0 = System.nanoTime();
176 >            long startTime = System.nanoTime();
177              a.quietlyJoin();        // should be no-op
178 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
178 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
179          }
180  
181          try {
# Line 186 | Line 199 | public class ForkJoinTask8Test extends J
199  
200      abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
201  
202 <        private BinaryAsyncAction parent;
202 >        private volatile BinaryAsyncAction parent;
203  
204 <        private BinaryAsyncAction sibling;
204 >        private volatile BinaryAsyncAction sibling;
205  
206          protected BinaryAsyncAction() {
207              setForkJoinTaskTag(INITIAL_STATE);
# Line 231 | Line 244 | public class ForkJoinTask8Test extends J
244              super.completeExceptionally(ex);
245          }
246  
247 +        public boolean cancel(boolean mayInterruptIfRunning) {
248 +            if (super.cancel(mayInterruptIfRunning)) {
249 +                completeExceptionally(new FJException());
250 +                return true;
251 +            }
252 +            return false;
253 +        }
254 +
255          public final void complete() {
256              BinaryAsyncAction a = this;
257              for (;;) {
# Line 253 | Line 274 | public class ForkJoinTask8Test extends J
274          }
275  
276          public final void completeExceptionally(Throwable ex) {
277 <            BinaryAsyncAction a = this;
257 <            while (!a.isCompletedAbnormally()) {
277 >            for (BinaryAsyncAction a = this;;) {
278                  a.completeThisExceptionally(ex);
279                  BinaryAsyncAction s = a.sibling;
280 <                if (s != null)
281 <                    s.cancel(false);
282 <                if (!a.onException() || (a = a.parent) == null)
280 >                if (s != null && !s.isDone())
281 >                    s.completeExceptionally(ex);
282 >                if ((a = a.parent) == null)
283                      break;
284              }
285          }
# Line 279 | Line 299 | public class ForkJoinTask8Test extends J
299  
300      }
301  
302 <    static final class AsyncFib extends BinaryAsyncAction {
302 >    final class AsyncFib extends BinaryAsyncAction {
303          int number;
304 <        public AsyncFib(int n) {
305 <            this.number = n;
304 >        int expectedResult;
305 >        public AsyncFib(int number) {
306 >            this.number = number;
307 >            this.expectedResult = fib[number];
308          }
309  
310          public final boolean exec() {
311              try {
312                  AsyncFib f = this;
313                  int n = f.number;
314 <                if (n > 1) {
315 <                    while (n > 1) {
316 <                        AsyncFib p = f;
317 <                        AsyncFib r = new AsyncFib(n - 2);
318 <                        f = new AsyncFib(--n);
319 <                        p.linkSubtasks(r, f);
298 <                        r.fork();
299 <                    }
300 <                    f.number = n;
314 >                while (n > 1) {
315 >                    AsyncFib p = f;
316 >                    AsyncFib r = new AsyncFib(n - 2);
317 >                    f = new AsyncFib(--n);
318 >                    p.linkSubtasks(r, f);
319 >                    r.fork();
320                  }
321                  f.complete();
322              }
323              catch (Throwable ex) {
324                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
325              }
326 +            if (getForkJoinTaskTag() == EXCEPTION_STATE)
327 +                throw new FJException();
328              return false;
329          }
330  
# Line 311 | Line 332 | public class ForkJoinTask8Test extends J
332              number = ((AsyncFib)x).number + ((AsyncFib)y).number;
333              super.onComplete(x, y);
334          }
335 +
336 +        public void checkCompletedNormally() {
337 +            assertEquals(expectedResult, number);
338 +            ForkJoinTask8Test.this.checkCompletedNormally(this);
339 +        }
340      }
341  
342      static final class FailingAsyncFib extends BinaryAsyncAction {
# Line 320 | Line 346 | public class ForkJoinTask8Test extends J
346          }
347  
348          public final boolean exec() {
349 <            FailingAsyncFib f = this;
350 <            int n = f.number;
351 <            if (n > 1) {
349 >            try {
350 >                FailingAsyncFib f = this;
351 >                int n = f.number;
352                  while (n > 1) {
353                      FailingAsyncFib p = f;
354                      FailingAsyncFib r = new FailingAsyncFib(n - 2);
# Line 330 | Line 356 | public class ForkJoinTask8Test extends J
356                      p.linkSubtasks(r, f);
357                      r.fork();
358                  }
359 <                f.number = n;
359 >                f.complete();
360 >            }
361 >            catch (Throwable ex) {
362 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
363              }
364 <            f.complete();
364 >            if (getForkJoinTaskTag() == EXCEPTION_STATE)
365 >                throw new FJException();
366              return false;
367          }
368  
# Line 347 | Line 377 | public class ForkJoinTask8Test extends J
377       * completed tasks; getRawResult returns null.
378       */
379      public void testInvoke() {
380 +        testInvoke(mainPool());
381 +    }
382 +    public void testInvoke_Singleton() {
383 +        testInvoke(singletonPool());
384 +    }
385 +    public void testInvoke(ForkJoinPool pool) {
386          RecursiveAction a = new CheckedRecursiveAction() {
387              protected void realCompute() {
388                  AsyncFib f = new AsyncFib(8);
389                  assertNull(f.invoke());
390 <                assertEquals(21, f.number);
355 <                checkCompletedNormally(f);
390 >                f.checkCompletedNormally();
391              }};
392 <        testInvokeOnPool(mainPool(), a);
392 >        testInvokeOnPool(pool, a);
393      }
394  
395      /**
# Line 363 | Line 398 | public class ForkJoinTask8Test extends J
398       * completed tasks
399       */
400      public void testQuietlyInvoke() {
401 +        testQuietlyInvoke(mainPool());
402 +    }
403 +    public void testQuietlyInvoke_Singleton() {
404 +        testQuietlyInvoke(singletonPool());
405 +    }
406 +    public void testQuietlyInvoke(ForkJoinPool pool) {
407          RecursiveAction a = new CheckedRecursiveAction() {
408              protected void realCompute() {
409                  AsyncFib f = new AsyncFib(8);
410                  f.quietlyInvoke();
411 <                assertEquals(21, f.number);
371 <                checkCompletedNormally(f);
411 >                f.checkCompletedNormally();
412              }};
413 <        testInvokeOnPool(mainPool(), a);
413 >        testInvokeOnPool(pool, a);
414      }
415  
416      /**
417       * join of a forked task returns when task completes
418       */
419      public void testForkJoin() {
420 +        testForkJoin(mainPool());
421 +    }
422 +    public void testForkJoin_Singleton() {
423 +        testForkJoin(singletonPool());
424 +    }
425 +    public void testForkJoin(ForkJoinPool pool) {
426          RecursiveAction a = new CheckedRecursiveAction() {
427              protected void realCompute() {
428                  AsyncFib f = new AsyncFib(8);
429                  assertSame(f, f.fork());
430                  assertNull(f.join());
431 <                assertEquals(21, f.number);
386 <                checkCompletedNormally(f);
431 >                f.checkCompletedNormally();
432              }};
433 <        testInvokeOnPool(mainPool(), a);
433 >        testInvokeOnPool(pool, a);
434      }
435  
436      /**
437       * get of a forked task returns when task completes
438       */
439      public void testForkGet() {
440 +        testForkGet(mainPool());
441 +    }
442 +    public void testForkGet_Singleton() {
443 +        testForkGet(singletonPool());
444 +    }
445 +    public void testForkGet(ForkJoinPool pool) {
446          RecursiveAction a = new CheckedRecursiveAction() {
447              protected void realCompute() throws Exception {
448                  AsyncFib f = new AsyncFib(8);
449                  assertSame(f, f.fork());
450                  assertNull(f.get());
451 <                assertEquals(21, f.number);
401 <                checkCompletedNormally(f);
451 >                f.checkCompletedNormally();
452              }};
453 <        testInvokeOnPool(mainPool(), a);
453 >        testInvokeOnPool(pool, a);
454      }
455  
456      /**
457       * timed get of a forked task returns when task completes
458       */
459      public void testForkTimedGet() {
460 +        testForkTimedGet(mainPool());
461 +    }
462 +    public void testForkTimedGet_Singleton() {
463 +        testForkTimedGet(singletonPool());
464 +    }
465 +    public void testForkTimedGet(ForkJoinPool pool) {
466          RecursiveAction a = new CheckedRecursiveAction() {
467              protected void realCompute() throws Exception {
468                  AsyncFib f = new AsyncFib(8);
469                  assertSame(f, f.fork());
470                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
471 <                assertEquals(21, f.number);
416 <                checkCompletedNormally(f);
471 >                f.checkCompletedNormally();
472              }};
473 <        testInvokeOnPool(mainPool(), a);
473 >        testInvokeOnPool(pool, a);
474      }
475  
476      /**
477 <     * timed get with null time unit throws NPE
477 >     * timed get with null time unit throws NullPointerException
478       */
479 <    public void testForkTimedGetNPE() {
479 >    public void testForkTimedGetNullTimeUnit() {
480 >        testForkTimedGetNullTimeUnit(mainPool());
481 >    }
482 >    public void testForkTimedGetNullTimeUnit_Singleton() {
483 >        testForkTimedGet(singletonPool());
484 >    }
485 >    public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
486          RecursiveAction a = new CheckedRecursiveAction() {
487              protected void realCompute() throws Exception {
488                  AsyncFib f = new AsyncFib(8);
# Line 431 | Line 492 | public class ForkJoinTask8Test extends J
492                      shouldThrow();
493                  } catch (NullPointerException success) {}
494              }};
495 <        testInvokeOnPool(mainPool(), a);
495 >        testInvokeOnPool(pool, a);
496      }
497  
498      /**
499       * quietlyJoin of a forked task returns when task completes
500       */
501      public void testForkQuietlyJoin() {
502 +        testForkQuietlyJoin(mainPool());
503 +    }
504 +    public void testForkQuietlyJoin_Singleton() {
505 +        testForkQuietlyJoin(singletonPool());
506 +    }
507 +    public void testForkQuietlyJoin(ForkJoinPool pool) {
508          RecursiveAction a = new CheckedRecursiveAction() {
509              protected void realCompute() {
510                  AsyncFib f = new AsyncFib(8);
511                  assertSame(f, f.fork());
512                  f.quietlyJoin();
513 <                assertEquals(21, f.number);
447 <                checkCompletedNormally(f);
513 >                f.checkCompletedNormally();
514              }};
515 <        testInvokeOnPool(mainPool(), a);
515 >        testInvokeOnPool(pool, a);
516      }
517  
518      /**
# Line 454 | Line 520 | public class ForkJoinTask8Test extends J
520       * getQueuedTaskCount returns 0 when quiescent
521       */
522      public void testForkHelpQuiesce() {
523 +        testForkHelpQuiesce(mainPool());
524 +    }
525 +    public void testForkHelpQuiesce_Singleton() {
526 +        testForkHelpQuiesce(singletonPool());
527 +    }
528 +    public void testForkHelpQuiesce(ForkJoinPool pool) {
529          RecursiveAction a = new CheckedRecursiveAction() {
530              protected void realCompute() {
531                  AsyncFib f = new AsyncFib(8);
532                  assertSame(f, f.fork());
533                  helpQuiesce();
462                assertEquals(21, f.number);
534                  assertEquals(0, getQueuedTaskCount());
535 <                checkCompletedNormally(f);
535 >                f.checkCompletedNormally();
536              }};
537 <        testInvokeOnPool(mainPool(), a);
537 >        testInvokeOnPool(pool, a);
538      }
539  
540      /**
541       * invoke task throws exception when task completes abnormally
542       */
543      public void testAbnormalInvoke() {
544 +        testAbnormalInvoke(mainPool());
545 +    }
546 +    public void testAbnormalInvoke_Singleton() {
547 +        testAbnormalInvoke(singletonPool());
548 +    }
549 +    public void testAbnormalInvoke(ForkJoinPool pool) {
550          RecursiveAction a = new CheckedRecursiveAction() {
551              protected void realCompute() {
552                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 480 | Line 557 | public class ForkJoinTask8Test extends J
557                      checkCompletedAbnormally(f, success);
558                  }
559              }};
560 <        testInvokeOnPool(mainPool(), a);
560 >        testInvokeOnPool(pool, a);
561      }
562  
563      /**
564       * quietlyInvoke task returns when task completes abnormally
565       */
566      public void testAbnormalQuietlyInvoke() {
567 +        testAbnormalQuietlyInvoke(mainPool());
568 +    }
569 +    public void testAbnormalQuietlyInvoke_Singleton() {
570 +        testAbnormalQuietlyInvoke(singletonPool());
571 +    }
572 +    public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
573          RecursiveAction a = new CheckedRecursiveAction() {
574              protected void realCompute() {
575                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 494 | Line 577 | public class ForkJoinTask8Test extends J
577                  assertTrue(f.getException() instanceof FJException);
578                  checkCompletedAbnormally(f, f.getException());
579              }};
580 <        testInvokeOnPool(mainPool(), a);
580 >        testInvokeOnPool(pool, a);
581      }
582  
583      /**
584       * join of a forked task throws exception when task completes abnormally
585       */
586      public void testAbnormalForkJoin() {
587 +        testAbnormalForkJoin(mainPool());
588 +    }
589 +    public void testAbnormalForkJoin_Singleton() {
590 +        testAbnormalForkJoin(singletonPool());
591 +    }
592 +    public void testAbnormalForkJoin(ForkJoinPool pool) {
593          RecursiveAction a = new CheckedRecursiveAction() {
594              protected void realCompute() {
595                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 512 | Line 601 | public class ForkJoinTask8Test extends J
601                      checkCompletedAbnormally(f, success);
602                  }
603              }};
604 <        testInvokeOnPool(mainPool(), a);
604 >        testInvokeOnPool(pool, a);
605      }
606  
607      /**
608       * get of a forked task throws exception when task completes abnormally
609       */
610      public void testAbnormalForkGet() {
611 +        testAbnormalForkGet(mainPool());
612 +    }
613 +    public void testAbnormalForkGet_Singleton() {
614 +        testAbnormalForkJoin(singletonPool());
615 +    }
616 +    public void testAbnormalForkGet(ForkJoinPool pool) {
617          RecursiveAction a = new CheckedRecursiveAction() {
618              protected void realCompute() throws Exception {
619                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 532 | Line 627 | public class ForkJoinTask8Test extends J
627                      checkCompletedAbnormally(f, cause);
628                  }
629              }};
630 <        testInvokeOnPool(mainPool(), a);
630 >        testInvokeOnPool(pool, a);
631      }
632  
633      /**
634       * timed get of a forked task throws exception when task completes abnormally
635       */
636      public void testAbnormalForkTimedGet() {
637 +        testAbnormalForkTimedGet(mainPool());
638 +    }
639 +    public void testAbnormalForkTimedGet_Singleton() {
640 +        testAbnormalForkTimedGet(singletonPool());
641 +    }
642 +    public void testAbnormalForkTimedGet(ForkJoinPool pool) {
643          RecursiveAction a = new CheckedRecursiveAction() {
644              protected void realCompute() throws Exception {
645                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 552 | Line 653 | public class ForkJoinTask8Test extends J
653                      checkCompletedAbnormally(f, cause);
654                  }
655              }};
656 <        testInvokeOnPool(mainPool(), a);
656 >        testInvokeOnPool(pool, a);
657      }
658  
659      /**
660       * quietlyJoin of a forked task returns when task completes abnormally
661       */
662      public void testAbnormalForkQuietlyJoin() {
663 +        testAbnormalForkQuietlyJoin(mainPool());
664 +    }
665 +    public void testAbnormalForkQuietlyJoin_Singleton() {
666 +        testAbnormalForkQuietlyJoin(singletonPool());
667 +    }
668 +    public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
669          RecursiveAction a = new CheckedRecursiveAction() {
670              protected void realCompute() {
671                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 567 | Line 674 | public class ForkJoinTask8Test extends J
674                  assertTrue(f.getException() instanceof FJException);
675                  checkCompletedAbnormally(f, f.getException());
676              }};
677 <        testInvokeOnPool(mainPool(), a);
677 >        testInvokeOnPool(pool, a);
678      }
679  
680      /**
681       * getPool of executing task returns its pool
682       */
683      public void testGetPool() {
684 <        final ForkJoinPool mainPool = mainPool();
684 >        testGetPool(mainPool());
685 >    }
686 >    public void testGetPool_Singleton() {
687 >        testGetPool(singletonPool());
688 >    }
689 >    public void testGetPool(ForkJoinPool pool) {
690          RecursiveAction a = new CheckedRecursiveAction() {
691              protected void realCompute() {
692 <                assertSame(mainPool, getPool());
692 >                assertSame(pool, getPool());
693              }};
694 <        testInvokeOnPool(mainPool, a);
694 >        testInvokeOnPool(pool, a);
695      }
696  
697      /**
# Line 597 | Line 709 | public class ForkJoinTask8Test extends J
709       * inForkJoinPool of executing task returns true
710       */
711      public void testInForkJoinPool() {
712 +        testInForkJoinPool(mainPool());
713 +    }
714 +    public void testInForkJoinPool_Singleton() {
715 +        testInForkJoinPool(singletonPool());
716 +    }
717 +    public void testInForkJoinPool(ForkJoinPool pool) {
718          RecursiveAction a = new CheckedRecursiveAction() {
719              protected void realCompute() {
720                  assertTrue(inForkJoinPool());
721              }};
722 <        testInvokeOnPool(mainPool(), a);
722 >        testInvokeOnPool(pool, a);
723      }
724  
725      /**
# Line 631 | Line 749 | public class ForkJoinTask8Test extends J
749       * invoke task throws exception after invoking completeExceptionally
750       */
751      public void testCompleteExceptionally() {
752 +        testCompleteExceptionally(mainPool());
753 +    }
754 +    public void testCompleteExceptionally_Singleton() {
755 +        testCompleteExceptionally(singletonPool());
756 +    }
757 +    public void testCompleteExceptionally(ForkJoinPool pool) {
758          RecursiveAction a = new CheckedRecursiveAction() {
759              protected void realCompute() {
760                  AsyncFib f = new AsyncFib(8);
# Line 642 | Line 766 | public class ForkJoinTask8Test extends J
766                      checkCompletedAbnormally(f, success);
767                  }
768              }};
769 <        testInvokeOnPool(mainPool(), a);
769 >        testInvokeOnPool(pool, a);
770      }
771  
772      /**
773 <     * invokeAll(t1, t2) invokes all task arguments
773 >     * invokeAll(tasks) with 1 argument invokes task
774       */
775 <    public void testInvokeAll2() {
775 >    public void testInvokeAll1() {
776 >        testInvokeAll1(mainPool());
777 >    }
778 >    public void testInvokeAll1_Singleton() {
779 >        testInvokeAll1(singletonPool());
780 >    }
781 >    public void testInvokeAll1(ForkJoinPool pool) {
782          RecursiveAction a = new CheckedRecursiveAction() {
783              protected void realCompute() {
784                  AsyncFib f = new AsyncFib(8);
785 <                AsyncFib g = new AsyncFib(9);
786 <                invokeAll(f, g);
657 <                assertEquals(21, f.number);
658 <                assertEquals(34, g.number);
659 <                checkCompletedNormally(f);
660 <                checkCompletedNormally(g);
785 >                invokeAll(f);
786 >                f.checkCompletedNormally();
787              }};
788 <        testInvokeOnPool(mainPool(), a);
788 >        testInvokeOnPool(pool, a);
789      }
790  
791      /**
792 <     * invokeAll(tasks) with 1 argument invokes task
792 >     * invokeAll(t1, t2) invokes all task arguments
793       */
794 <    public void testInvokeAll1() {
794 >    public void testInvokeAll2() {
795 >        testInvokeAll2(mainPool());
796 >    }
797 >    public void testInvokeAll2_Singleton() {
798 >        testInvokeAll2(singletonPool());
799 >    }
800 >    public void testInvokeAll2(ForkJoinPool pool) {
801          RecursiveAction a = new CheckedRecursiveAction() {
802              protected void realCompute() {
803 <                AsyncFib f = new AsyncFib(8);
804 <                invokeAll(f);
805 <                checkCompletedNormally(f);
806 <                assertEquals(21, f.number);
803 >                AsyncFib[] tasks = {
804 >                    new AsyncFib(8),
805 >                    new AsyncFib(9),
806 >                };
807 >                invokeAll(tasks[0], tasks[1]);
808 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
809 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
810              }};
811 <        testInvokeOnPool(mainPool(), a);
811 >        testInvokeOnPool(pool, a);
812      }
813  
814      /**
815       * invokeAll(tasks) with > 2 argument invokes tasks
816       */
817      public void testInvokeAll3() {
818 +        testInvokeAll3(mainPool());
819 +    }
820 +    public void testInvokeAll3_Singleton() {
821 +        testInvokeAll3(singletonPool());
822 +    }
823 +    public void testInvokeAll3(ForkJoinPool pool) {
824          RecursiveAction a = new CheckedRecursiveAction() {
825              protected void realCompute() {
826 <                AsyncFib f = new AsyncFib(8);
827 <                AsyncFib g = new AsyncFib(9);
828 <                AsyncFib h = new AsyncFib(7);
829 <                invokeAll(f, g, h);
830 <                assertEquals(21, f.number);
831 <                assertEquals(34, g.number);
832 <                assertEquals(13, h.number);
833 <                checkCompletedNormally(f);
693 <                checkCompletedNormally(g);
694 <                checkCompletedNormally(h);
826 >                AsyncFib[] tasks = {
827 >                    new AsyncFib(8),
828 >                    new AsyncFib(9),
829 >                    new AsyncFib(7),
830 >                };
831 >                invokeAll(tasks[0], tasks[1], tasks[2]);
832 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
833 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
834              }};
835 <        testInvokeOnPool(mainPool(), a);
835 >        testInvokeOnPool(pool, a);
836      }
837  
838      /**
839       * invokeAll(collection) invokes all tasks in the collection
840       */
841      public void testInvokeAllCollection() {
842 +        testInvokeAllCollection(mainPool());
843 +    }
844 +    public void testInvokeAllCollection_Singleton() {
845 +        testInvokeAllCollection(singletonPool());
846 +    }
847 +    public void testInvokeAllCollection(ForkJoinPool pool) {
848          RecursiveAction a = new CheckedRecursiveAction() {
849              protected void realCompute() {
850 <                AsyncFib f = new AsyncFib(8);
851 <                AsyncFib g = new AsyncFib(9);
852 <                AsyncFib h = new AsyncFib(7);
853 <                HashSet set = new HashSet();
854 <                set.add(f);
855 <                set.add(g);
856 <                set.add(h);
857 <                invokeAll(set);
713 <                assertEquals(21, f.number);
714 <                assertEquals(34, g.number);
715 <                assertEquals(13, h.number);
716 <                checkCompletedNormally(f);
717 <                checkCompletedNormally(g);
718 <                checkCompletedNormally(h);
850 >                AsyncFib[] tasks = {
851 >                    new AsyncFib(8),
852 >                    new AsyncFib(9),
853 >                    new AsyncFib(7),
854 >                };
855 >                invokeAll(Arrays.asList(tasks));
856 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
857 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
858              }};
859 <        testInvokeOnPool(mainPool(), a);
859 >        testInvokeOnPool(pool, a);
860      }
861  
862      /**
863 <     * invokeAll(tasks) with any null task throws NPE
863 >     * invokeAll(tasks) with any null task throws NullPointerException
864       */
865 <    public void testInvokeAllNPE() {
865 >    public void testInvokeAllNullTask() {
866 >        testInvokeAllNullTask(mainPool());
867 >    }
868 >    public void testInvokeAllNullTask_Singleton() {
869 >        testInvokeAllNullTask(singletonPool());
870 >    }
871 >    public void testInvokeAllNullTask(ForkJoinPool pool) {
872          RecursiveAction a = new CheckedRecursiveAction() {
873              protected void realCompute() {
874 <                AsyncFib f = new AsyncFib(8);
730 <                AsyncFib g = new AsyncFib(9);
731 <                AsyncFib h = null;
874 >                AsyncFib nul = null;
875                  Runnable[] throwingActions = {
876 <                    () -> invokeAll(h),
877 <                    () -> invokeAll(f, g, h),
878 <                    () -> invokeAll(f, h, g),
879 <                    () -> invokeAll(h, f, g),
876 >                    () -> invokeAll(nul),
877 >                    () -> invokeAll(nul, nul),
878 >                    () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
879 >                    () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
880 >                    () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
881                  };
882                  assertThrows(NullPointerException.class, throwingActions);
883              }};
884 <        testInvokeOnPool(mainPool(), a);
884 >        testInvokeOnPool(pool, a);
885      }
886  
887      /**
888 <     * invokeAll(t1, t2) throw exception if any task does
888 >     * invokeAll(tasks) with 1 argument throws exception if task does
889       */
890 <    public void testAbnormalInvokeAll2() {
890 >    public void testAbnormalInvokeAll1() {
891 >        testAbnormalInvokeAll1(mainPool());
892 >    }
893 >    public void testAbnormalInvokeAll1_Singleton() {
894 >        testAbnormalInvokeAll1(singletonPool());
895 >    }
896 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
897          RecursiveAction a = new CheckedRecursiveAction() {
898              protected void realCompute() {
749                AsyncFib f = new AsyncFib(8);
899                  FailingAsyncFib g = new FailingAsyncFib(9);
900                  try {
901 <                    invokeAll(f, g);
901 >                    invokeAll(g);
902                      shouldThrow();
903                  } catch (FJException success) {
904                      checkCompletedAbnormally(g, success);
905                  }
906              }};
907 <        testInvokeOnPool(mainPool(), a);
907 >        testInvokeOnPool(pool, a);
908      }
909  
910      /**
911 <     * invokeAll(tasks) with 1 argument throws exception if task does
911 >     * invokeAll(t1, t2) throw exception if any task does
912       */
913 <    public void testAbnormalInvokeAll1() {
913 >    public void testAbnormalInvokeAll2() {
914 >        testAbnormalInvokeAll2(mainPool());
915 >    }
916 >    public void testAbnormalInvokeAll2_Singleton() {
917 >        testAbnormalInvokeAll2(singletonPool());
918 >    }
919 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
920          RecursiveAction a = new CheckedRecursiveAction() {
921              protected void realCompute() {
922 +                AsyncFib f = new AsyncFib(8);
923                  FailingAsyncFib g = new FailingAsyncFib(9);
924 +                ForkJoinTask[] tasks = { f, g };
925 +                shuffle(tasks);
926                  try {
927 <                    invokeAll(g);
927 >                    invokeAll(tasks[0], tasks[1]);
928                      shouldThrow();
929                  } catch (FJException success) {
930                      checkCompletedAbnormally(g, success);
931                  }
932              }};
933 <        testInvokeOnPool(mainPool(), a);
933 >        testInvokeOnPool(pool, a);
934      }
935  
936      /**
937       * invokeAll(tasks) with > 2 argument throws exception if any task does
938       */
939      public void testAbnormalInvokeAll3() {
940 +        testAbnormalInvokeAll3(mainPool());
941 +    }
942 +    public void testAbnormalInvokeAll3_Singleton() {
943 +        testAbnormalInvokeAll3(singletonPool());
944 +    }
945 +    public void testAbnormalInvokeAll3(ForkJoinPool pool) {
946          RecursiveAction a = new CheckedRecursiveAction() {
947              protected void realCompute() {
948                  AsyncFib f = new AsyncFib(8);
949                  FailingAsyncFib g = new FailingAsyncFib(9);
950                  AsyncFib h = new AsyncFib(7);
951 +                ForkJoinTask[] tasks = { f, g, h };
952 +                shuffle(tasks);
953                  try {
954 <                    invokeAll(f, g, h);
954 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
955                      shouldThrow();
956                  } catch (FJException success) {
957                      checkCompletedAbnormally(g, success);
958                  }
959              }};
960 <        testInvokeOnPool(mainPool(), a);
960 >        testInvokeOnPool(pool, a);
961      }
962  
963      /**
964       * invokeAll(collection) throws exception if any task does
965       */
966      public void testAbnormalInvokeAllCollection() {
967 +        testAbnormalInvokeAllCollection(mainPool());
968 +    }
969 +    public void testAbnormalInvokeAllCollection_Singleton() {
970 +        testAbnormalInvokeAllCollection(singletonPool());
971 +    }
972 +    public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
973          RecursiveAction a = new CheckedRecursiveAction() {
974              protected void realCompute() {
975                  FailingAsyncFib f = new FailingAsyncFib(8);
976                  AsyncFib g = new AsyncFib(9);
977                  AsyncFib h = new AsyncFib(7);
978 <                HashSet set = new HashSet();
979 <                set.add(f);
808 <                set.add(g);
809 <                set.add(h);
978 >                ForkJoinTask[] tasks = { f, g, h };
979 >                shuffle(tasks);
980                  try {
981 <                    invokeAll(set);
981 >                    invokeAll(Arrays.asList(tasks));
982                      shouldThrow();
983                  } catch (FJException success) {
984                      checkCompletedAbnormally(f, success);
985                  }
986              }};
987 <        testInvokeOnPool(mainPool(), a);
987 >        testInvokeOnPool(pool, a);
988      }
989  
990      /**
# Line 831 | Line 1001 | public class ForkJoinTask8Test extends J
1001                  assertTrue(f.tryUnfork());
1002                  helpQuiesce();
1003                  checkNotDone(f);
1004 <                checkCompletedNormally(g);
1004 >                g.checkCompletedNormally();
1005              }};
1006          testInvokeOnPool(singletonPool(), a);
1007      }
# Line 852 | Line 1022 | public class ForkJoinTask8Test extends J
1022                  assertTrue(getSurplusQueuedTaskCount() > 0);
1023                  helpQuiesce();
1024                  assertEquals(0, getSurplusQueuedTaskCount());
1025 <                checkCompletedNormally(f);
1026 <                checkCompletedNormally(g);
1027 <                checkCompletedNormally(h);
1025 >                f.checkCompletedNormally();
1026 >                g.checkCompletedNormally();
1027 >                h.checkCompletedNormally();
1028              }};
1029          testInvokeOnPool(singletonPool(), a);
1030      }
# Line 871 | Line 1041 | public class ForkJoinTask8Test extends J
1041                  assertSame(f, f.fork());
1042                  assertSame(f, peekNextLocalTask());
1043                  assertNull(f.join());
1044 <                checkCompletedNormally(f);
1044 >                f.checkCompletedNormally();
1045                  helpQuiesce();
1046 <                checkCompletedNormally(g);
1046 >                g.checkCompletedNormally();
1047              }};
1048          testInvokeOnPool(singletonPool(), a);
1049      }
# Line 892 | Line 1062 | public class ForkJoinTask8Test extends J
1062                  assertSame(f, pollNextLocalTask());
1063                  helpQuiesce();
1064                  checkNotDone(f);
1065 <                assertEquals(34, g.number);
896 <                checkCompletedNormally(g);
1065 >                g.checkCompletedNormally();
1066              }};
1067          testInvokeOnPool(singletonPool(), a);
1068      }
# Line 911 | Line 1080 | public class ForkJoinTask8Test extends J
1080                  assertSame(f, pollTask());
1081                  helpQuiesce();
1082                  checkNotDone(f);
1083 <                checkCompletedNormally(g);
1083 >                g.checkCompletedNormally();
1084              }};
1085          testInvokeOnPool(singletonPool(), a);
1086      }
# Line 929 | Line 1098 | public class ForkJoinTask8Test extends J
1098                  assertSame(g, peekNextLocalTask());
1099                  assertNull(f.join());
1100                  helpQuiesce();
1101 <                checkCompletedNormally(f);
1102 <                assertEquals(34, g.number);
934 <                checkCompletedNormally(g);
1101 >                f.checkCompletedNormally();
1102 >                g.checkCompletedNormally();
1103              }};
1104          testInvokeOnPool(asyncSingletonPool(), a);
1105      }
# Line 949 | Line 1117 | public class ForkJoinTask8Test extends J
1117                  assertSame(f, f.fork());
1118                  assertSame(g, pollNextLocalTask());
1119                  helpQuiesce();
1120 <                assertEquals(21, f.number);
953 <                checkCompletedNormally(f);
1120 >                f.checkCompletedNormally();
1121                  checkNotDone(g);
1122              }};
1123          testInvokeOnPool(asyncSingletonPool(), a);
# Line 969 | Line 1136 | public class ForkJoinTask8Test extends J
1136                  assertSame(f, f.fork());
1137                  assertSame(g, pollTask());
1138                  helpQuiesce();
1139 <                assertEquals(21, f.number);
973 <                checkCompletedNormally(f);
1139 >                f.checkCompletedNormally();
1140                  checkNotDone(g);
1141              }};
1142          testInvokeOnPool(asyncSingletonPool(), a);
1143      }
1144  
979    // versions for singleton pools
980
981    /**
982     * invoke returns when task completes normally.
983     * isCompletedAbnormally and isCancelled return false for normally
984     * completed tasks; getRawResult returns null.
985     */
986    public void testInvokeSingleton() {
987        RecursiveAction a = new CheckedRecursiveAction() {
988            protected void realCompute() {
989                AsyncFib f = new AsyncFib(8);
990                assertNull(f.invoke());
991                assertEquals(21, f.number);
992                checkCompletedNormally(f);
993            }};
994        testInvokeOnPool(singletonPool(), a);
995    }
996
997    /**
998     * quietlyInvoke task returns when task completes normally.
999     * isCompletedAbnormally and isCancelled return false for normally
1000     * completed tasks
1001     */
1002    public void testQuietlyInvokeSingleton() {
1003        RecursiveAction a = new CheckedRecursiveAction() {
1004            protected void realCompute() {
1005                AsyncFib f = new AsyncFib(8);
1006                f.quietlyInvoke();
1007                assertEquals(21, f.number);
1008                checkCompletedNormally(f);
1009            }};
1010        testInvokeOnPool(singletonPool(), a);
1011    }
1012
1013    /**
1014     * join of a forked task returns when task completes
1015     */
1016    public void testForkJoinSingleton() {
1017        RecursiveAction a = new CheckedRecursiveAction() {
1018            protected void realCompute() {
1019                AsyncFib f = new AsyncFib(8);
1020                assertSame(f, f.fork());
1021                assertNull(f.join());
1022                assertEquals(21, f.number);
1023                checkCompletedNormally(f);
1024            }};
1025        testInvokeOnPool(singletonPool(), a);
1026    }
1027
1028    /**
1029     * get of a forked task returns when task completes
1030     */
1031    public void testForkGetSingleton() {
1032        RecursiveAction a = new CheckedRecursiveAction() {
1033            protected void realCompute() throws Exception {
1034                AsyncFib f = new AsyncFib(8);
1035                assertSame(f, f.fork());
1036                assertNull(f.get());
1037                assertEquals(21, f.number);
1038                checkCompletedNormally(f);
1039            }};
1040        testInvokeOnPool(singletonPool(), a);
1041    }
1042
1043    /**
1044     * timed get of a forked task returns when task completes
1045     */
1046    public void testForkTimedGetSingleton() {
1047        RecursiveAction a = new CheckedRecursiveAction() {
1048            protected void realCompute() throws Exception {
1049                AsyncFib f = new AsyncFib(8);
1050                assertSame(f, f.fork());
1051                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1052                assertEquals(21, f.number);
1053                checkCompletedNormally(f);
1054            }};
1055        testInvokeOnPool(singletonPool(), a);
1056    }
1057
1058    /**
1059     * timed get with null time unit throws NPE
1060     */
1061    public void testForkTimedGetNPESingleton() {
1062        RecursiveAction a = new CheckedRecursiveAction() {
1063            protected void realCompute() throws Exception {
1064                AsyncFib f = new AsyncFib(8);
1065                assertSame(f, f.fork());
1066                try {
1067                    f.get(5L, null);
1068                    shouldThrow();
1069                } catch (NullPointerException success) {}
1070            }};
1071        testInvokeOnPool(singletonPool(), a);
1072    }
1073
1074    /**
1075     * quietlyJoin of a forked task returns when task completes
1076     */
1077    public void testForkQuietlyJoinSingleton() {
1078        RecursiveAction a = new CheckedRecursiveAction() {
1079            protected void realCompute() {
1080                AsyncFib f = new AsyncFib(8);
1081                assertSame(f, f.fork());
1082                f.quietlyJoin();
1083                assertEquals(21, f.number);
1084                checkCompletedNormally(f);
1085            }};
1086        testInvokeOnPool(singletonPool(), a);
1087    }
1088
1089    /**
1090     * helpQuiesce returns when tasks are complete.
1091     * getQueuedTaskCount returns 0 when quiescent
1092     */
1093    public void testForkHelpQuiesceSingleton() {
1094        RecursiveAction a = new CheckedRecursiveAction() {
1095            protected void realCompute() {
1096                AsyncFib f = new AsyncFib(8);
1097                assertSame(f, f.fork());
1098                helpQuiesce();
1099                assertEquals(0, getQueuedTaskCount());
1100                assertEquals(21, f.number);
1101                checkCompletedNormally(f);
1102            }};
1103        testInvokeOnPool(singletonPool(), a);
1104    }
1105
1106    /**
1107     * invoke task throws exception when task completes abnormally
1108     */
1109    public void testAbnormalInvokeSingleton() {
1110        RecursiveAction a = new CheckedRecursiveAction() {
1111            protected void realCompute() {
1112                FailingAsyncFib f = new FailingAsyncFib(8);
1113                try {
1114                    f.invoke();
1115                    shouldThrow();
1116                } catch (FJException success) {
1117                    checkCompletedAbnormally(f, success);
1118                }
1119            }};
1120        testInvokeOnPool(singletonPool(), a);
1121    }
1122
1123    /**
1124     * quietlyInvoke task returns when task completes abnormally
1125     */
1126    public void testAbnormalQuietlyInvokeSingleton() {
1127        RecursiveAction a = new CheckedRecursiveAction() {
1128            protected void realCompute() {
1129                FailingAsyncFib f = new FailingAsyncFib(8);
1130                f.quietlyInvoke();
1131                assertTrue(f.getException() instanceof FJException);
1132                checkCompletedAbnormally(f, f.getException());
1133            }};
1134        testInvokeOnPool(singletonPool(), a);
1135    }
1136
1137    /**
1138     * join of a forked task throws exception when task completes abnormally
1139     */
1140    public void testAbnormalForkJoinSingleton() {
1141        RecursiveAction a = new CheckedRecursiveAction() {
1142            protected void realCompute() {
1143                FailingAsyncFib f = new FailingAsyncFib(8);
1144                assertSame(f, f.fork());
1145                try {
1146                    f.join();
1147                    shouldThrow();
1148                } catch (FJException success) {
1149                    checkCompletedAbnormally(f, success);
1150                }
1151            }};
1152        testInvokeOnPool(singletonPool(), a);
1153    }
1154
1155    /**
1156     * get of a forked task throws exception when task completes abnormally
1157     */
1158    public void testAbnormalForkGetSingleton() {
1159        RecursiveAction a = new CheckedRecursiveAction() {
1160            protected void realCompute() throws Exception {
1161                FailingAsyncFib f = new FailingAsyncFib(8);
1162                assertSame(f, f.fork());
1163                try {
1164                    f.get();
1165                    shouldThrow();
1166                } catch (ExecutionException success) {
1167                    Throwable cause = success.getCause();
1168                    assertTrue(cause instanceof FJException);
1169                    checkCompletedAbnormally(f, cause);
1170                }
1171            }};
1172        testInvokeOnPool(singletonPool(), a);
1173    }
1174
1175    /**
1176     * timed get of a forked task throws exception when task completes abnormally
1177     */
1178    public void testAbnormalForkTimedGetSingleton() {
1179        RecursiveAction a = new CheckedRecursiveAction() {
1180            protected void realCompute() throws Exception {
1181                FailingAsyncFib f = new FailingAsyncFib(8);
1182                assertSame(f, f.fork());
1183                try {
1184                    f.get(LONG_DELAY_MS, MILLISECONDS);
1185                    shouldThrow();
1186                } catch (ExecutionException success) {
1187                    Throwable cause = success.getCause();
1188                    assertTrue(cause instanceof FJException);
1189                    checkCompletedAbnormally(f, cause);
1190                }
1191            }};
1192        testInvokeOnPool(singletonPool(), a);
1193    }
1194
1195    /**
1196     * quietlyJoin of a forked task returns when task completes abnormally
1197     */
1198    public void testAbnormalForkQuietlyJoinSingleton() {
1199        RecursiveAction a = new CheckedRecursiveAction() {
1200            protected void realCompute() {
1201                FailingAsyncFib f = new FailingAsyncFib(8);
1202                assertSame(f, f.fork());
1203                f.quietlyJoin();
1204                assertTrue(f.getException() instanceof FJException);
1205                checkCompletedAbnormally(f, f.getException());
1206            }};
1207        testInvokeOnPool(singletonPool(), a);
1208    }
1209
1210    /**
1211     * invoke task throws exception after invoking completeExceptionally
1212     */
1213    public void testCompleteExceptionallySingleton() {
1214        RecursiveAction a = new CheckedRecursiveAction() {
1215            protected void realCompute() {
1216                AsyncFib f = new AsyncFib(8);
1217                f.completeExceptionally(new FJException());
1218                try {
1219                    f.invoke();
1220                    shouldThrow();
1221                } catch (FJException success) {
1222                    checkCompletedAbnormally(f, success);
1223                }
1224            }};
1225        testInvokeOnPool(singletonPool(), a);
1226    }
1227
1228    /**
1229     * invokeAll(t1, t2) invokes all task arguments
1230     */
1231    public void testInvokeAll2Singleton() {
1232        RecursiveAction a = new CheckedRecursiveAction() {
1233            protected void realCompute() {
1234                AsyncFib f = new AsyncFib(8);
1235                AsyncFib g = new AsyncFib(9);
1236                invokeAll(f, g);
1237                assertEquals(21, f.number);
1238                assertEquals(34, g.number);
1239                checkCompletedNormally(f);
1240                checkCompletedNormally(g);
1241            }};
1242        testInvokeOnPool(singletonPool(), a);
1243    }
1244
1245    /**
1246     * invokeAll(tasks) with 1 argument invokes task
1247     */
1248    public void testInvokeAll1Singleton() {
1249        RecursiveAction a = new CheckedRecursiveAction() {
1250            protected void realCompute() {
1251                AsyncFib f = new AsyncFib(8);
1252                invokeAll(f);
1253                checkCompletedNormally(f);
1254                assertEquals(21, f.number);
1255            }};
1256        testInvokeOnPool(singletonPool(), a);
1257    }
1258
1259    /**
1260     * invokeAll(tasks) with > 2 argument invokes tasks
1261     */
1262    public void testInvokeAll3Singleton() {
1263        RecursiveAction a = new CheckedRecursiveAction() {
1264            protected void realCompute() {
1265                AsyncFib f = new AsyncFib(8);
1266                AsyncFib g = new AsyncFib(9);
1267                AsyncFib h = new AsyncFib(7);
1268                invokeAll(f, g, h);
1269                assertEquals(21, f.number);
1270                assertEquals(34, g.number);
1271                assertEquals(13, h.number);
1272                checkCompletedNormally(f);
1273                checkCompletedNormally(g);
1274                checkCompletedNormally(h);
1275            }};
1276        testInvokeOnPool(singletonPool(), a);
1277    }
1278
1279    /**
1280     * invokeAll(collection) invokes all tasks in the collection
1281     */
1282    public void testInvokeAllCollectionSingleton() {
1283        RecursiveAction a = new CheckedRecursiveAction() {
1284            protected void realCompute() {
1285                AsyncFib f = new AsyncFib(8);
1286                AsyncFib g = new AsyncFib(9);
1287                AsyncFib h = new AsyncFib(7);
1288                HashSet set = new HashSet();
1289                set.add(f);
1290                set.add(g);
1291                set.add(h);
1292                invokeAll(set);
1293                assertEquals(21, f.number);
1294                assertEquals(34, g.number);
1295                assertEquals(13, h.number);
1296                checkCompletedNormally(f);
1297                checkCompletedNormally(g);
1298                checkCompletedNormally(h);
1299            }};
1300        testInvokeOnPool(singletonPool(), a);
1301    }
1302
1303    /**
1304     * invokeAll(tasks) with any null task throws NPE
1305     */
1306    public void testInvokeAllNPESingleton() {
1307        RecursiveAction a = new CheckedRecursiveAction() {
1308            protected void realCompute() {
1309                AsyncFib f = new AsyncFib(8);
1310                AsyncFib g = new AsyncFib(9);
1311                AsyncFib h = null;
1312                try {
1313                    invokeAll(f, g, h);
1314                    shouldThrow();
1315                } catch (NullPointerException success) {}
1316            }};
1317        testInvokeOnPool(singletonPool(), a);
1318    }
1319
1320    /**
1321     * invokeAll(t1, t2) throw exception if any task does
1322     */
1323    public void testAbnormalInvokeAll2Singleton() {
1324        RecursiveAction a = new CheckedRecursiveAction() {
1325            protected void realCompute() {
1326                AsyncFib f = new AsyncFib(8);
1327                FailingAsyncFib g = new FailingAsyncFib(9);
1328                try {
1329                    invokeAll(f, g);
1330                    shouldThrow();
1331                } catch (FJException success) {
1332                    checkCompletedAbnormally(g, success);
1333                }
1334            }};
1335        testInvokeOnPool(singletonPool(), a);
1336    }
1337
1338    /**
1339     * invokeAll(tasks) with 1 argument throws exception if task does
1340     */
1341    public void testAbnormalInvokeAll1Singleton() {
1342        RecursiveAction a = new CheckedRecursiveAction() {
1343            protected void realCompute() {
1344                FailingAsyncFib g = new FailingAsyncFib(9);
1345                try {
1346                    invokeAll(g);
1347                    shouldThrow();
1348                } catch (FJException success) {
1349                    checkCompletedAbnormally(g, success);
1350                }
1351            }};
1352        testInvokeOnPool(singletonPool(), a);
1353    }
1354
1355    /**
1356     * invokeAll(tasks) with > 2 argument throws exception if any task does
1357     */
1358    public void testAbnormalInvokeAll3Singleton() {
1359        RecursiveAction a = new CheckedRecursiveAction() {
1360            protected void realCompute() {
1361                AsyncFib f = new AsyncFib(8);
1362                FailingAsyncFib g = new FailingAsyncFib(9);
1363                AsyncFib h = new AsyncFib(7);
1364                try {
1365                    invokeAll(f, g, h);
1366                    shouldThrow();
1367                } catch (FJException success) {
1368                    checkCompletedAbnormally(g, success);
1369                }
1370            }};
1371        testInvokeOnPool(singletonPool(), a);
1372    }
1373
1374    /**
1375     * invokeAll(collection) throws exception if any task does
1376     */
1377    public void testAbnormalInvokeAllCollectionSingleton() {
1378        RecursiveAction a = new CheckedRecursiveAction() {
1379            protected void realCompute() {
1380                FailingAsyncFib f = new FailingAsyncFib(8);
1381                AsyncFib g = new AsyncFib(9);
1382                AsyncFib h = new AsyncFib(7);
1383                HashSet set = new HashSet();
1384                set.add(f);
1385                set.add(g);
1386                set.add(h);
1387                try {
1388                    invokeAll(set);
1389                    shouldThrow();
1390                } catch (FJException success) {
1391                    checkCompletedAbnormally(f, success);
1392                }
1393            }};
1394        testInvokeOnPool(singletonPool(), a);
1395    }
1396
1145      /**
1146       * ForkJoinTask.quietlyComplete returns when task completes
1147       * normally without setting a value. The most recent value
# Line 1415 | Line 1163 | public class ForkJoinTask8Test extends J
1163          testInvokeOnPool(mainPool(), a);
1164      }
1165  
1166 +    // jdk9
1167 +
1168 +    /**
1169 +     * pollSubmission returns unexecuted submitted task, if present
1170 +     */
1171 +    public void testPollSubmission() {
1172 +        final CountDownLatch done = new CountDownLatch(1);
1173 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1174 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1175 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1176 +        final ForkJoinPool p = singletonPool();
1177 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1178 +            Thread external = new Thread(new CheckedRunnable() {
1179 +                public void realRun() {
1180 +                    p.execute(a);
1181 +                    p.execute(b);
1182 +                    p.execute(c);
1183 +                }});
1184 +            RecursiveAction s = new CheckedRecursiveAction() {
1185 +                protected void realCompute() {
1186 +                    external.start();
1187 +                    try {
1188 +                        external.join();
1189 +                    } catch (Exception ex) {
1190 +                        threadUnexpectedException(ex);
1191 +                    }
1192 +                    assertTrue(p.hasQueuedSubmissions());
1193 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1194 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1195 +                    assertTrue(r == a || r == b || r == c);
1196 +                    assertFalse(r.isDone());
1197 +                }};
1198 +            p.invoke(s);
1199 +        }
1200 +    }
1201 +
1202   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines