ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.42
Committed: Sun Feb 22 04:52:47 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.41: +1 -1 lines
Log Message:
use static TimeUnit import

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