ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.57
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.56: +2 -2 lines
Log Message:
use diamond <> pervasively

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.30 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6 jsr166 1.14
7 jsr166 1.50 import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 jsr166 1.40
9     import java.util.Arrays;
10     import java.util.HashSet;
11 jsr166 1.15 import java.util.concurrent.CancellationException;
12     import java.util.concurrent.ExecutionException;
13     import java.util.concurrent.ForkJoinPool;
14 jsr166 1.27 import java.util.concurrent.ForkJoinTask;
15 jsr166 1.15 import java.util.concurrent.ForkJoinWorkerThread;
16     import java.util.concurrent.RecursiveAction;
17 jsr166 1.40 import java.util.concurrent.SynchronousQueue;
18 jsr166 1.34 import java.util.concurrent.ThreadLocalRandom;
19 jsr166 1.40 import java.util.concurrent.TimeoutException;
20    
21     import junit.framework.Test;
22     import junit.framework.TestSuite;
23 dl 1.1
24     public class RecursiveActionTest extends JSR166TestCase {
25    
26     public static void main(String[] args) {
27 jsr166 1.44 main(suite(), args);
28 dl 1.1 }
29 jsr166 1.13
30 dl 1.1 public static Test suite() {
31 jsr166 1.9 return new TestSuite(RecursiveActionTest.class);
32 dl 1.1 }
33    
34 jsr166 1.14 private static ForkJoinPool mainPool() {
35     return new ForkJoinPool();
36     }
37    
38     private static ForkJoinPool singletonPool() {
39     return new ForkJoinPool(1);
40     }
41    
42     private static ForkJoinPool asyncSingletonPool() {
43     return new ForkJoinPool(1,
44     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
45     null, true);
46     }
47    
48     private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
49 jsr166 1.45 try (PoolCleaner cleaner = cleaner(pool)) {
50 jsr166 1.19 checkNotDone(a);
51 jsr166 1.18
52     assertNull(pool.invoke(a));
53    
54 jsr166 1.19 checkCompletedNormally(a);
55 jsr166 1.14 }
56     }
57 dl 1.1
58 jsr166 1.19 void checkNotDone(RecursiveAction a) {
59     assertFalse(a.isDone());
60     assertFalse(a.isCompletedNormally());
61     assertFalse(a.isCompletedAbnormally());
62     assertFalse(a.isCancelled());
63     assertNull(a.getException());
64     assertNull(a.getRawResult());
65    
66 jsr166 1.27 if (! ForkJoinTask.inForkJoinPool()) {
67 jsr166 1.19 Thread.currentThread().interrupt();
68     try {
69     a.get();
70     shouldThrow();
71     } catch (InterruptedException success) {
72     } catch (Throwable fail) { threadUnexpectedException(fail); }
73    
74     Thread.currentThread().interrupt();
75     try {
76 jsr166 1.50 a.get(randomTimeout(), randomTimeUnit());
77 jsr166 1.19 shouldThrow();
78     } catch (InterruptedException success) {
79     } catch (Throwable fail) { threadUnexpectedException(fail); }
80     }
81    
82     try {
83 jsr166 1.50 a.get(randomExpiredTimeout(), randomTimeUnit());
84 jsr166 1.19 shouldThrow();
85     } catch (TimeoutException success) {
86     } catch (Throwable fail) { threadUnexpectedException(fail); }
87     }
88    
89     void checkCompletedNormally(RecursiveAction a) {
90     assertTrue(a.isDone());
91     assertFalse(a.isCancelled());
92     assertTrue(a.isCompletedNormally());
93     assertFalse(a.isCompletedAbnormally());
94     assertNull(a.getException());
95     assertNull(a.getRawResult());
96     assertNull(a.join());
97 jsr166 1.22 assertFalse(a.cancel(false));
98     assertFalse(a.cancel(true));
99 jsr166 1.55
100     Object v1 = null, v2 = null;
101 jsr166 1.19 try {
102 jsr166 1.55 v1 = a.get();
103     v2 = a.get(randomTimeout(), randomTimeUnit());
104 jsr166 1.52 } catch (Throwable fail) { threadUnexpectedException(fail); }
105 jsr166 1.55 assertNull(v1);
106     assertNull(v2);
107 jsr166 1.19 }
108    
109     void checkCancelled(RecursiveAction a) {
110     assertTrue(a.isDone());
111     assertTrue(a.isCancelled());
112     assertFalse(a.isCompletedNormally());
113     assertTrue(a.isCompletedAbnormally());
114     assertTrue(a.getException() instanceof CancellationException);
115     assertNull(a.getRawResult());
116    
117     try {
118     a.join();
119     shouldThrow();
120     } catch (CancellationException success) {
121     } catch (Throwable fail) { threadUnexpectedException(fail); }
122    
123     try {
124     a.get();
125     shouldThrow();
126     } catch (CancellationException success) {
127     } catch (Throwable fail) { threadUnexpectedException(fail); }
128    
129     try {
130 jsr166 1.50 a.get(randomTimeout(), randomTimeUnit());
131 jsr166 1.19 shouldThrow();
132     } catch (CancellationException success) {
133     } catch (Throwable fail) { threadUnexpectedException(fail); }
134     }
135    
136 jsr166 1.21 void checkCompletedAbnormally(RecursiveAction a, Throwable t) {
137 jsr166 1.19 assertTrue(a.isDone());
138     assertFalse(a.isCancelled());
139     assertFalse(a.isCompletedNormally());
140     assertTrue(a.isCompletedAbnormally());
141 dl 1.29 assertSame(t.getClass(), a.getException().getClass());
142 jsr166 1.19 assertNull(a.getRawResult());
143 jsr166 1.22 assertFalse(a.cancel(false));
144     assertFalse(a.cancel(true));
145 jsr166 1.19
146     try {
147     a.join();
148     shouldThrow();
149     } catch (Throwable expected) {
150 dl 1.29 assertSame(expected.getClass(), t.getClass());
151 jsr166 1.19 }
152    
153     try {
154     a.get();
155     shouldThrow();
156     } catch (ExecutionException success) {
157 dl 1.29 assertSame(t.getClass(), success.getCause().getClass());
158 jsr166 1.19 } catch (Throwable fail) { threadUnexpectedException(fail); }
159    
160     try {
161 jsr166 1.50 a.get(randomTimeout(), randomTimeUnit());
162 jsr166 1.19 shouldThrow();
163     } catch (ExecutionException success) {
164 dl 1.29 assertSame(t.getClass(), success.getCause().getClass());
165 jsr166 1.19 } catch (Throwable fail) { threadUnexpectedException(fail); }
166     }
167    
168 dl 1.29 public static final class FJException extends RuntimeException {
169     public FJException() { super(); }
170     public FJException(Throwable cause) { super(cause); }
171 dl 1.1 }
172    
173 jsr166 1.49 /** A simple recursive action for testing. */
174 jsr166 1.18 final class FibAction extends CheckedRecursiveAction {
175 dl 1.1 final int number;
176     int result;
177     FibAction(int n) { number = n; }
178 jsr166 1.39 protected void realCompute() {
179 dl 1.1 int n = number;
180     if (n <= 1)
181     result = n;
182     else {
183     FibAction f1 = new FibAction(n - 1);
184     FibAction f2 = new FibAction(n - 2);
185     invokeAll(f1, f2);
186     result = f1.result + f2.result;
187     }
188     }
189     }
190    
191 jsr166 1.49 /** A recursive action failing in base case. */
192 jsr166 1.2 static final class FailingFibAction extends RecursiveAction {
193 dl 1.1 final int number;
194     int result;
195     FailingFibAction(int n) { number = n; }
196     public void compute() {
197     int n = number;
198     if (n <= 1)
199     throw new FJException();
200     else {
201     FailingFibAction f1 = new FailingFibAction(n - 1);
202     FailingFibAction f2 = new FailingFibAction(n - 2);
203     invokeAll(f1, f2);
204     result = f1.result + f2.result;
205     }
206     }
207     }
208    
209 jsr166 1.2 /**
210 dl 1.1 * invoke returns when task completes normally.
211     * isCompletedAbnormally and isCancelled return false for normally
212     * completed tasks. getRawResult of a RecursiveAction returns null;
213     */
214     public void testInvoke() {
215 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
216 jsr166 1.39 protected void realCompute() {
217 jsr166 1.7 FibAction f = new FibAction(8);
218 jsr166 1.18 assertNull(f.invoke());
219     assertEquals(21, f.result);
220 jsr166 1.19 checkCompletedNormally(f);
221 jsr166 1.7 }};
222 jsr166 1.14 testInvokeOnPool(mainPool(), a);
223 dl 1.1 }
224    
225 jsr166 1.2 /**
226 dl 1.1 * quietlyInvoke task returns when task completes normally.
227     * isCompletedAbnormally and isCancelled return false for normally
228     * completed tasks
229     */
230     public void testQuietlyInvoke() {
231 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
232 jsr166 1.39 protected void realCompute() {
233 jsr166 1.7 FibAction f = new FibAction(8);
234     f.quietlyInvoke();
235 jsr166 1.18 assertEquals(21, f.result);
236 jsr166 1.19 checkCompletedNormally(f);
237 jsr166 1.7 }};
238 jsr166 1.14 testInvokeOnPool(mainPool(), a);
239 dl 1.1 }
240    
241 jsr166 1.2 /**
242 dl 1.1 * join of a forked task returns when task completes
243     */
244     public void testForkJoin() {
245 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
246 jsr166 1.39 protected void realCompute() {
247 jsr166 1.7 FibAction f = new FibAction(8);
248 jsr166 1.18 assertSame(f, f.fork());
249     assertNull(f.join());
250     assertEquals(21, f.result);
251 jsr166 1.19 checkCompletedNormally(f);
252 jsr166 1.7 }};
253 jsr166 1.14 testInvokeOnPool(mainPool(), a);
254 dl 1.1 }
255    
256 jsr166 1.2 /**
257 jsr166 1.25 * join/quietlyJoin of a forked task succeeds in the presence of interrupts
258 jsr166 1.24 */
259     public void testJoinIgnoresInterrupts() {
260     RecursiveAction a = new CheckedRecursiveAction() {
261 jsr166 1.39 protected void realCompute() {
262 jsr166 1.24 FibAction f = new FibAction(8);
263 jsr166 1.47 final Thread currentThread = Thread.currentThread();
264 jsr166 1.24
265     // test join()
266     assertSame(f, f.fork());
267 jsr166 1.47 currentThread.interrupt();
268 jsr166 1.24 assertNull(f.join());
269 jsr166 1.25 Thread.interrupted();
270 jsr166 1.24 assertEquals(21, f.result);
271     checkCompletedNormally(f);
272    
273 dl 1.32 f = new FibAction(8);
274 jsr166 1.24 f.cancel(true);
275     assertSame(f, f.fork());
276 jsr166 1.47 currentThread.interrupt();
277 jsr166 1.24 try {
278     f.join();
279     shouldThrow();
280     } catch (CancellationException success) {
281 jsr166 1.25 Thread.interrupted();
282 jsr166 1.24 checkCancelled(f);
283     }
284    
285 dl 1.32 f = new FibAction(8);
286 jsr166 1.24 f.completeExceptionally(new FJException());
287     assertSame(f, f.fork());
288 jsr166 1.47 currentThread.interrupt();
289 jsr166 1.24 try {
290     f.join();
291     shouldThrow();
292     } catch (FJException success) {
293 jsr166 1.25 Thread.interrupted();
294 jsr166 1.24 checkCompletedAbnormally(f, success);
295     }
296    
297     // test quietlyJoin()
298 dl 1.32 f = new FibAction(8);
299 jsr166 1.24 assertSame(f, f.fork());
300 jsr166 1.47 currentThread.interrupt();
301 jsr166 1.24 f.quietlyJoin();
302 jsr166 1.25 Thread.interrupted();
303 jsr166 1.24 assertEquals(21, f.result);
304     checkCompletedNormally(f);
305    
306 dl 1.32 f = new FibAction(8);
307 jsr166 1.24 f.cancel(true);
308     assertSame(f, f.fork());
309 jsr166 1.47 currentThread.interrupt();
310 jsr166 1.24 f.quietlyJoin();
311 jsr166 1.25 Thread.interrupted();
312 jsr166 1.24 checkCancelled(f);
313    
314 dl 1.32 f = new FibAction(8);
315 jsr166 1.24 f.completeExceptionally(new FJException());
316     assertSame(f, f.fork());
317 jsr166 1.47 currentThread.interrupt();
318 jsr166 1.24 f.quietlyJoin();
319 jsr166 1.25 Thread.interrupted();
320 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
321     }};
322     testInvokeOnPool(mainPool(), a);
323     a.reinitialize();
324     testInvokeOnPool(singletonPool(), a);
325     }
326    
327     /**
328 jsr166 1.26 * join/quietlyJoin of a forked task when not in ForkJoinPool
329     * succeeds in the presence of interrupts
330     */
331     public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
332 jsr166 1.53 final SynchronousQueue<FibAction[]> sq = new SynchronousQueue<>();
333 jsr166 1.26 RecursiveAction a = new CheckedRecursiveAction() {
334 jsr166 1.39 protected void realCompute() throws InterruptedException {
335 jsr166 1.26 FibAction[] fibActions = new FibAction[6];
336     for (int i = 0; i < fibActions.length; i++)
337     fibActions[i] = new FibAction(8);
338    
339     fibActions[1].cancel(false);
340     fibActions[2].completeExceptionally(new FJException());
341     fibActions[4].cancel(true);
342     fibActions[5].completeExceptionally(new FJException());
343    
344 jsr166 1.54 for (FibAction fibAction : fibActions)
345     fibAction.fork();
346 jsr166 1.26
347     sq.put(fibActions);
348    
349     helpQuiesce();
350     }};
351    
352     Runnable r = new CheckedRunnable() {
353     public void realRun() throws InterruptedException {
354     FibAction[] fibActions = sq.take();
355     FibAction f;
356 jsr166 1.47 final Thread currentThread = Thread.currentThread();
357 jsr166 1.26
358     // test join() ------------
359    
360     f = fibActions[0];
361 jsr166 1.27 assertFalse(ForkJoinTask.inForkJoinPool());
362 jsr166 1.47 currentThread.interrupt();
363 jsr166 1.26 assertNull(f.join());
364     assertTrue(Thread.interrupted());
365     assertEquals(21, f.result);
366     checkCompletedNormally(f);
367    
368     f = fibActions[1];
369 jsr166 1.47 currentThread.interrupt();
370 jsr166 1.26 try {
371     f.join();
372     shouldThrow();
373     } catch (CancellationException success) {
374     assertTrue(Thread.interrupted());
375     checkCancelled(f);
376     }
377    
378     f = fibActions[2];
379 jsr166 1.47 currentThread.interrupt();
380 jsr166 1.26 try {
381     f.join();
382     shouldThrow();
383     } catch (FJException success) {
384     assertTrue(Thread.interrupted());
385     checkCompletedAbnormally(f, success);
386     }
387    
388     // test quietlyJoin() ---------
389    
390     f = fibActions[3];
391 jsr166 1.47 currentThread.interrupt();
392 jsr166 1.26 f.quietlyJoin();
393     assertTrue(Thread.interrupted());
394     assertEquals(21, f.result);
395     checkCompletedNormally(f);
396    
397     f = fibActions[4];
398 jsr166 1.47 currentThread.interrupt();
399 jsr166 1.26 f.quietlyJoin();
400     assertTrue(Thread.interrupted());
401     checkCancelled(f);
402    
403     f = fibActions[5];
404 jsr166 1.47 currentThread.interrupt();
405 jsr166 1.26 f.quietlyJoin();
406     assertTrue(Thread.interrupted());
407     assertTrue(f.getException() instanceof FJException);
408     checkCompletedAbnormally(f, f.getException());
409     }};
410    
411     Thread t;
412    
413     t = newStartedThread(r);
414     testInvokeOnPool(mainPool(), a);
415 jsr166 1.46 awaitTermination(t);
416 jsr166 1.26
417     a.reinitialize();
418     t = newStartedThread(r);
419     testInvokeOnPool(singletonPool(), a);
420 jsr166 1.46 awaitTermination(t);
421 jsr166 1.26 }
422    
423     /**
424 dl 1.1 * get of a forked task returns when task completes
425     */
426     public void testForkGet() {
427 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
428 jsr166 1.39 protected void realCompute() throws Exception {
429 jsr166 1.18 FibAction f = new FibAction(8);
430     assertSame(f, f.fork());
431     assertNull(f.get());
432     assertEquals(21, f.result);
433 jsr166 1.19 checkCompletedNormally(f);
434 jsr166 1.7 }};
435 jsr166 1.14 testInvokeOnPool(mainPool(), a);
436 dl 1.1 }
437    
438 jsr166 1.2 /**
439 dl 1.1 * timed get of a forked task returns when task completes
440     */
441     public void testForkTimedGet() {
442 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
443 jsr166 1.39 protected void realCompute() throws Exception {
444 jsr166 1.18 FibAction f = new FibAction(8);
445     assertSame(f, f.fork());
446 jsr166 1.50 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
447 jsr166 1.18 assertEquals(21, f.result);
448 jsr166 1.19 checkCompletedNormally(f);
449 jsr166 1.7 }};
450 jsr166 1.14 testInvokeOnPool(mainPool(), a);
451 dl 1.1 }
452    
453 jsr166 1.2 /**
454 dl 1.1 * timed get with null time unit throws NPE
455     */
456     public void testForkTimedGetNPE() {
457 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
458 jsr166 1.39 protected void realCompute() throws Exception {
459 jsr166 1.18 FibAction f = new FibAction(8);
460     assertSame(f, f.fork());
461 jsr166 1.8 try {
462 jsr166 1.50 f.get(randomTimeout(), null);
463 jsr166 1.8 shouldThrow();
464 jsr166 1.18 } catch (NullPointerException success) {}
465 jsr166 1.8 }};
466 jsr166 1.14 testInvokeOnPool(mainPool(), a);
467 dl 1.1 }
468    
469 jsr166 1.2 /**
470 dl 1.1 * quietlyJoin of a forked task returns when task completes
471     */
472     public void testForkQuietlyJoin() {
473 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
474 jsr166 1.39 protected void realCompute() {
475 jsr166 1.7 FibAction f = new FibAction(8);
476 jsr166 1.18 assertSame(f, f.fork());
477 jsr166 1.7 f.quietlyJoin();
478 jsr166 1.18 assertEquals(21, f.result);
479 jsr166 1.19 checkCompletedNormally(f);
480 jsr166 1.7 }};
481 jsr166 1.14 testInvokeOnPool(mainPool(), a);
482 dl 1.1 }
483    
484 jsr166 1.2 /**
485 dl 1.1 * helpQuiesce returns when tasks are complete.
486     * getQueuedTaskCount returns 0 when quiescent
487     */
488     public void testForkHelpQuiesce() {
489 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
490 jsr166 1.39 protected void realCompute() {
491 jsr166 1.7 FibAction f = new FibAction(8);
492 jsr166 1.18 assertSame(f, f.fork());
493 jsr166 1.31 helpQuiesce();
494 dl 1.41 while (!f.isDone()) // wait out race
495     ;
496 jsr166 1.18 assertEquals(21, f.result);
497     assertEquals(0, getQueuedTaskCount());
498 jsr166 1.19 checkCompletedNormally(f);
499 jsr166 1.7 }};
500 jsr166 1.14 testInvokeOnPool(mainPool(), a);
501 dl 1.1 }
502    
503 jsr166 1.2 /**
504 dl 1.1 * invoke task throws exception when task completes abnormally
505     */
506     public void testAbnormalInvoke() {
507 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
508 jsr166 1.39 protected void realCompute() {
509 jsr166 1.18 FailingFibAction f = new FailingFibAction(8);
510 jsr166 1.7 try {
511     f.invoke();
512     shouldThrow();
513 jsr166 1.19 } catch (FJException success) {
514 jsr166 1.21 checkCompletedAbnormally(f, success);
515 jsr166 1.19 }
516 jsr166 1.7 }};
517 jsr166 1.14 testInvokeOnPool(mainPool(), a);
518 dl 1.1 }
519    
520 jsr166 1.2 /**
521 jsr166 1.4 * quietlyInvoke task returns when task completes abnormally
522 dl 1.1 */
523     public void testAbnormalQuietlyInvoke() {
524 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
525 jsr166 1.39 protected void realCompute() {
526 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
527     f.quietlyInvoke();
528 jsr166 1.19 assertTrue(f.getException() instanceof FJException);
529 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
530 jsr166 1.7 }};
531 jsr166 1.14 testInvokeOnPool(mainPool(), a);
532 dl 1.1 }
533    
534 jsr166 1.2 /**
535 dl 1.1 * join of a forked task throws exception when task completes abnormally
536     */
537     public void testAbnormalForkJoin() {
538 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
539 jsr166 1.39 protected void realCompute() {
540 jsr166 1.18 FailingFibAction f = new FailingFibAction(8);
541     assertSame(f, f.fork());
542 jsr166 1.7 try {
543     f.join();
544     shouldThrow();
545 jsr166 1.19 } catch (FJException success) {
546 jsr166 1.21 checkCompletedAbnormally(f, success);
547 jsr166 1.19 }
548 jsr166 1.7 }};
549 jsr166 1.14 testInvokeOnPool(mainPool(), a);
550 dl 1.1 }
551    
552 jsr166 1.2 /**
553 dl 1.1 * get of a forked task throws exception when task completes abnormally
554     */
555     public void testAbnormalForkGet() {
556 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
557 jsr166 1.39 protected void realCompute() throws Exception {
558 jsr166 1.18 FailingFibAction f = new FailingFibAction(8);
559     assertSame(f, f.fork());
560 jsr166 1.7 try {
561     f.get();
562     shouldThrow();
563 jsr166 1.19 } catch (ExecutionException success) {
564 jsr166 1.21 Throwable cause = success.getCause();
565     assertTrue(cause instanceof FJException);
566     checkCompletedAbnormally(f, cause);
567 jsr166 1.19 }
568 jsr166 1.7 }};
569 jsr166 1.14 testInvokeOnPool(mainPool(), a);
570 dl 1.1 }
571    
572 jsr166 1.2 /**
573 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
574     */
575     public void testAbnormalForkTimedGet() {
576 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
577 jsr166 1.39 protected void realCompute() throws Exception {
578 jsr166 1.18 FailingFibAction f = new FailingFibAction(8);
579     assertSame(f, f.fork());
580 jsr166 1.7 try {
581 jsr166 1.50 f.get(LONG_DELAY_MS, MILLISECONDS);
582 jsr166 1.7 shouldThrow();
583 jsr166 1.19 } catch (ExecutionException success) {
584 jsr166 1.21 Throwable cause = success.getCause();
585     assertTrue(cause instanceof FJException);
586     checkCompletedAbnormally(f, cause);
587 jsr166 1.19 }
588 jsr166 1.7 }};
589 jsr166 1.14 testInvokeOnPool(mainPool(), a);
590 dl 1.1 }
591    
592 jsr166 1.2 /**
593 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
594     */
595     public void testAbnormalForkQuietlyJoin() {
596 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
597 jsr166 1.39 protected void realCompute() {
598 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
599 jsr166 1.18 assertSame(f, f.fork());
600 jsr166 1.7 f.quietlyJoin();
601 jsr166 1.18 assertTrue(f.getException() instanceof FJException);
602 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
603 jsr166 1.7 }};
604 jsr166 1.14 testInvokeOnPool(mainPool(), a);
605 dl 1.1 }
606    
607 jsr166 1.2 /**
608 dl 1.1 * invoke task throws exception when task cancelled
609     */
610     public void testCancelledInvoke() {
611 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
612 jsr166 1.39 protected void realCompute() {
613 jsr166 1.18 FibAction f = new FibAction(8);
614     assertTrue(f.cancel(true));
615 jsr166 1.7 try {
616     f.invoke();
617     shouldThrow();
618 jsr166 1.19 } catch (CancellationException success) {
619     checkCancelled(f);
620     }
621 jsr166 1.7 }};
622 jsr166 1.14 testInvokeOnPool(mainPool(), a);
623 dl 1.1 }
624    
625 jsr166 1.2 /**
626 dl 1.1 * join of a forked task throws exception when task cancelled
627     */
628     public void testCancelledForkJoin() {
629 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
630 jsr166 1.39 protected void realCompute() {
631 jsr166 1.18 FibAction f = new FibAction(8);
632     assertTrue(f.cancel(true));
633     assertSame(f, f.fork());
634 jsr166 1.7 try {
635     f.join();
636     shouldThrow();
637 jsr166 1.19 } catch (CancellationException success) {
638     checkCancelled(f);
639     }
640 jsr166 1.7 }};
641 jsr166 1.14 testInvokeOnPool(mainPool(), a);
642 dl 1.1 }
643    
644 jsr166 1.2 /**
645 dl 1.1 * get of a forked task throws exception when task cancelled
646     */
647     public void testCancelledForkGet() {
648 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
649 jsr166 1.39 protected void realCompute() throws Exception {
650 jsr166 1.18 FibAction f = new FibAction(8);
651     assertTrue(f.cancel(true));
652     assertSame(f, f.fork());
653 jsr166 1.7 try {
654     f.get();
655     shouldThrow();
656 jsr166 1.19 } catch (CancellationException success) {
657     checkCancelled(f);
658     }
659 jsr166 1.7 }};
660 jsr166 1.14 testInvokeOnPool(mainPool(), a);
661 dl 1.1 }
662    
663 jsr166 1.2 /**
664 dl 1.1 * timed get of a forked task throws exception when task cancelled
665     */
666     public void testCancelledForkTimedGet() {
667 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
668 jsr166 1.39 protected void realCompute() throws Exception {
669 jsr166 1.18 FibAction f = new FibAction(8);
670     assertTrue(f.cancel(true));
671     assertSame(f, f.fork());
672 jsr166 1.7 try {
673 jsr166 1.50 f.get(LONG_DELAY_MS, MILLISECONDS);
674 jsr166 1.7 shouldThrow();
675 jsr166 1.19 } catch (CancellationException success) {
676     checkCancelled(f);
677     }
678 jsr166 1.7 }};
679 jsr166 1.14 testInvokeOnPool(mainPool(), a);
680 dl 1.1 }
681    
682 jsr166 1.2 /**
683 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
684     */
685     public void testCancelledForkQuietlyJoin() {
686 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
687 jsr166 1.39 protected void realCompute() {
688 jsr166 1.7 FibAction f = new FibAction(8);
689 jsr166 1.18 assertTrue(f.cancel(true));
690     assertSame(f, f.fork());
691 jsr166 1.7 f.quietlyJoin();
692 jsr166 1.19 checkCancelled(f);
693 jsr166 1.7 }};
694 jsr166 1.14 testInvokeOnPool(mainPool(), a);
695 dl 1.1 }
696    
697     /**
698     * getPool of executing task returns its pool
699     */
700     public void testGetPool() {
701 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
702 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
703 jsr166 1.39 protected void realCompute() {
704 jsr166 1.18 assertSame(mainPool, getPool());
705 jsr166 1.7 }};
706 jsr166 1.14 testInvokeOnPool(mainPool, a);
707 dl 1.1 }
708    
709     /**
710     * getPool of non-FJ task returns null
711     */
712     public void testGetPool2() {
713 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
714 jsr166 1.39 protected void realCompute() {
715 jsr166 1.18 assertNull(getPool());
716 jsr166 1.7 }};
717 jsr166 1.16 assertNull(a.invoke());
718 dl 1.1 }
719    
720     /**
721     * inForkJoinPool of executing task returns true
722     */
723     public void testInForkJoinPool() {
724 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
725 jsr166 1.39 protected void realCompute() {
726 jsr166 1.18 assertTrue(inForkJoinPool());
727 jsr166 1.7 }};
728 jsr166 1.14 testInvokeOnPool(mainPool(), a);
729 dl 1.1 }
730    
731     /**
732     * inForkJoinPool of non-FJ task returns false
733     */
734     public void testInForkJoinPool2() {
735 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
736 jsr166 1.39 protected void realCompute() {
737 jsr166 1.18 assertFalse(inForkJoinPool());
738 jsr166 1.7 }};
739 jsr166 1.16 assertNull(a.invoke());
740 dl 1.1 }
741    
742     /**
743     * getPool of current thread in pool returns its pool
744     */
745     public void testWorkerGetPool() {
746 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
747 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
748 jsr166 1.39 protected void realCompute() {
749 jsr166 1.7 ForkJoinWorkerThread w =
750 jsr166 1.14 (ForkJoinWorkerThread) Thread.currentThread();
751 jsr166 1.18 assertSame(mainPool, w.getPool());
752 jsr166 1.7 }};
753 jsr166 1.14 testInvokeOnPool(mainPool, a);
754 dl 1.1 }
755    
756     /**
757     * getPoolIndex of current thread in pool returns 0 <= value < poolSize
758     */
759     public void testWorkerGetPoolIndex() {
760 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
761 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
762 jsr166 1.39 protected void realCompute() {
763 jsr166 1.7 ForkJoinWorkerThread w =
764 jsr166 1.28 (ForkJoinWorkerThread) Thread.currentThread();
765 jsr166 1.19 assertTrue(w.getPoolIndex() >= 0);
766 dl 1.29 // pool size can shrink after assigning index, so cannot check
767     // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
768 jsr166 1.7 }};
769 jsr166 1.14 testInvokeOnPool(mainPool, a);
770 dl 1.1 }
771    
772     /**
773     * setRawResult(null) succeeds
774     */
775     public void testSetRawResult() {
776 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
777 jsr166 1.39 protected void realCompute() {
778 jsr166 1.7 setRawResult(null);
779 jsr166 1.23 assertNull(getRawResult());
780 jsr166 1.7 }};
781 jsr166 1.16 assertNull(a.invoke());
782 dl 1.1 }
783    
784 jsr166 1.2 /**
785 jsr166 1.33 * A reinitialized normally completed task may be re-invoked
786 dl 1.1 */
787     public void testReinitialize() {
788 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
789 jsr166 1.39 protected void realCompute() {
790 jsr166 1.7 FibAction f = new FibAction(8);
791 jsr166 1.19 checkNotDone(f);
792    
793     for (int i = 0; i < 3; i++) {
794     assertNull(f.invoke());
795     assertEquals(21, f.result);
796     checkCompletedNormally(f);
797     f.reinitialize();
798     checkNotDone(f);
799     }
800 jsr166 1.7 }};
801 jsr166 1.14 testInvokeOnPool(mainPool(), a);
802 dl 1.1 }
803    
804 jsr166 1.2 /**
805 jsr166 1.33 * A reinitialized abnormally completed task may be re-invoked
806     */
807     public void testReinitializeAbnormal() {
808     RecursiveAction a = new CheckedRecursiveAction() {
809 jsr166 1.39 protected void realCompute() {
810 jsr166 1.33 FailingFibAction f = new FailingFibAction(8);
811     checkNotDone(f);
812    
813     for (int i = 0; i < 3; i++) {
814     try {
815     f.invoke();
816     shouldThrow();
817     } catch (FJException success) {
818     checkCompletedAbnormally(f, success);
819     }
820     f.reinitialize();
821     checkNotDone(f);
822     }
823     }};
824     testInvokeOnPool(mainPool(), a);
825     }
826    
827     /**
828 dl 1.1 * invoke task throws exception after invoking completeExceptionally
829     */
830     public void testCompleteExceptionally() {
831 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
832 jsr166 1.39 protected void realCompute() {
833 jsr166 1.18 FibAction f = new FibAction(8);
834     f.completeExceptionally(new FJException());
835 jsr166 1.7 try {
836     f.invoke();
837     shouldThrow();
838 jsr166 1.19 } catch (FJException success) {
839 jsr166 1.21 checkCompletedAbnormally(f, success);
840 jsr166 1.19 }
841 jsr166 1.7 }};
842 jsr166 1.14 testInvokeOnPool(mainPool(), a);
843 dl 1.1 }
844    
845 jsr166 1.2 /**
846 dl 1.1 * invoke task suppresses execution invoking complete
847     */
848     public void testComplete() {
849 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
850 jsr166 1.39 protected void realCompute() {
851 jsr166 1.7 FibAction f = new FibAction(8);
852     f.complete(null);
853 jsr166 1.18 assertNull(f.invoke());
854     assertEquals(0, f.result);
855 jsr166 1.19 checkCompletedNormally(f);
856 jsr166 1.7 }};
857 jsr166 1.14 testInvokeOnPool(mainPool(), a);
858 dl 1.1 }
859    
860 jsr166 1.2 /**
861 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
862     */
863     public void testInvokeAll2() {
864 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
865 jsr166 1.39 protected void realCompute() {
866 jsr166 1.7 FibAction f = new FibAction(8);
867     FibAction g = new FibAction(9);
868     invokeAll(f, g);
869 jsr166 1.19 checkCompletedNormally(f);
870 jsr166 1.18 assertEquals(21, f.result);
871 jsr166 1.19 checkCompletedNormally(g);
872 jsr166 1.18 assertEquals(34, g.result);
873 jsr166 1.7 }};
874 jsr166 1.14 testInvokeOnPool(mainPool(), a);
875 dl 1.1 }
876    
877 jsr166 1.2 /**
878 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
879     */
880     public void testInvokeAll1() {
881 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
882 jsr166 1.39 protected void realCompute() {
883 jsr166 1.7 FibAction f = new FibAction(8);
884     invokeAll(f);
885 jsr166 1.19 checkCompletedNormally(f);
886 jsr166 1.18 assertEquals(21, f.result);
887 jsr166 1.7 }};
888 jsr166 1.14 testInvokeOnPool(mainPool(), a);
889 dl 1.1 }
890    
891 jsr166 1.2 /**
892 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
893     */
894     public void testInvokeAll3() {
895 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
896 jsr166 1.39 protected void realCompute() {
897 jsr166 1.7 FibAction f = new FibAction(8);
898     FibAction g = new FibAction(9);
899     FibAction h = new FibAction(7);
900     invokeAll(f, g, h);
901 jsr166 1.20 assertTrue(f.isDone());
902     assertTrue(g.isDone());
903     assertTrue(h.isDone());
904 jsr166 1.19 checkCompletedNormally(f);
905 jsr166 1.18 assertEquals(21, f.result);
906 jsr166 1.19 checkCompletedNormally(g);
907 jsr166 1.18 assertEquals(34, g.result);
908 jsr166 1.19 checkCompletedNormally(g);
909 jsr166 1.18 assertEquals(13, h.result);
910 jsr166 1.7 }};
911 jsr166 1.14 testInvokeOnPool(mainPool(), a);
912 dl 1.1 }
913    
914 jsr166 1.2 /**
915 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
916     */
917     public void testInvokeAllCollection() {
918 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
919 jsr166 1.39 protected void realCompute() {
920 jsr166 1.7 FibAction f = new FibAction(8);
921     FibAction g = new FibAction(9);
922     FibAction h = new FibAction(7);
923 jsr166 1.57 HashSet<ForkJoinTask<?>> set = new HashSet<>();
924 jsr166 1.7 set.add(f);
925     set.add(g);
926     set.add(h);
927     invokeAll(set);
928 jsr166 1.20 assertTrue(f.isDone());
929     assertTrue(g.isDone());
930     assertTrue(h.isDone());
931 jsr166 1.19 checkCompletedNormally(f);
932 jsr166 1.18 assertEquals(21, f.result);
933 jsr166 1.19 checkCompletedNormally(g);
934 jsr166 1.18 assertEquals(34, g.result);
935 jsr166 1.19 checkCompletedNormally(g);
936 jsr166 1.18 assertEquals(13, h.result);
937 jsr166 1.7 }};
938 jsr166 1.14 testInvokeOnPool(mainPool(), a);
939 dl 1.1 }
940    
941 jsr166 1.2 /**
942 dl 1.1 * invokeAll(tasks) with any null task throws NPE
943     */
944     public void testInvokeAllNPE() {
945 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
946 jsr166 1.39 protected void realCompute() {
947 jsr166 1.18 FibAction f = new FibAction(8);
948     FibAction g = new FibAction(9);
949     FibAction h = null;
950 jsr166 1.7 try {
951     invokeAll(f, g, h);
952     shouldThrow();
953 jsr166 1.18 } catch (NullPointerException success) {}
954 jsr166 1.7 }};
955 jsr166 1.14 testInvokeOnPool(mainPool(), a);
956 dl 1.1 }
957    
958 jsr166 1.2 /**
959 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
960     */
961     public void testAbnormalInvokeAll2() {
962 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
963 jsr166 1.39 protected void realCompute() {
964 jsr166 1.18 FibAction f = new FibAction(8);
965     FailingFibAction g = new FailingFibAction(9);
966 jsr166 1.7 try {
967     invokeAll(f, g);
968     shouldThrow();
969 jsr166 1.19 } catch (FJException success) {
970 jsr166 1.21 checkCompletedAbnormally(g, success);
971 jsr166 1.19 }
972 jsr166 1.7 }};
973 jsr166 1.14 testInvokeOnPool(mainPool(), a);
974 dl 1.1 }
975    
976 jsr166 1.2 /**
977 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
978     */
979     public void testAbnormalInvokeAll1() {
980 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
981 jsr166 1.39 protected void realCompute() {
982 jsr166 1.18 FailingFibAction g = new FailingFibAction(9);
983 jsr166 1.7 try {
984     invokeAll(g);
985     shouldThrow();
986 jsr166 1.19 } catch (FJException success) {
987 jsr166 1.21 checkCompletedAbnormally(g, success);
988 jsr166 1.19 }
989 jsr166 1.7 }};
990 jsr166 1.14 testInvokeOnPool(mainPool(), a);
991 dl 1.1 }
992    
993 jsr166 1.2 /**
994 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
995     */
996     public void testAbnormalInvokeAll3() {
997 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
998 jsr166 1.39 protected void realCompute() {
999 jsr166 1.18 FibAction f = new FibAction(8);
1000     FailingFibAction g = new FailingFibAction(9);
1001     FibAction h = new FibAction(7);
1002 jsr166 1.7 try {
1003     invokeAll(f, g, h);
1004     shouldThrow();
1005 jsr166 1.19 } catch (FJException success) {
1006 jsr166 1.21 checkCompletedAbnormally(g, success);
1007 jsr166 1.19 }
1008 jsr166 1.7 }};
1009 jsr166 1.14 testInvokeOnPool(mainPool(), a);
1010 dl 1.1 }
1011    
1012 jsr166 1.2 /**
1013 jsr166 1.3 * invokeAll(collection) throws exception if any task does
1014 dl 1.1 */
1015     public void testAbnormalInvokeAllCollection() {
1016 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1017 jsr166 1.39 protected void realCompute() {
1018 jsr166 1.18 FailingFibAction f = new FailingFibAction(8);
1019     FibAction g = new FibAction(9);
1020     FibAction h = new FibAction(7);
1021 jsr166 1.57 HashSet<ForkJoinTask<?>> set = new HashSet<>();
1022 jsr166 1.18 set.add(f);
1023     set.add(g);
1024     set.add(h);
1025 jsr166 1.7 try {
1026     invokeAll(set);
1027     shouldThrow();
1028 jsr166 1.19 } catch (FJException success) {
1029 jsr166 1.21 checkCompletedAbnormally(f, success);
1030 jsr166 1.19 }
1031 jsr166 1.7 }};
1032 jsr166 1.14 testInvokeOnPool(mainPool(), a);
1033 dl 1.1 }
1034    
1035 jsr166 1.2 /**
1036 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
1037     * and suppresses execution
1038     */
1039     public void testTryUnfork() {
1040 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1041 jsr166 1.39 protected void realCompute() {
1042 jsr166 1.7 FibAction g = new FibAction(9);
1043 jsr166 1.18 assertSame(g, g.fork());
1044 jsr166 1.7 FibAction f = new FibAction(8);
1045 jsr166 1.18 assertSame(f, f.fork());
1046     assertTrue(f.tryUnfork());
1047 jsr166 1.7 helpQuiesce();
1048 jsr166 1.19 checkNotDone(f);
1049     checkCompletedNormally(g);
1050 jsr166 1.7 }};
1051 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1052 dl 1.1 }
1053    
1054 jsr166 1.2 /**
1055 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
1056     * there are more tasks than threads
1057     */
1058     public void testGetSurplusQueuedTaskCount() {
1059 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1060 jsr166 1.39 protected void realCompute() {
1061 jsr166 1.7 FibAction h = new FibAction(7);
1062 jsr166 1.18 assertSame(h, h.fork());
1063 jsr166 1.7 FibAction g = new FibAction(9);
1064 jsr166 1.18 assertSame(g, g.fork());
1065 jsr166 1.7 FibAction f = new FibAction(8);
1066 jsr166 1.18 assertSame(f, f.fork());
1067     assertTrue(getSurplusQueuedTaskCount() > 0);
1068 jsr166 1.7 helpQuiesce();
1069 jsr166 1.21 assertEquals(0, getSurplusQueuedTaskCount());
1070 jsr166 1.19 checkCompletedNormally(f);
1071     checkCompletedNormally(g);
1072     checkCompletedNormally(h);
1073 jsr166 1.7 }};
1074 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1075 dl 1.1 }
1076    
1077 jsr166 1.2 /**
1078 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
1079     */
1080     public void testPeekNextLocalTask() {
1081 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1082 jsr166 1.39 protected void realCompute() {
1083 jsr166 1.7 FibAction g = new FibAction(9);
1084 jsr166 1.18 assertSame(g, g.fork());
1085 jsr166 1.7 FibAction f = new FibAction(8);
1086 jsr166 1.18 assertSame(f, f.fork());
1087     assertSame(f, peekNextLocalTask());
1088     assertNull(f.join());
1089 jsr166 1.19 checkCompletedNormally(f);
1090 jsr166 1.7 helpQuiesce();
1091 jsr166 1.19 checkCompletedNormally(f);
1092     checkCompletedNormally(g);
1093 jsr166 1.7 }};
1094 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1095 dl 1.1 }
1096    
1097 jsr166 1.2 /**
1098 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
1099     * without executing it
1100     */
1101     public void testPollNextLocalTask() {
1102 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1103 jsr166 1.39 protected void realCompute() {
1104 jsr166 1.7 FibAction g = new FibAction(9);
1105 jsr166 1.18 assertSame(g, g.fork());
1106 jsr166 1.7 FibAction f = new FibAction(8);
1107 jsr166 1.18 assertSame(f, f.fork());
1108     assertSame(f, pollNextLocalTask());
1109 jsr166 1.7 helpQuiesce();
1110 jsr166 1.19 checkNotDone(f);
1111     checkCompletedNormally(g);
1112 jsr166 1.7 }};
1113 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1114 dl 1.1 }
1115    
1116 jsr166 1.2 /**
1117 jsr166 1.19 * pollTask returns an unexecuted task without executing it
1118 dl 1.1 */
1119     public void testPollTask() {
1120 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1121 jsr166 1.39 protected void realCompute() {
1122 jsr166 1.7 FibAction g = new FibAction(9);
1123 jsr166 1.18 assertSame(g, g.fork());
1124 jsr166 1.7 FibAction f = new FibAction(8);
1125 jsr166 1.18 assertSame(f, f.fork());
1126     assertSame(f, pollTask());
1127 jsr166 1.7 helpQuiesce();
1128 jsr166 1.19 checkNotDone(f);
1129     checkCompletedNormally(g);
1130 jsr166 1.7 }};
1131 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1132 dl 1.1 }
1133    
1134 jsr166 1.2 /**
1135 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1136     */
1137     public void testPeekNextLocalTaskAsync() {
1138 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1139 jsr166 1.39 protected void realCompute() {
1140 jsr166 1.7 FibAction g = new FibAction(9);
1141 jsr166 1.18 assertSame(g, g.fork());
1142 jsr166 1.7 FibAction f = new FibAction(8);
1143 jsr166 1.18 assertSame(f, f.fork());
1144     assertSame(g, peekNextLocalTask());
1145     assertNull(f.join());
1146 jsr166 1.7 helpQuiesce();
1147 jsr166 1.19 checkCompletedNormally(f);
1148     checkCompletedNormally(g);
1149 jsr166 1.7 }};
1150 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1151 dl 1.1 }
1152    
1153 jsr166 1.2 /**
1154 jsr166 1.19 * pollNextLocalTask returns least recent unexecuted task without
1155     * executing it, in async mode
1156 dl 1.1 */
1157     public void testPollNextLocalTaskAsync() {
1158 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1159 jsr166 1.39 protected void realCompute() {
1160 jsr166 1.7 FibAction g = new FibAction(9);
1161 jsr166 1.18 assertSame(g, g.fork());
1162 jsr166 1.7 FibAction f = new FibAction(8);
1163 jsr166 1.18 assertSame(f, f.fork());
1164     assertSame(g, pollNextLocalTask());
1165 jsr166 1.7 helpQuiesce();
1166 jsr166 1.19 checkCompletedNormally(f);
1167     checkNotDone(g);
1168 jsr166 1.7 }};
1169 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1170 dl 1.1 }
1171    
1172 jsr166 1.2 /**
1173 jsr166 1.19 * pollTask returns an unexecuted task without executing it, in
1174     * async mode
1175 dl 1.1 */
1176     public void testPollTaskAsync() {
1177 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1178 jsr166 1.39 protected void realCompute() {
1179 jsr166 1.7 FibAction g = new FibAction(9);
1180 jsr166 1.18 assertSame(g, g.fork());
1181 jsr166 1.7 FibAction f = new FibAction(8);
1182 jsr166 1.18 assertSame(f, f.fork());
1183     assertSame(g, pollTask());
1184 jsr166 1.7 helpQuiesce();
1185 jsr166 1.19 checkCompletedNormally(f);
1186     checkNotDone(g);
1187 jsr166 1.7 }};
1188 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1189 dl 1.1 }
1190    
1191 jsr166 1.37 /** Demo from RecursiveAction javadoc */
1192 jsr166 1.34 static class SortTask extends RecursiveAction {
1193     final long[] array; final int lo, hi;
1194     SortTask(long[] array, int lo, int hi) {
1195     this.array = array; this.lo = lo; this.hi = hi;
1196     }
1197 jsr166 1.37 SortTask(long[] array) { this(array, 0, array.length); }
1198 jsr166 1.34 protected void compute() {
1199     if (hi - lo < THRESHOLD)
1200 jsr166 1.37 sortSequentially(lo, hi);
1201 jsr166 1.34 else {
1202     int mid = (lo + hi) >>> 1;
1203     invokeAll(new SortTask(array, lo, mid),
1204     new SortTask(array, mid, hi));
1205 jsr166 1.37 merge(lo, mid, hi);
1206 jsr166 1.34 }
1207     }
1208 jsr166 1.37 // implementation details follow:
1209 jsr166 1.38 static final int THRESHOLD = 100;
1210 jsr166 1.37 void sortSequentially(int lo, int hi) {
1211 jsr166 1.34 Arrays.sort(array, lo, hi);
1212     }
1213 jsr166 1.37 void merge(int lo, int mid, int hi) {
1214 jsr166 1.36 long[] buf = Arrays.copyOfRange(array, lo, mid);
1215     for (int i = 0, j = lo, k = mid; i < buf.length; j++)
1216     array[j] = (k == hi || buf[i] < array[k]) ?
1217     buf[i++] : array[k++];
1218 jsr166 1.34 }
1219     }
1220    
1221     /**
1222     * SortTask demo works as advertised
1223     */
1224     public void testSortTaskDemo() {
1225     ThreadLocalRandom rnd = ThreadLocalRandom.current();
1226 jsr166 1.35 long[] array = new long[1007];
1227 jsr166 1.34 for (int i = 0; i < array.length; i++)
1228     array[i] = rnd.nextLong();
1229     long[] arrayClone = array.clone();
1230 jsr166 1.37 testInvokeOnPool(mainPool(), new SortTask(array));
1231 jsr166 1.34 Arrays.sort(arrayClone);
1232     assertTrue(Arrays.equals(array, arrayClone));
1233     }
1234 dl 1.1 }