ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.43
Committed: Sun Feb 22 19:16:38 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.42: +0 -1 lines
Log Message:
remove unused imports

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