ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.47
Committed: Tue Aug 16 23:02:57 2016 UTC (7 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +26 -26 lines
Log Message:
rename myself to currentThread

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