ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.53
Committed: Sun Jan 7 22:59:18 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.52: +1 -2 lines
Log Message:
use <>

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