ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.36
Committed: Sun Jun 26 06:50:19 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +4 -7 lines
Log Message:
use only n/2 space overhead in SortTask.merge

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 dl 1.1 import junit.framework.*;
8 jsr166 1.15 import java.util.concurrent.CancellationException;
9 jsr166 1.26 import java.util.concurrent.SynchronousQueue;
10 jsr166 1.15 import java.util.concurrent.ExecutionException;
11     import java.util.concurrent.ForkJoinPool;
12 jsr166 1.27 import java.util.concurrent.ForkJoinTask;
13 jsr166 1.15 import java.util.concurrent.ForkJoinWorkerThread;
14     import java.util.concurrent.RecursiveAction;
15 jsr166 1.34 import java.util.concurrent.ThreadLocalRandom;
16 jsr166 1.15 import java.util.concurrent.TimeUnit;
17 jsr166 1.19 import java.util.concurrent.TimeoutException;
18     import static java.util.concurrent.TimeUnit.SECONDS;
19 jsr166 1.34 import java.util.Arrays;
20 jsr166 1.15 import java.util.HashSet;
21 dl 1.1
22     public class RecursiveActionTest extends JSR166TestCase {
23    
24     public static void main(String[] args) {
25 jsr166 1.11 junit.textui.TestRunner.run(suite());
26 dl 1.1 }
27 jsr166 1.13
28 dl 1.1 public static Test suite() {
29 jsr166 1.9 return new TestSuite(RecursiveActionTest.class);
30 dl 1.1 }
31    
32 jsr166 1.14 private static ForkJoinPool mainPool() {
33     return new ForkJoinPool();
34     }
35    
36     private static ForkJoinPool singletonPool() {
37     return new ForkJoinPool(1);
38     }
39    
40     private static ForkJoinPool asyncSingletonPool() {
41     return new ForkJoinPool(1,
42     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
43     null, true);
44     }
45    
46     private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
47     try {
48 jsr166 1.19 checkNotDone(a);
49 jsr166 1.18
50     assertNull(pool.invoke(a));
51    
52 jsr166 1.19 checkCompletedNormally(a);
53 jsr166 1.14 } finally {
54     joinPool(pool);
55     }
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.18 public 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     public 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     public 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     public 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     public void realCompute() {
260     FibAction f = new FibAction(8);
261 jsr166 1.28 final Thread myself = Thread.currentThread();
262 jsr166 1.24
263     // test join()
264     assertSame(f, f.fork());
265 jsr166 1.28 myself.interrupt();
266     assertTrue(myself.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.28 myself.interrupt();
276     assertTrue(myself.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.28 myself.interrupt();
289     assertTrue(myself.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.28 myself.interrupt();
302     assertTrue(myself.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.28 myself.interrupt();
312     assertTrue(myself.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.28 myself.interrupt();
321     assertTrue(myself.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     public void realCompute() throws InterruptedException {
340     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.28 final Thread myself = Thread.currentThread();
362 jsr166 1.26
363     // test join() ------------
364    
365     f = fibActions[0];
366 jsr166 1.27 assertFalse(ForkJoinTask.inForkJoinPool());
367 jsr166 1.28 myself.interrupt();
368     assertTrue(myself.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.28 myself.interrupt();
376     assertTrue(myself.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.28 myself.interrupt();
387     assertTrue(myself.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.28 myself.interrupt();
400     assertTrue(myself.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.28 myself.interrupt();
408     assertTrue(myself.isInterrupted());
409 jsr166 1.26 f.quietlyJoin();
410     assertTrue(Thread.interrupted());
411     checkCancelled(f);
412    
413     f = fibActions[5];
414 jsr166 1.28 myself.interrupt();
415     assertTrue(myself.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     awaitTermination(t, LONG_DELAY_MS);
427    
428     a.reinitialize();
429     t = newStartedThread(r);
430     testInvokeOnPool(singletonPool(), a);
431     awaitTermination(t, LONG_DELAY_MS);
432     }
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     public void realCompute() throws Exception {
440     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     public void realCompute() throws Exception {
455     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     public void realCompute() throws Exception {
470     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     public 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     public 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 jsr166 1.18 assertEquals(21, f.result);
506     assertEquals(0, getQueuedTaskCount());
507 jsr166 1.19 checkCompletedNormally(f);
508 jsr166 1.7 }};
509 jsr166 1.14 testInvokeOnPool(mainPool(), a);
510 dl 1.1 }
511    
512 jsr166 1.2 /**
513 dl 1.1 * invoke task throws exception when task completes abnormally
514     */
515     public void testAbnormalInvoke() {
516 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
517     public void realCompute() {
518     FailingFibAction f = new FailingFibAction(8);
519 jsr166 1.7 try {
520     f.invoke();
521     shouldThrow();
522 jsr166 1.19 } catch (FJException success) {
523 jsr166 1.21 checkCompletedAbnormally(f, success);
524 jsr166 1.19 }
525 jsr166 1.7 }};
526 jsr166 1.14 testInvokeOnPool(mainPool(), a);
527 dl 1.1 }
528    
529 jsr166 1.2 /**
530 jsr166 1.4 * quietlyInvoke task returns when task completes abnormally
531 dl 1.1 */
532     public void testAbnormalQuietlyInvoke() {
533 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
534     public void realCompute() {
535 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
536     f.quietlyInvoke();
537 jsr166 1.19 assertTrue(f.getException() instanceof FJException);
538 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
539 jsr166 1.7 }};
540 jsr166 1.14 testInvokeOnPool(mainPool(), a);
541 dl 1.1 }
542    
543 jsr166 1.2 /**
544 dl 1.1 * join of a forked task throws exception when task completes abnormally
545     */
546     public void testAbnormalForkJoin() {
547 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
548     public void realCompute() {
549     FailingFibAction f = new FailingFibAction(8);
550     assertSame(f, f.fork());
551 jsr166 1.7 try {
552     f.join();
553     shouldThrow();
554 jsr166 1.19 } catch (FJException success) {
555 jsr166 1.21 checkCompletedAbnormally(f, success);
556 jsr166 1.19 }
557 jsr166 1.7 }};
558 jsr166 1.14 testInvokeOnPool(mainPool(), a);
559 dl 1.1 }
560    
561 jsr166 1.2 /**
562 dl 1.1 * get of a forked task throws exception when task completes abnormally
563     */
564     public void testAbnormalForkGet() {
565 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
566     public void realCompute() throws Exception {
567     FailingFibAction f = new FailingFibAction(8);
568     assertSame(f, f.fork());
569 jsr166 1.7 try {
570     f.get();
571     shouldThrow();
572 jsr166 1.19 } catch (ExecutionException success) {
573 jsr166 1.21 Throwable cause = success.getCause();
574     assertTrue(cause instanceof FJException);
575     checkCompletedAbnormally(f, cause);
576 jsr166 1.19 }
577 jsr166 1.7 }};
578 jsr166 1.14 testInvokeOnPool(mainPool(), a);
579 dl 1.1 }
580    
581 jsr166 1.2 /**
582 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
583     */
584     public void testAbnormalForkTimedGet() {
585 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
586     public void realCompute() throws Exception {
587     FailingFibAction f = new FailingFibAction(8);
588     assertSame(f, f.fork());
589 jsr166 1.7 try {
590     f.get(5L, TimeUnit.SECONDS);
591     shouldThrow();
592 jsr166 1.19 } catch (ExecutionException success) {
593 jsr166 1.21 Throwable cause = success.getCause();
594     assertTrue(cause instanceof FJException);
595     checkCompletedAbnormally(f, cause);
596 jsr166 1.19 }
597 jsr166 1.7 }};
598 jsr166 1.14 testInvokeOnPool(mainPool(), a);
599 dl 1.1 }
600    
601 jsr166 1.2 /**
602 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
603     */
604     public void testAbnormalForkQuietlyJoin() {
605 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
606     public void realCompute() {
607 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
608 jsr166 1.18 assertSame(f, f.fork());
609 jsr166 1.7 f.quietlyJoin();
610 jsr166 1.18 assertTrue(f.getException() instanceof FJException);
611 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
612 jsr166 1.7 }};
613 jsr166 1.14 testInvokeOnPool(mainPool(), a);
614 dl 1.1 }
615    
616 jsr166 1.2 /**
617 dl 1.1 * invoke task throws exception when task cancelled
618     */
619     public void testCancelledInvoke() {
620 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
621     public void realCompute() {
622     FibAction f = new FibAction(8);
623     assertTrue(f.cancel(true));
624 jsr166 1.7 try {
625     f.invoke();
626     shouldThrow();
627 jsr166 1.19 } catch (CancellationException success) {
628     checkCancelled(f);
629     }
630 jsr166 1.7 }};
631 jsr166 1.14 testInvokeOnPool(mainPool(), a);
632 dl 1.1 }
633    
634 jsr166 1.2 /**
635 dl 1.1 * join of a forked task throws exception when task cancelled
636     */
637     public void testCancelledForkJoin() {
638 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
639     public void realCompute() {
640     FibAction f = new FibAction(8);
641     assertTrue(f.cancel(true));
642     assertSame(f, f.fork());
643 jsr166 1.7 try {
644     f.join();
645     shouldThrow();
646 jsr166 1.19 } catch (CancellationException success) {
647     checkCancelled(f);
648     }
649 jsr166 1.7 }};
650 jsr166 1.14 testInvokeOnPool(mainPool(), a);
651 dl 1.1 }
652    
653 jsr166 1.2 /**
654 dl 1.1 * get of a forked task throws exception when task cancelled
655     */
656     public void testCancelledForkGet() {
657 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
658     public void realCompute() throws Exception {
659     FibAction f = new FibAction(8);
660     assertTrue(f.cancel(true));
661     assertSame(f, f.fork());
662 jsr166 1.7 try {
663     f.get();
664     shouldThrow();
665 jsr166 1.19 } catch (CancellationException success) {
666     checkCancelled(f);
667     }
668 jsr166 1.7 }};
669 jsr166 1.14 testInvokeOnPool(mainPool(), a);
670 dl 1.1 }
671    
672 jsr166 1.2 /**
673 dl 1.1 * timed get of a forked task throws exception when task cancelled
674     */
675     public void testCancelledForkTimedGet() {
676 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
677     public void realCompute() throws Exception {
678     FibAction f = new FibAction(8);
679     assertTrue(f.cancel(true));
680     assertSame(f, f.fork());
681 jsr166 1.7 try {
682 jsr166 1.19 f.get(5L, SECONDS);
683 jsr166 1.7 shouldThrow();
684 jsr166 1.19 } catch (CancellationException success) {
685     checkCancelled(f);
686     }
687 jsr166 1.7 }};
688 jsr166 1.14 testInvokeOnPool(mainPool(), a);
689 dl 1.1 }
690    
691 jsr166 1.2 /**
692 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
693     */
694     public void testCancelledForkQuietlyJoin() {
695 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
696     public void realCompute() {
697 jsr166 1.7 FibAction f = new FibAction(8);
698 jsr166 1.18 assertTrue(f.cancel(true));
699     assertSame(f, f.fork());
700 jsr166 1.7 f.quietlyJoin();
701 jsr166 1.19 checkCancelled(f);
702 jsr166 1.7 }};
703 jsr166 1.14 testInvokeOnPool(mainPool(), a);
704 dl 1.1 }
705    
706     /**
707     * getPool of executing task returns its pool
708     */
709     public void testGetPool() {
710 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
711 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
712     public void realCompute() {
713     assertSame(mainPool, getPool());
714 jsr166 1.7 }};
715 jsr166 1.14 testInvokeOnPool(mainPool, a);
716 dl 1.1 }
717    
718     /**
719     * getPool of non-FJ task returns null
720     */
721     public void testGetPool2() {
722 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
723     public void realCompute() {
724     assertNull(getPool());
725 jsr166 1.7 }};
726 jsr166 1.16 assertNull(a.invoke());
727 dl 1.1 }
728    
729     /**
730     * inForkJoinPool of executing task returns true
731     */
732     public void testInForkJoinPool() {
733 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
734     public void realCompute() {
735     assertTrue(inForkJoinPool());
736 jsr166 1.7 }};
737 jsr166 1.14 testInvokeOnPool(mainPool(), a);
738 dl 1.1 }
739    
740     /**
741     * inForkJoinPool of non-FJ task returns false
742     */
743     public void testInForkJoinPool2() {
744 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
745     public void realCompute() {
746     assertFalse(inForkJoinPool());
747 jsr166 1.7 }};
748 jsr166 1.16 assertNull(a.invoke());
749 dl 1.1 }
750    
751     /**
752     * getPool of current thread in pool returns its pool
753     */
754     public void testWorkerGetPool() {
755 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
756 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
757     public void realCompute() {
758 jsr166 1.7 ForkJoinWorkerThread w =
759 jsr166 1.14 (ForkJoinWorkerThread) Thread.currentThread();
760 jsr166 1.18 assertSame(mainPool, w.getPool());
761 jsr166 1.7 }};
762 jsr166 1.14 testInvokeOnPool(mainPool, a);
763 dl 1.1 }
764    
765     /**
766     * getPoolIndex of current thread in pool returns 0 <= value < poolSize
767     */
768     public void testWorkerGetPoolIndex() {
769 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
770 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
771     public void realCompute() {
772 jsr166 1.7 ForkJoinWorkerThread w =
773 jsr166 1.28 (ForkJoinWorkerThread) Thread.currentThread();
774 jsr166 1.19 assertTrue(w.getPoolIndex() >= 0);
775 dl 1.29 // pool size can shrink after assigning index, so cannot check
776     // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
777 jsr166 1.7 }};
778 jsr166 1.14 testInvokeOnPool(mainPool, a);
779 dl 1.1 }
780    
781     /**
782     * setRawResult(null) succeeds
783     */
784     public void testSetRawResult() {
785 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
786     public void realCompute() {
787 jsr166 1.7 setRawResult(null);
788 jsr166 1.23 assertNull(getRawResult());
789 jsr166 1.7 }};
790 jsr166 1.16 assertNull(a.invoke());
791 dl 1.1 }
792    
793 jsr166 1.2 /**
794 jsr166 1.33 * A reinitialized normally completed task may be re-invoked
795 dl 1.1 */
796     public void testReinitialize() {
797 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
798     public void realCompute() {
799 jsr166 1.7 FibAction f = new FibAction(8);
800 jsr166 1.19 checkNotDone(f);
801    
802     for (int i = 0; i < 3; i++) {
803     assertNull(f.invoke());
804     assertEquals(21, f.result);
805     checkCompletedNormally(f);
806     f.reinitialize();
807     checkNotDone(f);
808     }
809 jsr166 1.7 }};
810 jsr166 1.14 testInvokeOnPool(mainPool(), a);
811 dl 1.1 }
812    
813 jsr166 1.2 /**
814 jsr166 1.33 * A reinitialized abnormally completed task may be re-invoked
815     */
816     public void testReinitializeAbnormal() {
817     RecursiveAction a = new CheckedRecursiveAction() {
818     public void realCompute() {
819     FailingFibAction f = new FailingFibAction(8);
820     checkNotDone(f);
821    
822     for (int i = 0; i < 3; i++) {
823     try {
824     f.invoke();
825     shouldThrow();
826     } catch (FJException success) {
827     checkCompletedAbnormally(f, success);
828     }
829     f.reinitialize();
830     checkNotDone(f);
831     }
832     }};
833     testInvokeOnPool(mainPool(), a);
834     }
835    
836     /**
837 dl 1.1 * invoke task throws exception after invoking completeExceptionally
838     */
839     public void testCompleteExceptionally() {
840 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
841     public void realCompute() {
842     FibAction f = new FibAction(8);
843     f.completeExceptionally(new FJException());
844 jsr166 1.7 try {
845     f.invoke();
846     shouldThrow();
847 jsr166 1.19 } catch (FJException success) {
848 jsr166 1.21 checkCompletedAbnormally(f, success);
849 jsr166 1.19 }
850 jsr166 1.7 }};
851 jsr166 1.14 testInvokeOnPool(mainPool(), a);
852 dl 1.1 }
853    
854 jsr166 1.2 /**
855 dl 1.1 * invoke task suppresses execution invoking complete
856     */
857     public void testComplete() {
858 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
859     public void realCompute() {
860 jsr166 1.7 FibAction f = new FibAction(8);
861     f.complete(null);
862 jsr166 1.18 assertNull(f.invoke());
863     assertEquals(0, f.result);
864 jsr166 1.19 checkCompletedNormally(f);
865 jsr166 1.7 }};
866 jsr166 1.14 testInvokeOnPool(mainPool(), a);
867 dl 1.1 }
868    
869 jsr166 1.2 /**
870 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
871     */
872     public void testInvokeAll2() {
873 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
874     public void realCompute() {
875 jsr166 1.7 FibAction f = new FibAction(8);
876     FibAction g = new FibAction(9);
877     invokeAll(f, g);
878 jsr166 1.19 checkCompletedNormally(f);
879 jsr166 1.18 assertEquals(21, f.result);
880 jsr166 1.19 checkCompletedNormally(g);
881 jsr166 1.18 assertEquals(34, g.result);
882 jsr166 1.7 }};
883 jsr166 1.14 testInvokeOnPool(mainPool(), a);
884 dl 1.1 }
885    
886 jsr166 1.2 /**
887 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
888     */
889     public void testInvokeAll1() {
890 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
891     public void realCompute() {
892 jsr166 1.7 FibAction f = new FibAction(8);
893     invokeAll(f);
894 jsr166 1.19 checkCompletedNormally(f);
895 jsr166 1.18 assertEquals(21, f.result);
896 jsr166 1.7 }};
897 jsr166 1.14 testInvokeOnPool(mainPool(), a);
898 dl 1.1 }
899    
900 jsr166 1.2 /**
901 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
902     */
903     public void testInvokeAll3() {
904 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
905     public void realCompute() {
906 jsr166 1.7 FibAction f = new FibAction(8);
907     FibAction g = new FibAction(9);
908     FibAction h = new FibAction(7);
909     invokeAll(f, g, h);
910 jsr166 1.20 assertTrue(f.isDone());
911     assertTrue(g.isDone());
912     assertTrue(h.isDone());
913 jsr166 1.19 checkCompletedNormally(f);
914 jsr166 1.18 assertEquals(21, f.result);
915 jsr166 1.19 checkCompletedNormally(g);
916 jsr166 1.18 assertEquals(34, g.result);
917 jsr166 1.19 checkCompletedNormally(g);
918 jsr166 1.18 assertEquals(13, h.result);
919 jsr166 1.7 }};
920 jsr166 1.14 testInvokeOnPool(mainPool(), a);
921 dl 1.1 }
922    
923 jsr166 1.2 /**
924 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
925     */
926     public void testInvokeAllCollection() {
927 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
928     public void realCompute() {
929 jsr166 1.7 FibAction f = new FibAction(8);
930     FibAction g = new FibAction(9);
931     FibAction h = new FibAction(7);
932     HashSet set = new HashSet();
933     set.add(f);
934     set.add(g);
935     set.add(h);
936     invokeAll(set);
937 jsr166 1.20 assertTrue(f.isDone());
938     assertTrue(g.isDone());
939     assertTrue(h.isDone());
940 jsr166 1.19 checkCompletedNormally(f);
941 jsr166 1.18 assertEquals(21, f.result);
942 jsr166 1.19 checkCompletedNormally(g);
943 jsr166 1.18 assertEquals(34, g.result);
944 jsr166 1.19 checkCompletedNormally(g);
945 jsr166 1.18 assertEquals(13, h.result);
946 jsr166 1.7 }};
947 jsr166 1.14 testInvokeOnPool(mainPool(), a);
948 dl 1.1 }
949    
950 jsr166 1.2 /**
951 dl 1.1 * invokeAll(tasks) with any null task throws NPE
952     */
953     public void testInvokeAllNPE() {
954 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
955     public void realCompute() {
956     FibAction f = new FibAction(8);
957     FibAction g = new FibAction(9);
958     FibAction h = null;
959 jsr166 1.7 try {
960     invokeAll(f, g, h);
961     shouldThrow();
962 jsr166 1.18 } catch (NullPointerException success) {}
963 jsr166 1.7 }};
964 jsr166 1.14 testInvokeOnPool(mainPool(), a);
965 dl 1.1 }
966    
967 jsr166 1.2 /**
968 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
969     */
970     public void testAbnormalInvokeAll2() {
971 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
972     public void realCompute() {
973     FibAction f = new FibAction(8);
974     FailingFibAction g = new FailingFibAction(9);
975 jsr166 1.7 try {
976     invokeAll(f, g);
977     shouldThrow();
978 jsr166 1.19 } catch (FJException success) {
979 jsr166 1.21 checkCompletedAbnormally(g, success);
980 jsr166 1.19 }
981 jsr166 1.7 }};
982 jsr166 1.14 testInvokeOnPool(mainPool(), a);
983 dl 1.1 }
984    
985 jsr166 1.2 /**
986 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
987     */
988     public void testAbnormalInvokeAll1() {
989 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
990     public void realCompute() {
991     FailingFibAction g = new FailingFibAction(9);
992 jsr166 1.7 try {
993     invokeAll(g);
994     shouldThrow();
995 jsr166 1.19 } catch (FJException success) {
996 jsr166 1.21 checkCompletedAbnormally(g, success);
997 jsr166 1.19 }
998 jsr166 1.7 }};
999 jsr166 1.14 testInvokeOnPool(mainPool(), a);
1000 dl 1.1 }
1001    
1002 jsr166 1.2 /**
1003 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
1004     */
1005     public void testAbnormalInvokeAll3() {
1006 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1007     public void realCompute() {
1008     FibAction f = new FibAction(8);
1009     FailingFibAction g = new FailingFibAction(9);
1010     FibAction h = new FibAction(7);
1011 jsr166 1.7 try {
1012     invokeAll(f, g, h);
1013     shouldThrow();
1014 jsr166 1.19 } catch (FJException success) {
1015 jsr166 1.21 checkCompletedAbnormally(g, success);
1016 jsr166 1.19 }
1017 jsr166 1.7 }};
1018 jsr166 1.14 testInvokeOnPool(mainPool(), a);
1019 dl 1.1 }
1020    
1021 jsr166 1.2 /**
1022 jsr166 1.3 * invokeAll(collection) throws exception if any task does
1023 dl 1.1 */
1024     public void testAbnormalInvokeAllCollection() {
1025 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1026     public void realCompute() {
1027     FailingFibAction f = new FailingFibAction(8);
1028     FibAction g = new FibAction(9);
1029     FibAction h = new FibAction(7);
1030     HashSet set = new HashSet();
1031     set.add(f);
1032     set.add(g);
1033     set.add(h);
1034 jsr166 1.7 try {
1035     invokeAll(set);
1036     shouldThrow();
1037 jsr166 1.19 } catch (FJException success) {
1038 jsr166 1.21 checkCompletedAbnormally(f, success);
1039 jsr166 1.19 }
1040 jsr166 1.7 }};
1041 jsr166 1.14 testInvokeOnPool(mainPool(), a);
1042 dl 1.1 }
1043    
1044 jsr166 1.2 /**
1045 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
1046     * and suppresses execution
1047     */
1048     public void testTryUnfork() {
1049 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1050     public void realCompute() {
1051 jsr166 1.7 FibAction g = new FibAction(9);
1052 jsr166 1.18 assertSame(g, g.fork());
1053 jsr166 1.7 FibAction f = new FibAction(8);
1054 jsr166 1.18 assertSame(f, f.fork());
1055     assertTrue(f.tryUnfork());
1056 jsr166 1.7 helpQuiesce();
1057 jsr166 1.19 checkNotDone(f);
1058     checkCompletedNormally(g);
1059 jsr166 1.7 }};
1060 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1061 dl 1.1 }
1062    
1063 jsr166 1.2 /**
1064 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
1065     * there are more tasks than threads
1066     */
1067     public void testGetSurplusQueuedTaskCount() {
1068 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1069     public void realCompute() {
1070 jsr166 1.7 FibAction h = new FibAction(7);
1071 jsr166 1.18 assertSame(h, h.fork());
1072 jsr166 1.7 FibAction g = new FibAction(9);
1073 jsr166 1.18 assertSame(g, g.fork());
1074 jsr166 1.7 FibAction f = new FibAction(8);
1075 jsr166 1.18 assertSame(f, f.fork());
1076     assertTrue(getSurplusQueuedTaskCount() > 0);
1077 jsr166 1.7 helpQuiesce();
1078 jsr166 1.21 assertEquals(0, getSurplusQueuedTaskCount());
1079 jsr166 1.19 checkCompletedNormally(f);
1080     checkCompletedNormally(g);
1081     checkCompletedNormally(h);
1082 jsr166 1.7 }};
1083 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1084 dl 1.1 }
1085    
1086 jsr166 1.2 /**
1087 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
1088     */
1089     public void testPeekNextLocalTask() {
1090 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1091     public void realCompute() {
1092 jsr166 1.7 FibAction g = new FibAction(9);
1093 jsr166 1.18 assertSame(g, g.fork());
1094 jsr166 1.7 FibAction f = new FibAction(8);
1095 jsr166 1.18 assertSame(f, f.fork());
1096     assertSame(f, peekNextLocalTask());
1097     assertNull(f.join());
1098 jsr166 1.19 checkCompletedNormally(f);
1099 jsr166 1.7 helpQuiesce();
1100 jsr166 1.19 checkCompletedNormally(f);
1101     checkCompletedNormally(g);
1102 jsr166 1.7 }};
1103 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1104 dl 1.1 }
1105    
1106 jsr166 1.2 /**
1107 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
1108     * without executing it
1109     */
1110     public void testPollNextLocalTask() {
1111 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1112     public void realCompute() {
1113 jsr166 1.7 FibAction g = new FibAction(9);
1114 jsr166 1.18 assertSame(g, g.fork());
1115 jsr166 1.7 FibAction f = new FibAction(8);
1116 jsr166 1.18 assertSame(f, f.fork());
1117     assertSame(f, pollNextLocalTask());
1118 jsr166 1.7 helpQuiesce();
1119 jsr166 1.19 checkNotDone(f);
1120     checkCompletedNormally(g);
1121 jsr166 1.7 }};
1122 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1123 dl 1.1 }
1124    
1125 jsr166 1.2 /**
1126 jsr166 1.19 * pollTask returns an unexecuted task without executing it
1127 dl 1.1 */
1128     public void testPollTask() {
1129 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1130     public void realCompute() {
1131 jsr166 1.7 FibAction g = new FibAction(9);
1132 jsr166 1.18 assertSame(g, g.fork());
1133 jsr166 1.7 FibAction f = new FibAction(8);
1134 jsr166 1.18 assertSame(f, f.fork());
1135     assertSame(f, pollTask());
1136 jsr166 1.7 helpQuiesce();
1137 jsr166 1.19 checkNotDone(f);
1138     checkCompletedNormally(g);
1139 jsr166 1.7 }};
1140 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1141 dl 1.1 }
1142    
1143 jsr166 1.2 /**
1144 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1145     */
1146     public void testPeekNextLocalTaskAsync() {
1147 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1148     public void realCompute() {
1149 jsr166 1.7 FibAction g = new FibAction(9);
1150 jsr166 1.18 assertSame(g, g.fork());
1151 jsr166 1.7 FibAction f = new FibAction(8);
1152 jsr166 1.18 assertSame(f, f.fork());
1153     assertSame(g, peekNextLocalTask());
1154     assertNull(f.join());
1155 jsr166 1.7 helpQuiesce();
1156 jsr166 1.19 checkCompletedNormally(f);
1157     checkCompletedNormally(g);
1158 jsr166 1.7 }};
1159 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1160 dl 1.1 }
1161    
1162 jsr166 1.2 /**
1163 jsr166 1.19 * pollNextLocalTask returns least recent unexecuted task without
1164     * executing it, in async mode
1165 dl 1.1 */
1166     public void testPollNextLocalTaskAsync() {
1167 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1168     public void realCompute() {
1169 jsr166 1.7 FibAction g = new FibAction(9);
1170 jsr166 1.18 assertSame(g, g.fork());
1171 jsr166 1.7 FibAction f = new FibAction(8);
1172 jsr166 1.18 assertSame(f, f.fork());
1173     assertSame(g, pollNextLocalTask());
1174 jsr166 1.7 helpQuiesce();
1175 jsr166 1.19 checkCompletedNormally(f);
1176     checkNotDone(g);
1177 jsr166 1.7 }};
1178 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1179 dl 1.1 }
1180    
1181 jsr166 1.2 /**
1182 jsr166 1.19 * pollTask returns an unexecuted task without executing it, in
1183     * async mode
1184 dl 1.1 */
1185     public void testPollTaskAsync() {
1186 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1187     public void realCompute() {
1188 jsr166 1.7 FibAction g = new FibAction(9);
1189 jsr166 1.18 assertSame(g, g.fork());
1190 jsr166 1.7 FibAction f = new FibAction(8);
1191 jsr166 1.18 assertSame(f, f.fork());
1192     assertSame(g, pollTask());
1193 jsr166 1.7 helpQuiesce();
1194 jsr166 1.19 checkCompletedNormally(f);
1195     checkNotDone(g);
1196 jsr166 1.7 }};
1197 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1198 dl 1.1 }
1199    
1200 jsr166 1.34 static class SortTask extends RecursiveAction {
1201     final long[] array; final int lo, hi;
1202     SortTask(long[] array, int lo, int hi) {
1203     this.array = array; this.lo = lo; this.hi = hi;
1204     }
1205     final static int THRESHOLD = 100;
1206     protected void compute() {
1207     if (hi - lo < THRESHOLD)
1208     sequentiallySort(array, lo, hi);
1209     else {
1210     int mid = (lo + hi) >>> 1;
1211     invokeAll(new SortTask(array, lo, mid),
1212     new SortTask(array, mid, hi));
1213     merge(array, lo, mid, hi);
1214     }
1215     }
1216     static void sequentiallySort(long[] array, int lo, int hi) {
1217     Arrays.sort(array, lo, hi);
1218     }
1219     static void merge(long[] array, int lo, int mid, int hi) {
1220 jsr166 1.36 long[] buf = Arrays.copyOfRange(array, lo, mid);
1221     for (int i = 0, j = lo, k = mid; i < buf.length; j++)
1222     array[j] = (k == hi || buf[i] < array[k]) ?
1223     buf[i++] : array[k++];
1224 jsr166 1.34 }
1225     }
1226    
1227     /**
1228     * SortTask demo works as advertised
1229     */
1230     public void testSortTaskDemo() {
1231     ThreadLocalRandom rnd = ThreadLocalRandom.current();
1232 jsr166 1.35 long[] array = new long[1007];
1233 jsr166 1.34 for (int i = 0; i < array.length; i++)
1234     array[i] = rnd.nextLong();
1235     long[] arrayClone = array.clone();
1236     testInvokeOnPool(mainPool(),
1237     new SortTask(array, 0, array.length));
1238     Arrays.sort(arrayClone);
1239     assertTrue(Arrays.equals(array, arrayClone));
1240     }
1241 dl 1.1 }