ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.48
Committed: Tue Aug 16 23:17:13 2016 UTC (7 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.47: +0 -12 lines
Log Message:
stop checking that Thread.interrupt() works

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