ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.40
Committed: Wed Dec 31 19:05:43 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.39: +9 -6 lines
Log Message:
no wildcard imports

File Contents

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