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

Comparing jsr166/src/test/tck/CountedCompleterTest.java (file contents):
Revision 1.4 by jsr166, Fri Mar 22 16:29:42 2013 UTC vs.
Revision 1.28 by jsr166, Mon Aug 29 19:13:16 2016 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import java.util.concurrent.ExecutionException;
6 >
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 > import static java.util.concurrent.TimeUnit.SECONDS;
9 >
10 > import java.util.HashSet;
11   import java.util.concurrent.CancellationException;
12 + import java.util.concurrent.CountedCompleter;
13 + import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
16 < import java.util.concurrent.CountedCompleter;
11 < import java.util.concurrent.ForkJoinWorkerThread;
12 < import java.util.concurrent.RecursiveAction;
13 < import java.util.concurrent.TimeUnit;
16 > import java.util.concurrent.ThreadLocalRandom;
17   import java.util.concurrent.TimeoutException;
18 < import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
21 < import java.util.HashSet;
22 < import junit.framework.*;
18 > import java.util.concurrent.atomic.AtomicInteger;
19 > import java.util.concurrent.atomic.AtomicReference;
20 > import java.util.function.BiConsumer;
21 > import java.util.function.Consumer;
22 >
23 > import junit.framework.Test;
24 > import junit.framework.TestSuite;
25  
26   public class CountedCompleterTest extends JSR166TestCase {
27  
28      public static void main(String[] args) {
29 <        junit.textui.TestRunner.run(suite());
29 >        main(suite(), args);
30      }
31  
32      public static Test suite() {
# Line 32 | Line 37 | public class CountedCompleterTest extend
37      static final int mainPoolSize =
38          Math.max(2, Runtime.getRuntime().availableProcessors());
39  
35    /**
36     * Analog of CheckedRunnable for ForkJoinTasks
37     */
38    public abstract class CheckedFJTask extends RecursiveAction {
39        protected abstract void realCompute() throws Throwable;
40
41        public final void compute() {
42            try {
43                realCompute();
44            } catch (Throwable t) {
45                threadUnexpectedException(t);
46            }
47        }
48    }
49
40      private static ForkJoinPool mainPool() {
41          return new ForkJoinPool(mainPoolSize);
42      }
# Line 62 | Line 52 | public class CountedCompleterTest extend
52      }
53  
54      private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) {
55 <        try {
55 >        try (PoolCleaner cleaner = cleaner(pool)) {
56              assertFalse(a.isDone());
57              assertFalse(a.isCompletedNormally());
58              assertFalse(a.isCompletedAbnormally());
# Line 78 | Line 68 | public class CountedCompleterTest extend
68              assertFalse(a.isCancelled());
69              assertNull(a.getException());
70              assertNull(a.getRawResult());
81        } finally {
82            joinPool(pool);
71          }
72      }
73  
# Line 98 | Line 86 | public class CountedCompleterTest extend
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87      }
88  
89 <    <T> void checkCompletedNormally(CountedCompleter<T> a) {
102 <        checkCompletedNormally(a, null);
103 <    }
104 <
105 <    <T> void checkCompletedNormally(CountedCompleter<T> a, T expected) {
89 >    void checkCompletedNormally(CountedCompleter<?> a) {
90          assertTrue(a.isDone());
91          assertFalse(a.isCancelled());
92          assertTrue(a.isCompletedNormally());
93          assertFalse(a.isCompletedAbnormally());
94          assertNull(a.getException());
95 <        assertSame(expected, a.getRawResult());
95 >        assertNull(a.getRawResult());
96  
97          {
98              Thread.currentThread().interrupt();
99 <            long t0 = System.nanoTime();
100 <            assertSame(expected, a.join());
101 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
99 >            long startTime = System.nanoTime();
100 >            assertNull(a.join());
101 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
102              Thread.interrupted();
103          }
104  
105          {
106              Thread.currentThread().interrupt();
107 <            long t0 = System.nanoTime();
107 >            long startTime = System.nanoTime();
108              a.quietlyJoin();        // should be no-op
109 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
109 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
110              Thread.interrupted();
111          }
112  
113          assertFalse(a.cancel(false));
114          assertFalse(a.cancel(true));
115          try {
116 <            assertSame(expected, a.get());
116 >            assertNull(a.get());
117          } catch (Throwable fail) { threadUnexpectedException(fail); }
118          try {
119 <            assertSame(expected, a.get(5L, SECONDS));
119 >            assertNull(a.get(5L, SECONDS));
120          } catch (Throwable fail) { threadUnexpectedException(fail); }
121      }
122  
# Line 155 | Line 139 | public class CountedCompleterTest extend
139          Thread.interrupted();
140  
141          {
142 <            long t0 = System.nanoTime();
142 >            long startTime = System.nanoTime();
143              a.quietlyJoin();        // should be no-op
144 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
144 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
145          }
146  
147          try {
# Line 193 | Line 177 | public class CountedCompleterTest extend
177          Thread.interrupted();
178  
179          {
180 <            long t0 = System.nanoTime();
180 >            long startTime = System.nanoTime();
181              a.quietlyJoin();        // should be no-op
182 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
182 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
183          }
184  
185          try {
# Line 211 | Line 195 | public class CountedCompleterTest extend
195          } catch (ExecutionException success) {
196              assertSame(t.getClass(), success.getCause().getClass());
197          } catch (Throwable fail) { threadUnexpectedException(fail); }
198 +
199 +        try {
200 +            a.invoke();
201 +            shouldThrow();
202 +        } catch (Throwable success) {
203 +            assertSame(t, success);
204 +        }
205      }
206  
207      public static final class FJException extends RuntimeException {
208          FJException() { super(); }
209      }
210  
211 <    static final class NoopCountedCompleter extends CountedCompleter {
212 <        boolean post; // set true if onCompletion called
213 <        NoopCountedCompleter() { super(); }
214 <        NoopCountedCompleter(CountedCompleter p) { super(p); }
215 <        public void compute() {}
216 <        public final void onCompletion(CountedCompleter caller) {
217 <            post = true;
211 >    abstract class CheckedCC extends CountedCompleter<Object> {
212 >        final AtomicInteger computeN = new AtomicInteger(0);
213 >        final AtomicInteger onCompletionN = new AtomicInteger(0);
214 >        final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0);
215 >        final AtomicInteger setRawResultN = new AtomicInteger(0);
216 >        final AtomicReference<Object> rawResult = new AtomicReference<Object>(null);
217 >        int computeN() { return computeN.get(); }
218 >        int onCompletionN() { return onCompletionN.get(); }
219 >        int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); }
220 >        int setRawResultN() { return setRawResultN.get(); }
221 >
222 >        CheckedCC() { super(); }
223 >        CheckedCC(CountedCompleter p) { super(p); }
224 >        CheckedCC(CountedCompleter p, int n) { super(p, n); }
225 >        abstract void realCompute();
226 >        public final void compute() {
227 >            computeN.incrementAndGet();
228 >            realCompute();
229 >        }
230 >        public void onCompletion(CountedCompleter caller) {
231 >            onCompletionN.incrementAndGet();
232 >            super.onCompletion(caller);
233 >        }
234 >        public boolean onExceptionalCompletion(Throwable ex,
235 >                                               CountedCompleter caller) {
236 >            onExceptionalCompletionN.incrementAndGet();
237 >            assertNotNull(ex);
238 >            assertTrue(isCompletedAbnormally());
239 >            assertTrue(super.onExceptionalCompletion(ex, caller));
240 >            return true;
241 >        }
242 >        protected void setRawResult(Object t) {
243 >            setRawResultN.incrementAndGet();
244 >            rawResult.set(t);
245 >            super.setRawResult(t);
246 >        }
247 >        void checkIncomplete() {
248 >            assertEquals(0, computeN());
249 >            assertEquals(0, onCompletionN());
250 >            assertEquals(0, onExceptionalCompletionN());
251 >            assertEquals(0, setRawResultN());
252 >            checkNotDone(this);
253 >        }
254 >        void checkCompletes(Object rawResult) {
255 >            checkIncomplete();
256 >            int pendingCount = getPendingCount();
257 >            complete(rawResult);
258 >            assertEquals(pendingCount, getPendingCount());
259 >            assertEquals(0, computeN());
260 >            assertEquals(1, onCompletionN());
261 >            assertEquals(0, onExceptionalCompletionN());
262 >            assertEquals(1, setRawResultN());
263 >            assertSame(rawResult, this.rawResult.get());
264 >            checkCompletedNormally(this);
265 >        }
266 >        void checkCompletesExceptionally(Throwable ex) {
267 >            checkIncomplete();
268 >            completeExceptionally(ex);
269 >            checkCompletedExceptionally(ex);
270 >        }
271 >        void checkCompletedExceptionally(Throwable ex) {
272 >            assertEquals(0, computeN());
273 >            assertEquals(0, onCompletionN());
274 >            assertEquals(1, onExceptionalCompletionN());
275 >            assertEquals(0, setRawResultN());
276 >            assertNull(this.rawResult.get());
277 >            checkCompletedAbnormally(this, ex);
278          }
279      }
280  
281 +    final class NoopCC extends CheckedCC {
282 +        NoopCC() { super(); }
283 +        NoopCC(CountedCompleter p) { super(p); }
284 +        NoopCC(CountedCompleter p, int initialPendingCount) {
285 +            super(p, initialPendingCount);
286 +        }
287 +        protected void realCompute() {}
288 +    }
289 +
290      /**
291       * A newly constructed CountedCompleter is not completed;
292 <     * complete() causes completion.
292 >     * complete() causes completion. pendingCount is ignored.
293       */
294      public void testComplete() {
295 <        NoopCountedCompleter a = new NoopCountedCompleter();
296 <        assertFalse(a.isDone());
297 <        assertFalse(a.isCompletedNormally());
298 <        assertFalse(a.isCompletedAbnormally());
299 <        assertFalse(a.isCancelled());
300 <        assertNull(a.getException());
301 <        assertNull(a.getRawResult());
302 <        assertFalse(a.post);
303 <        a.complete(null);
304 <        assertTrue(a.post);
305 <        assertTrue(a.isDone());
246 <        assertTrue(a.isCompletedNormally());
247 <        assertFalse(a.isCompletedAbnormally());
248 <        assertFalse(a.isCancelled());
249 <        assertNull(a.getException());
250 <        assertNull(a.getRawResult());
295 >        for (Object x : new Object[] { Boolean.TRUE, null }) {
296 >            for (int pendingCount : new int[] { 0, 42 }) {
297 >                testComplete(new NoopCC(), x, pendingCount);
298 >                testComplete(new NoopCC(new NoopCC()), x, pendingCount);
299 >            }
300 >        }
301 >    }
302 >    void testComplete(NoopCC cc, Object x, int pendingCount) {
303 >        cc.setPendingCount(pendingCount);
304 >        cc.checkCompletes(x);
305 >        assertEquals(pendingCount, cc.getPendingCount());
306      }
307  
308      /**
309       * completeExceptionally completes exceptionally
310       */
311      public void testCompleteExceptionally() {
312 <        NoopCountedCompleter a = new NoopCountedCompleter();
313 <        assertFalse(a.isDone());
314 <        assertFalse(a.isCompletedNormally());
315 <        assertFalse(a.isCompletedAbnormally());
316 <        assertFalse(a.isCancelled());
317 <        assertNull(a.getException());
318 <        assertNull(a.getRawResult());
319 <        assertFalse(a.post);
320 <        a.completeExceptionally(new FJException());
321 <        assertFalse(a.post);
322 <        assertTrue(a.isDone());
323 <        assertFalse(a.isCompletedNormally());
324 <        assertTrue(a.isCompletedAbnormally());
325 <        assertFalse(a.isCancelled());
326 <        assertTrue(a.getException() instanceof FJException);
327 <        assertNull(a.getRawResult());
312 >        new NoopCC()
313 >            .checkCompletesExceptionally(new FJException());
314 >        new NoopCC(new NoopCC())
315 >            .checkCompletesExceptionally(new FJException());
316 >    }
317 >
318 >    /**
319 >     * completeExceptionally(null) surprisingly has the same effect as
320 >     * completeExceptionally(new RuntimeException())
321 >     */
322 >    public void testCompleteExceptionally_null() {
323 >        NoopCC a = new NoopCC();
324 >        a.completeExceptionally(null);
325 >        try {
326 >            a.invoke();
327 >            shouldThrow();
328 >        } catch (RuntimeException success) {
329 >            assertSame(success.getClass(), RuntimeException.class);
330 >            assertNull(success.getCause());
331 >            a.checkCompletedExceptionally(success);
332 >        }
333      }
334  
335      /**
336       * setPendingCount sets the reported pending count
337       */
338      public void testSetPendingCount() {
339 <        NoopCountedCompleter a = new NoopCountedCompleter();
339 >        NoopCC a = new NoopCC();
340          assertEquals(0, a.getPendingCount());
341 <        a.setPendingCount(1);
342 <        assertEquals(1, a.getPendingCount());
343 <        a.setPendingCount(27);
344 <        assertEquals(27, a.getPendingCount());
341 >        int[] vals = {
342 >             -1, 0, 1,
343 >             Integer.MIN_VALUE,
344 >             Integer.MAX_VALUE,
345 >        };
346 >        for (int val : vals) {
347 >            a.setPendingCount(val);
348 >            assertEquals(val, a.getPendingCount());
349 >        }
350      }
351  
352      /**
353       * addToPendingCount adds to the reported pending count
354       */
355      public void testAddToPendingCount() {
356 <        NoopCountedCompleter a = new NoopCountedCompleter();
356 >        NoopCC a = new NoopCC();
357          assertEquals(0, a.getPendingCount());
358          a.addToPendingCount(1);
359          assertEquals(1, a.getPendingCount());
360          a.addToPendingCount(27);
361          assertEquals(28, a.getPendingCount());
362 +        a.addToPendingCount(-28);
363 +        assertEquals(0, a.getPendingCount());
364      }
365  
366      /**
367       * decrementPendingCountUnlessZero decrements reported pending
368       * count unless zero
369       */
370 <    public void testDecrementPendingCount() {
371 <        NoopCountedCompleter a = new NoopCountedCompleter();
372 <        assertEquals(0, a.getPendingCount());
373 <        a.addToPendingCount(1);
370 >    public void testDecrementPendingCountUnlessZero() {
371 >        NoopCC a = new NoopCC(null, 2);
372 >        assertEquals(2, a.getPendingCount());
373 >        assertEquals(2, a.decrementPendingCountUnlessZero());
374          assertEquals(1, a.getPendingCount());
375 <        a.decrementPendingCountUnlessZero();
375 >        assertEquals(1, a.decrementPendingCountUnlessZero());
376 >        assertEquals(0, a.getPendingCount());
377 >        assertEquals(0, a.decrementPendingCountUnlessZero());
378          assertEquals(0, a.getPendingCount());
379 <        a.decrementPendingCountUnlessZero();
379 >        a.setPendingCount(-1);
380 >        assertEquals(-1, a.decrementPendingCountUnlessZero());
381 >        assertEquals(-2, a.getPendingCount());
382 >    }
383 >
384 >    /**
385 >     * compareAndSetPendingCount compares and sets the reported
386 >     * pending count
387 >     */
388 >    public void testCompareAndSetPendingCount() {
389 >        NoopCC a = new NoopCC();
390          assertEquals(0, a.getPendingCount());
391 +        assertTrue(a.compareAndSetPendingCount(0, 1));
392 +        assertEquals(1, a.getPendingCount());
393 +        assertTrue(a.compareAndSetPendingCount(1, 2));
394 +        assertEquals(2, a.getPendingCount());
395 +        assertFalse(a.compareAndSetPendingCount(1, 3));
396 +        assertEquals(2, a.getPendingCount());
397      }
398  
399      /**
400       * getCompleter returns parent or null if at root
401       */
402      public void testGetCompleter() {
403 <        NoopCountedCompleter a = new NoopCountedCompleter();
403 >        NoopCC a = new NoopCC();
404          assertNull(a.getCompleter());
405 <        CountedCompleter b = new NoopCountedCompleter(a);
406 <        assertEquals(a, b.getCompleter());
405 >        CountedCompleter b = new NoopCC(a);
406 >        assertSame(a, b.getCompleter());
407 >        CountedCompleter c = new NoopCC(b);
408 >        assertSame(b, c.getCompleter());
409      }
410  
411      /**
412       * getRoot returns self if no parent, else parent's root
413       */
414      public void testGetRoot() {
415 <        NoopCountedCompleter a = new NoopCountedCompleter();
416 <        NoopCountedCompleter b = new NoopCountedCompleter(a);
417 <        assertEquals(a, a.getRoot());
418 <        assertEquals(a, b.getRoot());
415 >        NoopCC a = new NoopCC();
416 >        NoopCC b = new NoopCC(a);
417 >        NoopCC c = new NoopCC(b);
418 >        assertSame(a, a.getRoot());
419 >        assertSame(a, b.getRoot());
420 >        assertSame(a, c.getRoot());
421      }
422  
423      /**
424 <     * tryComplete causes completion if pending count is zero
424 >     * tryComplete decrements pending count unless zero, in which case
425 >     * causes completion
426       */
427 <    public void testTryComplete1() {
428 <        NoopCountedCompleter a = new NoopCountedCompleter();
427 >    public void testTryComplete() {
428 >        NoopCC a = new NoopCC();
429          assertEquals(0, a.getPendingCount());
430 +        int n = 3;
431 +        a.setPendingCount(n);
432 +        for (; n > 0; n--) {
433 +            assertEquals(n, a.getPendingCount());
434 +            a.tryComplete();
435 +            a.checkIncomplete();
436 +            assertEquals(n - 1, a.getPendingCount());
437 +        }
438          a.tryComplete();
439 <        assertTrue(a.post);
440 <        assertTrue(a.isDone());
439 >        assertEquals(0, a.computeN());
440 >        assertEquals(1, a.onCompletionN());
441 >        assertEquals(0, a.onExceptionalCompletionN());
442 >        assertEquals(0, a.setRawResultN());
443 >        checkCompletedNormally(a);
444      }
445  
446      /**
447 <     * propagateCompletion causes completion without invoking
448 <     * onCompletion if pending count is zero
447 >     * propagateCompletion decrements pending count unless zero, in
448 >     * which case causes completion, without invoking onCompletion
449       */
450      public void testPropagateCompletion() {
451 <        NoopCountedCompleter a = new NoopCountedCompleter();
451 >        NoopCC a = new NoopCC();
452          assertEquals(0, a.getPendingCount());
453 +        int n = 3;
454 +        a.setPendingCount(n);
455 +        for (; n > 0; n--) {
456 +            assertEquals(n, a.getPendingCount());
457 +            a.propagateCompletion();
458 +            a.checkIncomplete();
459 +            assertEquals(n - 1, a.getPendingCount());
460 +        }
461          a.propagateCompletion();
462 <        assertFalse(a.post);
463 <        assertTrue(a.isDone());
464 <    }
465 <
466 <    /**
358 <     * tryComplete decrements pending count unless zero
359 <     */
360 <    public void testTryComplete2() {
361 <        NoopCountedCompleter a = new NoopCountedCompleter();
362 <        assertEquals(0, a.getPendingCount());
363 <        a.setPendingCount(1);
364 <        a.tryComplete();
365 <        assertFalse(a.post);
366 <        assertFalse(a.isDone());
367 <        assertEquals(0, a.getPendingCount());
368 <        a.tryComplete();
369 <        assertTrue(a.post);
370 <        assertTrue(a.isDone());
462 >        assertEquals(0, a.computeN());
463 >        assertEquals(0, a.onCompletionN());
464 >        assertEquals(0, a.onExceptionalCompletionN());
465 >        assertEquals(0, a.setRawResultN());
466 >        checkCompletedNormally(a);
467      }
468  
469      /**
470       * firstComplete returns this if pending count is zero else null
471       */
472      public void testFirstComplete() {
473 <        NoopCountedCompleter a = new NoopCountedCompleter();
473 >        NoopCC a = new NoopCC();
474          a.setPendingCount(1);
475          assertNull(a.firstComplete());
476 <        assertEquals(a, a.firstComplete());
476 >        a.checkIncomplete();
477 >        assertSame(a, a.firstComplete());
478 >        a.checkIncomplete();
479      }
480  
481      /**
# Line 385 | Line 483 | public class CountedCompleterTest extend
483       * zero else null
484       */
485      public void testNextComplete() {
486 <        NoopCountedCompleter a = new NoopCountedCompleter();
487 <        NoopCountedCompleter b = new NoopCountedCompleter(a);
486 >        NoopCC a = new NoopCC();
487 >        NoopCC b = new NoopCC(a);
488          a.setPendingCount(1);
489          b.setPendingCount(1);
490          assertNull(b.firstComplete());
491 <        CountedCompleter c = b.firstComplete();
492 <        assertEquals(b, c);
493 <        CountedCompleter d = c.nextComplete();
494 <        assertNull(d);
495 <        CountedCompleter e = c.nextComplete();
496 <        assertEquals(a, e);
491 >        assertSame(b, b.firstComplete());
492 >        assertNull(b.nextComplete());
493 >        a.checkIncomplete();
494 >        b.checkIncomplete();
495 >        assertSame(a, b.nextComplete());
496 >        assertSame(a, b.nextComplete());
497 >        a.checkIncomplete();
498 >        b.checkIncomplete();
499 >        assertNull(a.nextComplete());
500 >        b.checkIncomplete();
501 >        checkCompletedNormally(a);
502      }
503  
504      /**
505 <     * quietlyCompleteRoot completes root task
505 >     * quietlyCompleteRoot completes root task and only root task
506       */
507      public void testQuietlyCompleteRoot() {
508 <        NoopCountedCompleter a = new NoopCountedCompleter();
509 <        NoopCountedCompleter b = new NoopCountedCompleter(a);
508 >        NoopCC a = new NoopCC();
509 >        NoopCC b = new NoopCC(a);
510 >        NoopCC c = new NoopCC(b);
511          a.setPendingCount(1);
512          b.setPendingCount(1);
513 <        b.quietlyCompleteRoot();
513 >        c.setPendingCount(1);
514 >        c.quietlyCompleteRoot();
515          assertTrue(a.isDone());
516          assertFalse(b.isDone());
517 +        assertFalse(c.isDone());
518      }
519  
520      // Invocation tests use some interdependent task classes
521      // to better test propagation etc
522  
523 <
524 <    // Version of Fibonacci with different classes for left vs right forks
525 <    abstract static class CCF extends CountedCompleter {
523 >    /**
524 >     * Version of Fibonacci with different classes for left vs right forks
525 >     */
526 >    abstract class CCF extends CheckedCC {
527          int number;
528          int rnumber;
529  
# Line 425 | Line 532 | public class CountedCompleterTest extend
532              this.number = n;
533          }
534  
535 <        public final void compute() {
429 <            CountedCompleter p;
535 >        protected final void realCompute() {
536              CCF f = this;
537              int n = number;
538              while (n >= 2) {
539                  new RCCF(f, n - 2).fork();
540                  f = new LCCF(f, --n);
541              }
542 <            f.number = n;
437 <            f.onCompletion(f);
438 <            if ((p = f.getCompleter()) != null)
439 <                p.tryComplete();
440 <            else
441 <                f.quietlyComplete();
542 >            f.complete(null);
543          }
544      }
545  
546 <    static final class LCCF extends CCF {
546 >    final class LCCF extends CCF {
547 >        public LCCF(int n) { this(null, n); }
548          public LCCF(CountedCompleter parent, int n) {
549              super(parent, n);
550          }
551          public final void onCompletion(CountedCompleter caller) {
552 +            super.onCompletion(caller);
553              CCF p = (CCF)getCompleter();
554              int n = number + rnumber;
555              if (p != null)
# Line 455 | Line 558 | public class CountedCompleterTest extend
558                  number = n;
559          }
560      }
561 <    static final class RCCF extends CCF {
561 >    final class RCCF extends CCF {
562          public RCCF(CountedCompleter parent, int n) {
563              super(parent, n);
564          }
565          public final void onCompletion(CountedCompleter caller) {
566 +            super.onCompletion(caller);
567              CCF p = (CCF)getCompleter();
568              int n = number + rnumber;
569              if (p != null)
# Line 470 | Line 574 | public class CountedCompleterTest extend
574      }
575  
576      // Version of CCF with forced failure in left completions
577 <    abstract static class FailingCCF extends CountedCompleter {
577 >    abstract class FailingCCF extends CheckedCC {
578          int number;
579          int rnumber;
580  
# Line 479 | Line 583 | public class CountedCompleterTest extend
583              this.number = n;
584          }
585  
586 <        public final void compute() {
483 <            CountedCompleter p;
586 >        protected final void realCompute() {
587              FailingCCF f = this;
588              int n = number;
589              while (n >= 2) {
590                  new RFCCF(f, n - 2).fork();
591                  f = new LFCCF(f, --n);
592              }
593 <            f.number = n;
491 <            f.onCompletion(f);
492 <            if ((p = f.getCompleter()) != null)
493 <                p.tryComplete();
494 <            else
495 <                f.quietlyComplete();
593 >            f.complete(null);
594          }
595      }
596  
597 <    static final class LFCCF extends FailingCCF {
597 >    final class LFCCF extends FailingCCF {
598 >        public LFCCF(int n) { this(null, n); }
599          public LFCCF(CountedCompleter parent, int n) {
600              super(parent, n);
601          }
602          public final void onCompletion(CountedCompleter caller) {
603 +            super.onCompletion(caller);
604              FailingCCF p = (FailingCCF)getCompleter();
605              int n = number + rnumber;
606              if (p != null)
# Line 509 | Line 609 | public class CountedCompleterTest extend
609                  number = n;
610          }
611      }
612 <    static final class RFCCF extends FailingCCF {
612 >    final class RFCCF extends FailingCCF {
613          public RFCCF(CountedCompleter parent, int n) {
614              super(parent, n);
615          }
616          public final void onCompletion(CountedCompleter caller) {
617 +            super.onCompletion(caller);
618              completeExceptionally(new FJException());
619          }
620      }
# Line 524 | Line 625 | public class CountedCompleterTest extend
625       * completed tasks; getRawResult returns null.
626       */
627      public void testInvoke() {
628 <       ForkJoinTask a =  new CheckedFJTask() {
629 <            public void realCompute() {
630 <                CCF f = new LCCF(null, 8);
628 >        ForkJoinTask a = new CheckedRecursiveAction() {
629 >            protected void realCompute() {
630 >                CCF f = new LCCF(8);
631                  assertNull(f.invoke());
632                  assertEquals(21, f.number);
633                  checkCompletedNormally(f);
# Line 540 | Line 641 | public class CountedCompleterTest extend
641       * completed tasks
642       */
643      public void testQuietlyInvoke() {
644 <       ForkJoinTask a =  new CheckedFJTask() {
645 <            public void realCompute() {
646 <                CCF f = new LCCF(null, 8);
644 >        ForkJoinTask a = new CheckedRecursiveAction() {
645 >            protected void realCompute() {
646 >                CCF f = new LCCF(8);
647                  f.quietlyInvoke();
648                  assertEquals(21, f.number);
649                  checkCompletedNormally(f);
# Line 554 | Line 655 | public class CountedCompleterTest extend
655       * join of a forked task returns when task completes
656       */
657      public void testForkJoin() {
658 <       ForkJoinTask a =  new CheckedFJTask() {
659 <            public void realCompute() {
660 <                CCF f = new LCCF(null, 8);
658 >        ForkJoinTask a = new CheckedRecursiveAction() {
659 >            protected void realCompute() {
660 >                CCF f = new LCCF(8);
661                  assertSame(f, f.fork());
662                  assertNull(f.join());
663                  assertEquals(21, f.number);
# Line 569 | Line 670 | public class CountedCompleterTest extend
670       * get of a forked task returns when task completes
671       */
672      public void testForkGet() {
673 <       ForkJoinTask a =  new CheckedFJTask() {
674 <            public void realCompute() throws Exception {
675 <                CCF f = new LCCF(null, 8);
673 >        ForkJoinTask a = new CheckedRecursiveAction() {
674 >            protected void realCompute() throws Exception {
675 >                CCF f = new LCCF(8);
676                  assertSame(f, f.fork());
677                  assertNull(f.get());
678                  assertEquals(21, f.number);
# Line 584 | Line 685 | public class CountedCompleterTest extend
685       * timed get of a forked task returns when task completes
686       */
687      public void testForkTimedGet() {
688 <       ForkJoinTask a =  new CheckedFJTask() {
689 <            public void realCompute() throws Exception {
690 <                CCF f = new LCCF(null, 8);
688 >        ForkJoinTask a = new CheckedRecursiveAction() {
689 >            protected void realCompute() throws Exception {
690 >                CCF f = new LCCF(8);
691                  assertSame(f, f.fork());
692                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
693                  assertEquals(21, f.number);
# Line 599 | Line 700 | public class CountedCompleterTest extend
700       * timed get with null time unit throws NPE
701       */
702      public void testForkTimedGetNPE() {
703 <       ForkJoinTask a =  new CheckedFJTask() {
704 <            public void realCompute() throws Exception {
705 <                CCF f = new LCCF(null, 8);
703 >        ForkJoinTask a = new CheckedRecursiveAction() {
704 >            protected void realCompute() throws Exception {
705 >                CCF f = new LCCF(8);
706                  assertSame(f, f.fork());
707                  try {
708                      f.get(5L, null);
# Line 615 | Line 716 | public class CountedCompleterTest extend
716       * quietlyJoin of a forked task returns when task completes
717       */
718      public void testForkQuietlyJoin() {
719 <       ForkJoinTask a =  new CheckedFJTask() {
720 <            public void realCompute() {
721 <                CCF f = new LCCF(null, 8);
719 >        ForkJoinTask a = new CheckedRecursiveAction() {
720 >            protected void realCompute() {
721 >                CCF f = new LCCF(8);
722                  assertSame(f, f.fork());
723                  f.quietlyJoin();
724                  assertEquals(21, f.number);
# Line 631 | Line 732 | public class CountedCompleterTest extend
732       * getQueuedTaskCount returns 0 when quiescent
733       */
734      public void testForkHelpQuiesce() {
735 <       ForkJoinTask a =  new CheckedFJTask() {
736 <            public void realCompute() {
737 <                CCF f = new LCCF(null, 8);
735 >        ForkJoinTask a = new CheckedRecursiveAction() {
736 >            protected void realCompute() {
737 >                CCF f = new LCCF(8);
738                  assertSame(f, f.fork());
739                  helpQuiesce();
740                  assertEquals(21, f.number);
# Line 647 | Line 748 | public class CountedCompleterTest extend
748       * invoke task throws exception when task completes abnormally
749       */
750      public void testAbnormalInvoke() {
751 <       ForkJoinTask a =  new CheckedFJTask() {
752 <            public void realCompute() {
753 <                FailingCCF f = new LFCCF(null, 8);
751 >        ForkJoinTask a = new CheckedRecursiveAction() {
752 >            protected void realCompute() {
753 >                FailingCCF f = new LFCCF(8);
754                  try {
755                      f.invoke();
756                      shouldThrow();
# Line 664 | Line 765 | public class CountedCompleterTest extend
765       * quietlyInvoke task returns when task completes abnormally
766       */
767      public void testAbnormalQuietlyInvoke() {
768 <       ForkJoinTask a =  new CheckedFJTask() {
769 <            public void realCompute() {
770 <                FailingCCF f = new LFCCF(null, 8);
768 >        ForkJoinTask a = new CheckedRecursiveAction() {
769 >            protected void realCompute() {
770 >                FailingCCF f = new LFCCF(8);
771                  f.quietlyInvoke();
772                  assertTrue(f.getException() instanceof FJException);
773                  checkCompletedAbnormally(f, f.getException());
# Line 678 | Line 779 | public class CountedCompleterTest extend
779       * join of a forked task throws exception when task completes abnormally
780       */
781      public void testAbnormalForkJoin() {
782 <       ForkJoinTask a =  new CheckedFJTask() {
783 <            public void realCompute() {
784 <                FailingCCF f = new LFCCF(null, 8);
782 >        ForkJoinTask a = new CheckedRecursiveAction() {
783 >            protected void realCompute() {
784 >                FailingCCF f = new LFCCF(8);
785                  assertSame(f, f.fork());
786                  try {
787                      f.join();
# Line 696 | Line 797 | public class CountedCompleterTest extend
797       * get of a forked task throws exception when task completes abnormally
798       */
799      public void testAbnormalForkGet() {
800 <       ForkJoinTask a =  new CheckedFJTask() {
801 <            public void realCompute() throws Exception {
802 <                FailingCCF f = new LFCCF(null, 8);
800 >        ForkJoinTask a = new CheckedRecursiveAction() {
801 >            protected void realCompute() throws Exception {
802 >                FailingCCF f = new LFCCF(8);
803                  assertSame(f, f.fork());
804                  try {
805                      f.get();
# Line 716 | Line 817 | public class CountedCompleterTest extend
817       * timed get of a forked task throws exception when task completes abnormally
818       */
819      public void testAbnormalForkTimedGet() {
820 <       ForkJoinTask a =  new CheckedFJTask() {
821 <            public void realCompute() throws Exception {
822 <                FailingCCF f = new LFCCF(null, 8);
820 >        ForkJoinTask a = new CheckedRecursiveAction() {
821 >            protected void realCompute() throws Exception {
822 >                FailingCCF f = new LFCCF(8);
823                  assertSame(f, f.fork());
824                  try {
825                      f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 736 | Line 837 | public class CountedCompleterTest extend
837       * quietlyJoin of a forked task returns when task completes abnormally
838       */
839      public void testAbnormalForkQuietlyJoin() {
840 <       ForkJoinTask a =  new CheckedFJTask() {
841 <            public void realCompute() {
842 <                FailingCCF f = new LFCCF(null, 8);
840 >        ForkJoinTask a = new CheckedRecursiveAction() {
841 >            protected void realCompute() {
842 >                FailingCCF f = new LFCCF(8);
843                  assertSame(f, f.fork());
844                  f.quietlyJoin();
845                  assertTrue(f.getException() instanceof FJException);
# Line 751 | Line 852 | public class CountedCompleterTest extend
852       * invoke task throws exception when task cancelled
853       */
854      public void testCancelledInvoke() {
855 <       ForkJoinTask a =  new CheckedFJTask() {
856 <            public void realCompute() {
857 <                CCF f = new LCCF(null, 8);
855 >        ForkJoinTask a = new CheckedRecursiveAction() {
856 >            protected void realCompute() {
857 >                CCF f = new LCCF(8);
858                  assertTrue(f.cancel(true));
859                  try {
860                      f.invoke();
# Line 769 | Line 870 | public class CountedCompleterTest extend
870       * join of a forked task throws exception when task cancelled
871       */
872      public void testCancelledForkJoin() {
873 <       ForkJoinTask a =  new CheckedFJTask() {
874 <            public void realCompute() {
875 <                CCF f = new LCCF(null, 8);
873 >        ForkJoinTask a = new CheckedRecursiveAction() {
874 >            protected void realCompute() {
875 >                CCF f = new LCCF(8);
876                  assertTrue(f.cancel(true));
877                  assertSame(f, f.fork());
878                  try {
# Line 788 | Line 889 | public class CountedCompleterTest extend
889       * get of a forked task throws exception when task cancelled
890       */
891      public void testCancelledForkGet() {
892 <       ForkJoinTask a =  new CheckedFJTask() {
893 <            public void realCompute() throws Exception {
894 <                CCF f = new LCCF(null, 8);
892 >        ForkJoinTask a = new CheckedRecursiveAction() {
893 >            protected void realCompute() throws Exception {
894 >                CCF f = new LCCF(8);
895                  assertTrue(f.cancel(true));
896                  assertSame(f, f.fork());
897                  try {
# Line 807 | Line 908 | public class CountedCompleterTest extend
908       * timed get of a forked task throws exception when task cancelled
909       */
910      public void testCancelledForkTimedGet() throws Exception {
911 <       ForkJoinTask a =  new CheckedFJTask() {
912 <            public void realCompute() throws Exception {
913 <                CCF f = new LCCF(null, 8);
911 >        ForkJoinTask a = new CheckedRecursiveAction() {
912 >            protected void realCompute() throws Exception {
913 >                CCF f = new LCCF(8);
914                  assertTrue(f.cancel(true));
915                  assertSame(f, f.fork());
916                  try {
# Line 826 | Line 927 | public class CountedCompleterTest extend
927       * quietlyJoin of a forked task returns when task cancelled
928       */
929      public void testCancelledForkQuietlyJoin() {
930 <       ForkJoinTask a =  new CheckedFJTask() {
931 <            public void realCompute() {
932 <                CCF f = new LCCF(null, 8);
930 >        ForkJoinTask a = new CheckedRecursiveAction() {
931 >            protected void realCompute() {
932 >                CCF f = new LCCF(8);
933                  assertTrue(f.cancel(true));
934                  assertSame(f, f.fork());
935                  f.quietlyJoin();
# Line 842 | Line 943 | public class CountedCompleterTest extend
943       */
944      public void testGetPool() {
945          final ForkJoinPool mainPool = mainPool();
946 <       ForkJoinTask a =  new CheckedFJTask() {
947 <            public void realCompute() {
946 >        ForkJoinTask a = new CheckedRecursiveAction() {
947 >            protected void realCompute() {
948                  assertSame(mainPool, getPool());
949              }};
950          testInvokeOnPool(mainPool, a);
# Line 853 | Line 954 | public class CountedCompleterTest extend
954       * getPool of non-FJ task returns null
955       */
956      public void testGetPool2() {
957 <       ForkJoinTask a =  new CheckedFJTask() {
958 <            public void realCompute() {
957 >        ForkJoinTask a = new CheckedRecursiveAction() {
958 >            protected void realCompute() {
959                  assertNull(getPool());
960              }};
961          assertNull(a.invoke());
# Line 864 | Line 965 | public class CountedCompleterTest extend
965       * inForkJoinPool of executing task returns true
966       */
967      public void testInForkJoinPool() {
968 <       ForkJoinTask a =  new CheckedFJTask() {
969 <            public void realCompute() {
968 >        ForkJoinTask a = new CheckedRecursiveAction() {
969 >            protected void realCompute() {
970                  assertTrue(inForkJoinPool());
971              }};
972          testInvokeOnPool(mainPool(), a);
# Line 875 | Line 976 | public class CountedCompleterTest extend
976       * inForkJoinPool of non-FJ task returns false
977       */
978      public void testInForkJoinPool2() {
979 <       ForkJoinTask a =  new CheckedFJTask() {
980 <            public void realCompute() {
979 >        ForkJoinTask a = new CheckedRecursiveAction() {
980 >            protected void realCompute() {
981                  assertFalse(inForkJoinPool());
982              }};
983          assertNull(a.invoke());
# Line 886 | Line 987 | public class CountedCompleterTest extend
987       * setRawResult(null) succeeds
988       */
989      public void testSetRawResult() {
990 <       ForkJoinTask a =  new CheckedFJTask() {
991 <            public void realCompute() {
990 >        ForkJoinTask a = new CheckedRecursiveAction() {
991 >            protected void realCompute() {
992                  setRawResult(null);
993                  assertNull(getRawResult());
994              }};
# Line 898 | Line 999 | public class CountedCompleterTest extend
999       * invoke task throws exception after invoking completeExceptionally
1000       */
1001      public void testCompleteExceptionally2() {
1002 <       ForkJoinTask a =  new CheckedFJTask() {
1003 <            public void realCompute() {
1004 <                CCF f = new LCCF(null, 8);
1005 <                f.completeExceptionally(new FJException());
1006 <                try {
1007 <                    f.invoke();
1008 <                    shouldThrow();
1009 <                } catch (FJException success) {
909 <                    checkCompletedAbnormally(f, success);
910 <                }
1002 >        ForkJoinTask a = new CheckedRecursiveAction() {
1003 >            protected void realCompute() {
1004 >                CCF n = new LCCF(8);
1005 >                CCF f = new LCCF(n, 8);
1006 >                FJException ex = new FJException();
1007 >                f.completeExceptionally(ex);
1008 >                f.checkCompletedExceptionally(ex);
1009 >                n.checkCompletedExceptionally(ex);
1010              }};
1011          testInvokeOnPool(mainPool(), a);
1012      }
# Line 916 | Line 1015 | public class CountedCompleterTest extend
1015       * invokeAll(t1, t2) invokes all task arguments
1016       */
1017      public void testInvokeAll2() {
1018 <       ForkJoinTask a =  new CheckedFJTask() {
1019 <            public void realCompute() {
1020 <                CCF f = new LCCF(null, 8);
1021 <                CCF g = new LCCF(null, 9);
1018 >        ForkJoinTask a = new CheckedRecursiveAction() {
1019 >            protected void realCompute() {
1020 >                CCF f = new LCCF(8);
1021 >                CCF g = new LCCF(9);
1022                  invokeAll(f, g);
1023                  assertEquals(21, f.number);
1024                  assertEquals(34, g.number);
# Line 933 | Line 1032 | public class CountedCompleterTest extend
1032       * invokeAll(tasks) with 1 argument invokes task
1033       */
1034      public void testInvokeAll1() {
1035 <       ForkJoinTask a =  new CheckedFJTask() {
1036 <            public void realCompute() {
1037 <                CCF f = new LCCF(null, 8);
1035 >        ForkJoinTask a = new CheckedRecursiveAction() {
1036 >            protected void realCompute() {
1037 >                CCF f = new LCCF(8);
1038                  invokeAll(f);
1039                  checkCompletedNormally(f);
1040                  assertEquals(21, f.number);
# Line 947 | Line 1046 | public class CountedCompleterTest extend
1046       * invokeAll(tasks) with > 2 argument invokes tasks
1047       */
1048      public void testInvokeAll3() {
1049 <       ForkJoinTask a =  new CheckedFJTask() {
1050 <            public void realCompute() {
1051 <                CCF f = new LCCF(null, 8);
1052 <                CCF g = new LCCF(null, 9);
1053 <                CCF h = new LCCF(null, 7);
1049 >        ForkJoinTask a = new CheckedRecursiveAction() {
1050 >            protected void realCompute() {
1051 >                CCF f = new LCCF(8);
1052 >                CCF g = new LCCF(9);
1053 >                CCF h = new LCCF(7);
1054                  invokeAll(f, g, h);
1055                  assertEquals(21, f.number);
1056                  assertEquals(34, g.number);
# Line 967 | Line 1066 | public class CountedCompleterTest extend
1066       * invokeAll(collection) invokes all tasks in the collection
1067       */
1068      public void testInvokeAllCollection() {
1069 <       ForkJoinTask a =  new CheckedFJTask() {
1070 <            public void realCompute() {
1071 <                CCF f = new LCCF(null, 8);
1072 <                CCF g = new LCCF(null, 9);
1073 <                CCF h = new LCCF(null, 7);
1069 >        ForkJoinTask a = new CheckedRecursiveAction() {
1070 >            protected void realCompute() {
1071 >                CCF f = new LCCF(8);
1072 >                CCF g = new LCCF(9);
1073 >                CCF h = new LCCF(7);
1074                  HashSet set = new HashSet();
1075                  set.add(f);
1076                  set.add(g);
# Line 991 | Line 1090 | public class CountedCompleterTest extend
1090       * invokeAll(tasks) with any null task throws NPE
1091       */
1092      public void testInvokeAllNPE() {
1093 <       ForkJoinTask a =  new CheckedFJTask() {
1094 <            public void realCompute() {
1095 <                CCF f = new LCCF(null, 8);
1096 <                CCF g = new LCCF(null, 9);
1093 >        ForkJoinTask a = new CheckedRecursiveAction() {
1094 >            protected void realCompute() {
1095 >                CCF f = new LCCF(8);
1096 >                CCF g = new LCCF(9);
1097                  CCF h = null;
1098                  try {
1099                      invokeAll(f, g, h);
# Line 1008 | Line 1107 | public class CountedCompleterTest extend
1107       * invokeAll(t1, t2) throw exception if any task does
1108       */
1109      public void testAbnormalInvokeAll2() {
1110 <       ForkJoinTask a =  new CheckedFJTask() {
1111 <            public void realCompute() {
1112 <                CCF f = new LCCF(null, 8);
1113 <                FailingCCF g = new LFCCF(null, 9);
1110 >        ForkJoinTask a = new CheckedRecursiveAction() {
1111 >            protected void realCompute() {
1112 >                CCF f = new LCCF(8);
1113 >                FailingCCF g = new LFCCF(9);
1114                  try {
1115                      invokeAll(f, g);
1116                      shouldThrow();
# Line 1026 | Line 1125 | public class CountedCompleterTest extend
1125       * invokeAll(tasks) with 1 argument throws exception if task does
1126       */
1127      public void testAbnormalInvokeAll1() {
1128 <       ForkJoinTask a =  new CheckedFJTask() {
1129 <            public void realCompute() {
1130 <                FailingCCF g = new LFCCF(null, 9);
1128 >        ForkJoinTask a = new CheckedRecursiveAction() {
1129 >            protected void realCompute() {
1130 >                FailingCCF g = new LFCCF(9);
1131                  try {
1132                      invokeAll(g);
1133                      shouldThrow();
# Line 1043 | Line 1142 | public class CountedCompleterTest extend
1142       * invokeAll(tasks) with > 2 argument throws exception if any task does
1143       */
1144      public void testAbnormalInvokeAll3() {
1145 <       ForkJoinTask a =  new CheckedFJTask() {
1146 <            public void realCompute() {
1147 <                CCF f = new LCCF(null, 8);
1148 <                FailingCCF g = new LFCCF(null, 9);
1149 <                CCF h = new LCCF(null, 7);
1145 >        ForkJoinTask a = new CheckedRecursiveAction() {
1146 >            protected void realCompute() {
1147 >                CCF f = new LCCF(8);
1148 >                FailingCCF g = new LFCCF(9);
1149 >                CCF h = new LCCF(7);
1150                  try {
1151                      invokeAll(f, g, h);
1152                      shouldThrow();
# Line 1059 | Line 1158 | public class CountedCompleterTest extend
1158      }
1159  
1160      /**
1161 <     * invokeAll(collection)  throws exception if any task does
1161 >     * invokeAll(collection) throws exception if any task does
1162       */
1163      public void testAbnormalInvokeAllCollection() {
1164 <       ForkJoinTask a =  new CheckedFJTask() {
1165 <            public void realCompute() {
1166 <                FailingCCF f = new LFCCF(null, 8);
1167 <                CCF g = new LCCF(null, 9);
1168 <                CCF h = new LCCF(null, 7);
1164 >        ForkJoinTask a = new CheckedRecursiveAction() {
1165 >            protected void realCompute() {
1166 >                FailingCCF f = new LFCCF(8);
1167 >                CCF g = new LCCF(9);
1168 >                CCF h = new LCCF(7);
1169                  HashSet set = new HashSet();
1170                  set.add(f);
1171                  set.add(g);
# Line 1086 | Line 1185 | public class CountedCompleterTest extend
1185       * and suppresses execution
1186       */
1187      public void testTryUnfork() {
1188 <       ForkJoinTask a =  new CheckedFJTask() {
1189 <            public void realCompute() {
1190 <                CCF g = new LCCF(null, 9);
1188 >        ForkJoinTask a = new CheckedRecursiveAction() {
1189 >            protected void realCompute() {
1190 >                CCF g = new LCCF(9);
1191                  assertSame(g, g.fork());
1192 <                CCF f = new LCCF(null, 8);
1192 >                CCF f = new LCCF(8);
1193                  assertSame(f, f.fork());
1194                  assertTrue(f.tryUnfork());
1195                  helpQuiesce();
# Line 1105 | Line 1204 | public class CountedCompleterTest extend
1204       * there are more tasks than threads
1205       */
1206      public void testGetSurplusQueuedTaskCount() {
1207 <       ForkJoinTask a =  new CheckedFJTask() {
1208 <            public void realCompute() {
1209 <                CCF h = new LCCF(null, 7);
1207 >        ForkJoinTask a = new CheckedRecursiveAction() {
1208 >            protected void realCompute() {
1209 >                CCF h = new LCCF(7);
1210                  assertSame(h, h.fork());
1211 <                CCF g = new LCCF(null, 9);
1211 >                CCF g = new LCCF(9);
1212                  assertSame(g, g.fork());
1213 <                CCF f = new LCCF(null, 8);
1213 >                CCF f = new LCCF(8);
1214                  assertSame(f, f.fork());
1215                  assertTrue(getSurplusQueuedTaskCount() > 0);
1216                  helpQuiesce();
# Line 1127 | Line 1226 | public class CountedCompleterTest extend
1226       * peekNextLocalTask returns most recent unexecuted task.
1227       */
1228      public void testPeekNextLocalTask() {
1229 <       ForkJoinTask a =  new CheckedFJTask() {
1230 <            public void realCompute() {
1231 <                CCF g = new LCCF(null, 9);
1229 >        ForkJoinTask a = new CheckedRecursiveAction() {
1230 >            protected void realCompute() {
1231 >                CCF g = new LCCF(9);
1232                  assertSame(g, g.fork());
1233 <                CCF f = new LCCF(null, 8);
1233 >                CCF f = new LCCF(8);
1234                  assertSame(f, f.fork());
1235                  assertSame(f, peekNextLocalTask());
1236                  assertNull(f.join());
# Line 1147 | Line 1246 | public class CountedCompleterTest extend
1246       * executing it
1247       */
1248      public void testPollNextLocalTask() {
1249 <       ForkJoinTask a =  new CheckedFJTask() {
1250 <            public void realCompute() {
1251 <                CCF g = new LCCF(null, 9);
1249 >        ForkJoinTask a = new CheckedRecursiveAction() {
1250 >            protected void realCompute() {
1251 >                CCF g = new LCCF(9);
1252                  assertSame(g, g.fork());
1253 <                CCF f = new LCCF(null, 8);
1253 >                CCF f = new LCCF(8);
1254                  assertSame(f, f.fork());
1255                  assertSame(f, pollNextLocalTask());
1256                  helpQuiesce();
# Line 1166 | Line 1265 | public class CountedCompleterTest extend
1265       * pollTask returns an unexecuted task without executing it
1266       */
1267      public void testPollTask() {
1268 <       ForkJoinTask a =  new CheckedFJTask() {
1269 <            public void realCompute() {
1270 <                CCF g = new LCCF(null, 9);
1268 >        ForkJoinTask a = new CheckedRecursiveAction() {
1269 >            protected void realCompute() {
1270 >                CCF g = new LCCF(9);
1271                  assertSame(g, g.fork());
1272 <                CCF f = new LCCF(null, 8);
1272 >                CCF f = new LCCF(8);
1273                  assertSame(f, f.fork());
1274                  assertSame(f, pollTask());
1275                  helpQuiesce();
# Line 1184 | Line 1283 | public class CountedCompleterTest extend
1283       * peekNextLocalTask returns least recent unexecuted task in async mode
1284       */
1285      public void testPeekNextLocalTaskAsync() {
1286 <       ForkJoinTask a =  new CheckedFJTask() {
1287 <            public void realCompute() {
1288 <                CCF g = new LCCF(null, 9);
1286 >        ForkJoinTask a = new CheckedRecursiveAction() {
1287 >            protected void realCompute() {
1288 >                CCF g = new LCCF(9);
1289                  assertSame(g, g.fork());
1290 <                CCF f = new LCCF(null, 8);
1290 >                CCF f = new LCCF(8);
1291                  assertSame(f, f.fork());
1292                  assertSame(g, peekNextLocalTask());
1293                  assertNull(f.join());
# Line 1205 | Line 1304 | public class CountedCompleterTest extend
1304       * executing it, in async mode
1305       */
1306      public void testPollNextLocalTaskAsync() {
1307 <       ForkJoinTask a =  new CheckedFJTask() {
1308 <            public void realCompute() {
1309 <                CCF g = new LCCF(null, 9);
1307 >        ForkJoinTask a = new CheckedRecursiveAction() {
1308 >            protected void realCompute() {
1309 >                CCF g = new LCCF(9);
1310                  assertSame(g, g.fork());
1311 <                CCF f = new LCCF(null, 8);
1311 >                CCF f = new LCCF(8);
1312                  assertSame(f, f.fork());
1313                  assertSame(g, pollNextLocalTask());
1314                  helpQuiesce();
# Line 1225 | Line 1324 | public class CountedCompleterTest extend
1324       * async mode
1325       */
1326      public void testPollTaskAsync() {
1327 <       ForkJoinTask a =  new CheckedFJTask() {
1328 <            public void realCompute() {
1329 <                CCF g = new LCCF(null, 9);
1327 >        ForkJoinTask a = new CheckedRecursiveAction() {
1328 >            protected void realCompute() {
1329 >                CCF g = new LCCF(9);
1330                  assertSame(g, g.fork());
1331 <                CCF f = new LCCF(null, 8);
1331 >                CCF f = new LCCF(8);
1332                  assertSame(f, f.fork());
1333                  assertSame(g, pollTask());
1334                  helpQuiesce();
# Line 1248 | Line 1347 | public class CountedCompleterTest extend
1347       * completed tasks; getRawResult returns null.
1348       */
1349      public void testInvokeSingleton() {
1350 <       ForkJoinTask a =  new CheckedFJTask() {
1351 <            public void realCompute() {
1352 <                CCF f = new LCCF(null, 8);
1350 >        ForkJoinTask a = new CheckedRecursiveAction() {
1351 >            protected void realCompute() {
1352 >                CCF f = new LCCF(8);
1353                  assertNull(f.invoke());
1354                  assertEquals(21, f.number);
1355                  checkCompletedNormally(f);
# Line 1264 | Line 1363 | public class CountedCompleterTest extend
1363       * completed tasks
1364       */
1365      public void testQuietlyInvokeSingleton() {
1366 <       ForkJoinTask a =  new CheckedFJTask() {
1367 <            public void realCompute() {
1368 <                CCF f = new LCCF(null, 8);
1366 >        ForkJoinTask a = new CheckedRecursiveAction() {
1367 >            protected void realCompute() {
1368 >                CCF f = new LCCF(8);
1369                  f.quietlyInvoke();
1370                  assertEquals(21, f.number);
1371                  checkCompletedNormally(f);
# Line 1278 | Line 1377 | public class CountedCompleterTest extend
1377       * join of a forked task returns when task completes
1378       */
1379      public void testForkJoinSingleton() {
1380 <       ForkJoinTask a =  new CheckedFJTask() {
1381 <            public void realCompute() {
1382 <                CCF f = new LCCF(null, 8);
1380 >        ForkJoinTask a = new CheckedRecursiveAction() {
1381 >            protected void realCompute() {
1382 >                CCF f = new LCCF(8);
1383                  assertSame(f, f.fork());
1384                  assertNull(f.join());
1385                  assertEquals(21, f.number);
# Line 1293 | Line 1392 | public class CountedCompleterTest extend
1392       * get of a forked task returns when task completes
1393       */
1394      public void testForkGetSingleton() {
1395 <       ForkJoinTask a =  new CheckedFJTask() {
1396 <            public void realCompute() throws Exception {
1397 <                CCF f = new LCCF(null, 8);
1395 >        ForkJoinTask a = new CheckedRecursiveAction() {
1396 >            protected void realCompute() throws Exception {
1397 >                CCF f = new LCCF(8);
1398                  assertSame(f, f.fork());
1399                  assertNull(f.get());
1400                  assertEquals(21, f.number);
# Line 1308 | Line 1407 | public class CountedCompleterTest extend
1407       * timed get of a forked task returns when task completes
1408       */
1409      public void testForkTimedGetSingleton() {
1410 <       ForkJoinTask a =  new CheckedFJTask() {
1411 <            public void realCompute() throws Exception {
1412 <                CCF f = new LCCF(null, 8);
1410 >        ForkJoinTask a = new CheckedRecursiveAction() {
1411 >            protected void realCompute() throws Exception {
1412 >                CCF f = new LCCF(8);
1413                  assertSame(f, f.fork());
1414                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1415                  assertEquals(21, f.number);
# Line 1323 | Line 1422 | public class CountedCompleterTest extend
1422       * timed get with null time unit throws NPE
1423       */
1424      public void testForkTimedGetNPESingleton() {
1425 <       ForkJoinTask a =  new CheckedFJTask() {
1426 <            public void realCompute() throws Exception {
1427 <                CCF f = new LCCF(null, 8);
1425 >        ForkJoinTask a = new CheckedRecursiveAction() {
1426 >            protected void realCompute() throws Exception {
1427 >                CCF f = new LCCF(8);
1428                  assertSame(f, f.fork());
1429                  try {
1430                      f.get(5L, null);
# Line 1339 | Line 1438 | public class CountedCompleterTest extend
1438       * quietlyJoin of a forked task returns when task completes
1439       */
1440      public void testForkQuietlyJoinSingleton() {
1441 <       ForkJoinTask a =  new CheckedFJTask() {
1442 <            public void realCompute() {
1443 <                CCF f = new LCCF(null, 8);
1441 >        ForkJoinTask a = new CheckedRecursiveAction() {
1442 >            protected void realCompute() {
1443 >                CCF f = new LCCF(8);
1444                  assertSame(f, f.fork());
1445                  f.quietlyJoin();
1446                  assertEquals(21, f.number);
# Line 1355 | Line 1454 | public class CountedCompleterTest extend
1454       * getQueuedTaskCount returns 0 when quiescent
1455       */
1456      public void testForkHelpQuiesceSingleton() {
1457 <       ForkJoinTask a =  new CheckedFJTask() {
1458 <            public void realCompute() {
1459 <                CCF f = new LCCF(null, 8);
1457 >        ForkJoinTask a = new CheckedRecursiveAction() {
1458 >            protected void realCompute() {
1459 >                CCF f = new LCCF(8);
1460                  assertSame(f, f.fork());
1461                  helpQuiesce();
1462                  assertEquals(0, getQueuedTaskCount());
# Line 1371 | Line 1470 | public class CountedCompleterTest extend
1470       * invoke task throws exception when task completes abnormally
1471       */
1472      public void testAbnormalInvokeSingleton() {
1473 <       ForkJoinTask a =  new CheckedFJTask() {
1474 <            public void realCompute() {
1475 <                FailingCCF f = new LFCCF(null, 8);
1473 >        ForkJoinTask a = new CheckedRecursiveAction() {
1474 >            protected void realCompute() {
1475 >                FailingCCF f = new LFCCF(8);
1476                  try {
1477                      f.invoke();
1478                      shouldThrow();
# Line 1388 | Line 1487 | public class CountedCompleterTest extend
1487       * quietlyInvoke task returns when task completes abnormally
1488       */
1489      public void testAbnormalQuietlyInvokeSingleton() {
1490 <       ForkJoinTask a =  new CheckedFJTask() {
1491 <            public void realCompute() {
1492 <                FailingCCF f = new LFCCF(null, 8);
1490 >        ForkJoinTask a = new CheckedRecursiveAction() {
1491 >            protected void realCompute() {
1492 >                FailingCCF f = new LFCCF(8);
1493                  f.quietlyInvoke();
1494                  assertTrue(f.getException() instanceof FJException);
1495                  checkCompletedAbnormally(f, f.getException());
# Line 1402 | Line 1501 | public class CountedCompleterTest extend
1501       * join of a forked task throws exception when task completes abnormally
1502       */
1503      public void testAbnormalForkJoinSingleton() {
1504 <       ForkJoinTask a =  new CheckedFJTask() {
1505 <            public void realCompute() {
1506 <                FailingCCF f = new LFCCF(null, 8);
1504 >        ForkJoinTask a = new CheckedRecursiveAction() {
1505 >            protected void realCompute() {
1506 >                FailingCCF f = new LFCCF(8);
1507                  assertSame(f, f.fork());
1508                  try {
1509                      f.join();
# Line 1420 | Line 1519 | public class CountedCompleterTest extend
1519       * get of a forked task throws exception when task completes abnormally
1520       */
1521      public void testAbnormalForkGetSingleton() {
1522 <       ForkJoinTask a =  new CheckedFJTask() {
1523 <            public void realCompute() throws Exception {
1524 <                FailingCCF f = new LFCCF(null, 8);
1522 >        ForkJoinTask a = new CheckedRecursiveAction() {
1523 >            protected void realCompute() throws Exception {
1524 >                FailingCCF f = new LFCCF(8);
1525                  assertSame(f, f.fork());
1526                  try {
1527                      f.get();
# Line 1440 | Line 1539 | public class CountedCompleterTest extend
1539       * timed get of a forked task throws exception when task completes abnormally
1540       */
1541      public void testAbnormalForkTimedGetSingleton() {
1542 <       ForkJoinTask a =  new CheckedFJTask() {
1543 <            public void realCompute() throws Exception {
1544 <                FailingCCF f = new LFCCF(null, 8);
1542 >        ForkJoinTask a = new CheckedRecursiveAction() {
1543 >            protected void realCompute() throws Exception {
1544 >                FailingCCF f = new LFCCF(8);
1545                  assertSame(f, f.fork());
1546                  try {
1547                      f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 1460 | Line 1559 | public class CountedCompleterTest extend
1559       * quietlyJoin of a forked task returns when task completes abnormally
1560       */
1561      public void testAbnormalForkQuietlyJoinSingleton() {
1562 <       ForkJoinTask a =  new CheckedFJTask() {
1563 <            public void realCompute() {
1564 <                FailingCCF f = new LFCCF(null, 8);
1562 >        ForkJoinTask a = new CheckedRecursiveAction() {
1563 >            protected void realCompute() {
1564 >                FailingCCF f = new LFCCF(8);
1565                  assertSame(f, f.fork());
1566                  f.quietlyJoin();
1567                  assertTrue(f.getException() instanceof FJException);
# Line 1475 | Line 1574 | public class CountedCompleterTest extend
1574       * invoke task throws exception when task cancelled
1575       */
1576      public void testCancelledInvokeSingleton() {
1577 <       ForkJoinTask a =  new CheckedFJTask() {
1578 <            public void realCompute() {
1579 <                CCF f = new LCCF(null, 8);
1577 >        ForkJoinTask a = new CheckedRecursiveAction() {
1578 >            protected void realCompute() {
1579 >                CCF f = new LCCF(8);
1580                  assertTrue(f.cancel(true));
1581                  try {
1582                      f.invoke();
# Line 1493 | Line 1592 | public class CountedCompleterTest extend
1592       * join of a forked task throws exception when task cancelled
1593       */
1594      public void testCancelledForkJoinSingleton() {
1595 <       ForkJoinTask a =  new CheckedFJTask() {
1596 <            public void realCompute() {
1597 <                CCF f = new LCCF(null, 8);
1595 >        ForkJoinTask a = new CheckedRecursiveAction() {
1596 >            protected void realCompute() {
1597 >                CCF f = new LCCF(8);
1598                  assertTrue(f.cancel(true));
1599                  assertSame(f, f.fork());
1600                  try {
# Line 1512 | Line 1611 | public class CountedCompleterTest extend
1611       * get of a forked task throws exception when task cancelled
1612       */
1613      public void testCancelledForkGetSingleton() {
1614 <       ForkJoinTask a =  new CheckedFJTask() {
1615 <            public void realCompute() throws Exception {
1616 <                CCF f = new LCCF(null, 8);
1614 >        ForkJoinTask a = new CheckedRecursiveAction() {
1615 >            protected void realCompute() throws Exception {
1616 >                CCF f = new LCCF(8);
1617                  assertTrue(f.cancel(true));
1618                  assertSame(f, f.fork());
1619                  try {
# Line 1531 | Line 1630 | public class CountedCompleterTest extend
1630       * timed get of a forked task throws exception when task cancelled
1631       */
1632      public void testCancelledForkTimedGetSingleton() throws Exception {
1633 <       ForkJoinTask a =  new CheckedFJTask() {
1634 <            public void realCompute() throws Exception {
1635 <                CCF f = new LCCF(null, 8);
1633 >        ForkJoinTask a = new CheckedRecursiveAction() {
1634 >            protected void realCompute() throws Exception {
1635 >                CCF f = new LCCF(8);
1636                  assertTrue(f.cancel(true));
1637                  assertSame(f, f.fork());
1638                  try {
# Line 1550 | Line 1649 | public class CountedCompleterTest extend
1649       * quietlyJoin of a forked task returns when task cancelled
1650       */
1651      public void testCancelledForkQuietlyJoinSingleton() {
1652 <       ForkJoinTask a =  new CheckedFJTask() {
1653 <            public void realCompute() {
1654 <                CCF f = new LCCF(null, 8);
1652 >        ForkJoinTask a = new CheckedRecursiveAction() {
1653 >            protected void realCompute() {
1654 >                CCF f = new LCCF(8);
1655                  assertTrue(f.cancel(true));
1656                  assertSame(f, f.fork());
1657                  f.quietlyJoin();
# Line 1565 | Line 1664 | public class CountedCompleterTest extend
1664       * invoke task throws exception after invoking completeExceptionally
1665       */
1666      public void testCompleteExceptionallySingleton() {
1667 <       ForkJoinTask a =  new CheckedFJTask() {
1668 <            public void realCompute() {
1669 <                CCF f = new LCCF(null, 8);
1670 <                f.completeExceptionally(new FJException());
1671 <                try {
1672 <                    f.invoke();
1673 <                    shouldThrow();
1674 <                } catch (FJException success) {
1576 <                    checkCompletedAbnormally(f, success);
1577 <                }
1667 >        ForkJoinTask a = new CheckedRecursiveAction() {
1668 >            protected void realCompute() {
1669 >                CCF n = new LCCF(8);
1670 >                CCF f = new LCCF(n, 8);
1671 >                FJException ex = new FJException();
1672 >                f.completeExceptionally(ex);
1673 >                f.checkCompletedExceptionally(ex);
1674 >                n.checkCompletedExceptionally(ex);
1675              }};
1676          testInvokeOnPool(singletonPool(), a);
1677      }
# Line 1583 | Line 1680 | public class CountedCompleterTest extend
1680       * invokeAll(t1, t2) invokes all task arguments
1681       */
1682      public void testInvokeAll2Singleton() {
1683 <       ForkJoinTask a =  new CheckedFJTask() {
1684 <            public void realCompute() {
1685 <                CCF f = new LCCF(null, 8);
1686 <                CCF g = new LCCF(null, 9);
1683 >        ForkJoinTask a = new CheckedRecursiveAction() {
1684 >            protected void realCompute() {
1685 >                CCF f = new LCCF(8);
1686 >                CCF g = new LCCF(9);
1687                  invokeAll(f, g);
1688                  assertEquals(21, f.number);
1689                  assertEquals(34, g.number);
# Line 1600 | Line 1697 | public class CountedCompleterTest extend
1697       * invokeAll(tasks) with 1 argument invokes task
1698       */
1699      public void testInvokeAll1Singleton() {
1700 <       ForkJoinTask a =  new CheckedFJTask() {
1701 <            public void realCompute() {
1702 <                CCF f = new LCCF(null, 8);
1700 >        ForkJoinTask a = new CheckedRecursiveAction() {
1701 >            protected void realCompute() {
1702 >                CCF f = new LCCF(8);
1703                  invokeAll(f);
1704                  checkCompletedNormally(f);
1705                  assertEquals(21, f.number);
# Line 1614 | Line 1711 | public class CountedCompleterTest extend
1711       * invokeAll(tasks) with > 2 argument invokes tasks
1712       */
1713      public void testInvokeAll3Singleton() {
1714 <       ForkJoinTask a =  new CheckedFJTask() {
1715 <            public void realCompute() {
1716 <                CCF f = new LCCF(null, 8);
1717 <                CCF g = new LCCF(null, 9);
1718 <                CCF h = new LCCF(null, 7);
1714 >        ForkJoinTask a = new CheckedRecursiveAction() {
1715 >            protected void realCompute() {
1716 >                CCF f = new LCCF(8);
1717 >                CCF g = new LCCF(9);
1718 >                CCF h = new LCCF(7);
1719                  invokeAll(f, g, h);
1720                  assertEquals(21, f.number);
1721                  assertEquals(34, g.number);
# Line 1634 | Line 1731 | public class CountedCompleterTest extend
1731       * invokeAll(collection) invokes all tasks in the collection
1732       */
1733      public void testInvokeAllCollectionSingleton() {
1734 <       ForkJoinTask a =  new CheckedFJTask() {
1735 <            public void realCompute() {
1736 <                CCF f = new LCCF(null, 8);
1737 <                CCF g = new LCCF(null, 9);
1738 <                CCF h = new LCCF(null, 7);
1734 >        ForkJoinTask a = new CheckedRecursiveAction() {
1735 >            protected void realCompute() {
1736 >                CCF f = new LCCF(8);
1737 >                CCF g = new LCCF(9);
1738 >                CCF h = new LCCF(7);
1739                  HashSet set = new HashSet();
1740                  set.add(f);
1741                  set.add(g);
# Line 1658 | Line 1755 | public class CountedCompleterTest extend
1755       * invokeAll(tasks) with any null task throws NPE
1756       */
1757      public void testInvokeAllNPESingleton() {
1758 <       ForkJoinTask a =  new CheckedFJTask() {
1759 <            public void realCompute() {
1760 <                CCF f = new LCCF(null, 8);
1761 <                CCF g = new LCCF(null, 9);
1758 >        ForkJoinTask a = new CheckedRecursiveAction() {
1759 >            protected void realCompute() {
1760 >                CCF f = new LCCF(8);
1761 >                CCF g = new LCCF(9);
1762                  CCF h = null;
1763                  try {
1764                      invokeAll(f, g, h);
# Line 1675 | Line 1772 | public class CountedCompleterTest extend
1772       * invokeAll(t1, t2) throw exception if any task does
1773       */
1774      public void testAbnormalInvokeAll2Singleton() {
1775 <       ForkJoinTask a =  new CheckedFJTask() {
1776 <            public void realCompute() {
1777 <                CCF f = new LCCF(null, 8);
1778 <                FailingCCF g = new LFCCF(null, 9);
1775 >        ForkJoinTask a = new CheckedRecursiveAction() {
1776 >            protected void realCompute() {
1777 >                CCF f = new LCCF(8);
1778 >                FailingCCF g = new LFCCF(9);
1779                  try {
1780                      invokeAll(f, g);
1781                      shouldThrow();
# Line 1693 | Line 1790 | public class CountedCompleterTest extend
1790       * invokeAll(tasks) with 1 argument throws exception if task does
1791       */
1792      public void testAbnormalInvokeAll1Singleton() {
1793 <       ForkJoinTask a =  new CheckedFJTask() {
1794 <            public void realCompute() {
1795 <                FailingCCF g = new LFCCF(null, 9);
1793 >        ForkJoinTask a = new CheckedRecursiveAction() {
1794 >            protected void realCompute() {
1795 >                FailingCCF g = new LFCCF(9);
1796                  try {
1797                      invokeAll(g);
1798                      shouldThrow();
# Line 1710 | Line 1807 | public class CountedCompleterTest extend
1807       * invokeAll(tasks) with > 2 argument throws exception if any task does
1808       */
1809      public void testAbnormalInvokeAll3Singleton() {
1810 <       ForkJoinTask a =  new CheckedFJTask() {
1811 <            public void realCompute() {
1812 <                CCF f = new LCCF(null, 8);
1813 <                FailingCCF g = new LFCCF(null, 9);
1814 <                CCF h = new LCCF(null, 7);
1810 >        ForkJoinTask a = new CheckedRecursiveAction() {
1811 >            protected void realCompute() {
1812 >                CCF f = new LCCF(8);
1813 >                FailingCCF g = new LFCCF(9);
1814 >                CCF h = new LCCF(7);
1815                  try {
1816                      invokeAll(f, g, h);
1817                      shouldThrow();
# Line 1726 | Line 1823 | public class CountedCompleterTest extend
1823      }
1824  
1825      /**
1826 <     * invokeAll(collection)  throws exception if any task does
1826 >     * invokeAll(collection) throws exception if any task does
1827       */
1828      public void testAbnormalInvokeAllCollectionSingleton() {
1829 <       ForkJoinTask a =  new CheckedFJTask() {
1830 <            public void realCompute() {
1831 <                FailingCCF f = new LFCCF(null, 8);
1832 <                CCF g = new LCCF(null, 9);
1833 <                CCF h = new LCCF(null, 7);
1829 >        ForkJoinTask a = new CheckedRecursiveAction() {
1830 >            protected void realCompute() {
1831 >                FailingCCF f = new LFCCF(8);
1832 >                CCF g = new LCCF(9);
1833 >                CCF h = new LCCF(7);
1834                  HashSet set = new HashSet();
1835                  set.add(f);
1836                  set.add(g);
# Line 1748 | Line 1845 | public class CountedCompleterTest extend
1845          testInvokeOnPool(singletonPool(), a);
1846      }
1847  
1848 +    /** CountedCompleter class javadoc code sample, version 1. */
1849 +    public static <E> void forEach1(E[] array, Consumer<E> action) {
1850 +        class Task extends CountedCompleter<Void> {
1851 +            final int lo, hi;
1852 +            Task(Task parent, int lo, int hi) {
1853 +                super(parent); this.lo = lo; this.hi = hi;
1854 +            }
1855 +
1856 +            public void compute() {
1857 +                if (hi - lo >= 2) {
1858 +                    int mid = (lo + hi) >>> 1;
1859 +                    // must set pending count before fork
1860 +                    setPendingCount(2);
1861 +                    new Task(this, mid, hi).fork(); // right child
1862 +                    new Task(this, lo, mid).fork(); // left child
1863 +                }
1864 +                else if (hi > lo)
1865 +                    action.accept(array[lo]);
1866 +                tryComplete();
1867 +            }
1868 +        }
1869 +        new Task(null, 0, array.length).invoke();
1870 +    }
1871 +
1872 +    /** CountedCompleter class javadoc code sample, version 2. */
1873 +    public static <E> void forEach2(E[] array, Consumer<E> action) {
1874 +        class Task extends CountedCompleter<Void> {
1875 +            final int lo, hi;
1876 +            Task(Task parent, int lo, int hi) {
1877 +                super(parent); this.lo = lo; this.hi = hi;
1878 +            }
1879 +
1880 +            public void compute() {
1881 +                if (hi - lo >= 2) {
1882 +                    int mid = (lo + hi) >>> 1;
1883 +                    setPendingCount(1); // not off by one !
1884 +                    new Task(this, mid, hi).fork(); // right child
1885 +                    new Task(this, lo, mid).compute(); // direct invoke
1886 +                } else {
1887 +                    if (hi > lo)
1888 +                        action.accept(array[lo]);
1889 +                    tryComplete();
1890 +                }
1891 +            }
1892 +        }
1893 +        new Task(null, 0, array.length).invoke();
1894 +    }
1895 +
1896 +    /** CountedCompleter class javadoc code sample, version 3. */
1897 +    public static <E> void forEach3(E[] array, Consumer<E> action) {
1898 +        class Task extends CountedCompleter<Void> {
1899 +            final int lo, hi;
1900 +            Task(Task parent, int lo, int hi) {
1901 +                super(parent); this.lo = lo; this.hi = hi;
1902 +            }
1903 +
1904 +            public void compute() {
1905 +                int n = hi - lo;
1906 +                for (; n >= 2; n /= 2) {
1907 +                    addToPendingCount(1);
1908 +                    new Task(this, lo + n/2, lo + n).fork();
1909 +                }
1910 +                if (n > 0)
1911 +                    action.accept(array[lo]);
1912 +                propagateCompletion();
1913 +            }
1914 +        }
1915 +        new Task(null, 0, array.length).invoke();
1916 +    }
1917 +
1918 +    /** CountedCompleter class javadoc code sample, version 4. */
1919 +    public static <E> void forEach4(E[] array, Consumer<E> action) {
1920 +        class Task extends CountedCompleter<Void> {
1921 +            final int lo, hi;
1922 +            Task(Task parent, int lo, int hi) {
1923 +                super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo));
1924 +                this.lo = lo; this.hi = hi;
1925 +            }
1926 +
1927 +            public void compute() {
1928 +                for (int n = hi - lo; n >= 2; n /= 2)
1929 +                    new Task(this, lo + n/2, lo + n).fork();
1930 +                action.accept(array[lo]);
1931 +                propagateCompletion();
1932 +            }
1933 +        }
1934 +        if (array.length > 0)
1935 +            new Task(null, 0, array.length).invoke();
1936 +    }
1937 +
1938 +    void testRecursiveDecomposition(
1939 +        BiConsumer<Integer[], Consumer<Integer>> action) {
1940 +        int n = ThreadLocalRandom.current().nextInt(8);
1941 +        Integer[] a = new Integer[n];
1942 +        for (int i = 0; i < n; i++) a[i] = i + 1;
1943 +        AtomicInteger ai = new AtomicInteger(0);
1944 +        action.accept(a, (x) -> ai.addAndGet(x));
1945 +        assertEquals(n * (n + 1) / 2, ai.get());
1946 +    }
1947 +
1948 +    /**
1949 +     * Variants of divide-by-two recursive decomposition into leaf tasks,
1950 +     * as described in the CountedCompleter class javadoc code samples
1951 +     */
1952 +    public void testRecursiveDecomposition() {
1953 +        testRecursiveDecomposition(CountedCompleterTest::forEach1);
1954 +        testRecursiveDecomposition(CountedCompleterTest::forEach2);
1955 +        testRecursiveDecomposition(CountedCompleterTest::forEach3);
1956 +        testRecursiveDecomposition(CountedCompleterTest::forEach4);
1957 +    }
1958 +
1959   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines