ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.14
Committed: Mon Sep 13 10:49:59 2010 UTC (13 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.13: +1 -1 lines
Log Message:
Fix documentation of testSetRawResult

File Contents

# Content
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 * http://creativecommons.org/licenses/publicdomain
5 */
6
7 import junit.framework.*;
8 import java.util.concurrent.*;
9 import java.util.*;
10
11 public class RecursiveTaskTest extends JSR166TestCase {
12
13 public static void main(String[] args) {
14 junit.textui.TestRunner.run(suite());
15 }
16 public static Test suite() {
17 return new TestSuite(RecursiveTaskTest.class);
18 }
19
20 private static ForkJoinPool mainPool() {
21 return new ForkJoinPool();
22 }
23
24 private static ForkJoinPool singletonPool() {
25 return new ForkJoinPool(1);
26 }
27
28 private static ForkJoinPool asyncSingletonPool() {
29 return new ForkJoinPool(1,
30 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
31 null, true);
32 }
33
34 private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) {
35 try {
36 return pool.invoke(a);
37 } finally {
38 joinPool(pool);
39 }
40 }
41
42 static final class FJException extends RuntimeException {
43 FJException() { super(); }
44 }
45
46 // An invalid return value for Fib
47 static final Integer NoResult = Integer.valueOf(-17);
48
49 // A simple recursive task for testing
50 static final class FibTask extends RecursiveTask<Integer> {
51 final int number;
52 FibTask(int n) { number = n; }
53 public Integer compute() {
54 int n = number;
55 if (n <= 1)
56 return n;
57 FibTask f1 = new FibTask(n - 1);
58 f1.fork();
59 return (new FibTask(n - 2)).compute() + f1.join();
60 }
61 }
62
63 // A recursive action failing in base case
64 static final class FailingFibTask extends RecursiveTask<Integer> {
65 final int number;
66 int result;
67 FailingFibTask(int n) { number = n; }
68 public Integer compute() {
69 int n = number;
70 if (n <= 1)
71 throw new FJException();
72 FailingFibTask f1 = new FailingFibTask(n - 1);
73 f1.fork();
74 return (new FibTask(n - 2)).compute() + f1.join();
75 }
76 }
77
78 /**
79 * invoke returns value when task completes normally.
80 * isCompletedAbnormally and isCancelled return false for normally
81 * completed tasks. getRawResult of a completed non-null task
82 * returns value;
83 */
84 public void testInvoke() {
85 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
86 public Integer compute() {
87 FibTask f = new FibTask(8);
88 Integer r = f.invoke();
89 threadAssertTrue(r == 21);
90 threadAssertTrue(f.isDone());
91 threadAssertFalse(f.isCancelled());
92 threadAssertFalse(f.isCompletedAbnormally());
93 threadAssertTrue(f.getRawResult() == 21);
94 return r;
95 }
96 };
97 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
98 }
99
100 /**
101 * quietlyInvoke task returns when task completes normally.
102 * isCompletedAbnormally and isCancelled return false for normally
103 * completed tasks
104 */
105 public void testQuietlyInvoke() {
106 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
107 public Integer compute() {
108 FibTask f = new FibTask(8);
109 f.quietlyInvoke();
110 Integer r = f.getRawResult();
111 threadAssertTrue(r == 21);
112 threadAssertTrue(f.isDone());
113 threadAssertFalse(f.isCancelled());
114 threadAssertFalse(f.isCompletedAbnormally());
115 return r;
116 }
117 };
118 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
119 }
120
121 /**
122 * join of a forked task returns when task completes
123 */
124 public void testForkJoin() {
125 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
126 public Integer compute() {
127 FibTask f = new FibTask(8);
128 f.fork();
129 Integer r = f.join();
130 threadAssertTrue(r == 21);
131 threadAssertTrue(f.isDone());
132 return r;
133 }
134 };
135 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
136 }
137
138 /**
139 * get of a forked task returns when task completes
140 */
141 public void testForkGet() {
142 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
143 public Integer compute() {
144 try {
145 FibTask f = new FibTask(8);
146 f.fork();
147 Integer r = f.get();
148 threadAssertTrue(r == 21);
149 threadAssertTrue(f.isDone());
150 return r;
151 } catch (Exception ex) {
152 unexpectedException(ex);
153 }
154 return NoResult;
155 }
156 };
157 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
158 }
159
160 /**
161 * timed get of a forked task returns when task completes
162 */
163 public void testForkTimedGet() {
164 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
165 public Integer compute() {
166 try {
167 FibTask f = new FibTask(8);
168 f.fork();
169 Integer r = f.get(5L, TimeUnit.SECONDS);
170 threadAssertTrue(r == 21);
171 threadAssertTrue(f.isDone());
172 return r;
173 } catch (Exception ex) {
174 unexpectedException(ex);
175 }
176 return NoResult;
177 }
178 };
179 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
180 }
181
182 /**
183 * quietlyJoin of a forked task returns when task completes
184 */
185 public void testForkQuietlyJoin() {
186 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
187 public Integer compute() {
188 FibTask f = new FibTask(8);
189 f.fork();
190 f.quietlyJoin();
191 Integer r = f.getRawResult();
192 threadAssertTrue(r == 21);
193 threadAssertTrue(f.isDone());
194 return r;
195 }
196 };
197 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
198 }
199
200
201 /**
202 * helpQuiesce returns when tasks are complete.
203 * getQueuedTaskCount returns 0 when quiescent
204 */
205 public void testForkHelpQuiesce() {
206 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
207 public Integer compute() {
208 FibTask f = new FibTask(8);
209 f.fork();
210 f.helpQuiesce();
211 Integer r = f.getRawResult();
212 threadAssertTrue(r == 21);
213 threadAssertTrue(f.isDone());
214 threadAssertTrue(getQueuedTaskCount() == 0);
215 return r;
216 }
217 };
218 assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
219 }
220
221
222 /**
223 * invoke task throws exception when task completes abnormally
224 */
225 public void testAbnormalInvoke() {
226 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
227 public Integer compute() {
228 try {
229 FailingFibTask f = new FailingFibTask(8);
230 f.invoke();
231 shouldThrow();
232 return NoResult;
233 } catch (FJException success) {
234 }
235 return NoResult;
236 }
237 };
238 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
239 }
240
241 /**
242 * quietlyInvoke task returns when task completes abnormally
243 */
244 public void testAbnormalQuietlyInvoke() {
245 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
246 public Integer compute() {
247 FailingFibTask f = new FailingFibTask(8);
248 f.quietlyInvoke();
249 threadAssertTrue(f.isDone());
250 return NoResult;
251 }
252 };
253 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
254 }
255
256 /**
257 * join of a forked task throws exception when task completes abnormally
258 */
259 public void testAbnormalForkJoin() {
260 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
261 public Integer compute() {
262 try {
263 FailingFibTask f = new FailingFibTask(8);
264 f.fork();
265 Integer r = f.join();
266 shouldThrow();
267 return r;
268 } catch (FJException success) {
269 }
270 return NoResult;
271 }
272 };
273 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
274 }
275
276 /**
277 * get of a forked task throws exception when task completes abnormally
278 */
279 public void testAbnormalForkGet() {
280 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
281 public Integer compute() {
282 try {
283 FailingFibTask f = new FailingFibTask(8);
284 f.fork();
285 Integer r = f.get();
286 shouldThrow();
287 return r;
288 } catch (ExecutionException success) {
289 } catch (Exception ex) {
290 unexpectedException(ex);
291 }
292 return NoResult;
293 }
294 };
295 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
296 }
297
298 /**
299 * timed get of a forked task throws exception when task completes abnormally
300 */
301 public void testAbnormalForkTimedGet() {
302 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
303 public Integer compute() {
304 try {
305 FailingFibTask f = new FailingFibTask(8);
306 f.fork();
307 Integer r = f.get(5L, TimeUnit.SECONDS);
308 shouldThrow();
309 return r;
310 } catch (ExecutionException success) {
311 } catch (Exception ex) {
312 unexpectedException(ex);
313 }
314 return NoResult;
315 }
316 };
317 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
318 }
319
320 /**
321 * quietlyJoin of a forked task returns when task completes abnormally
322 */
323 public void testAbnormalForkQuietlyJoin() {
324 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
325 public Integer compute() {
326 FailingFibTask f = new FailingFibTask(8);
327 f.fork();
328 f.quietlyJoin();
329 threadAssertTrue(f.isDone());
330 threadAssertTrue(f.isCompletedAbnormally());
331 threadAssertTrue(f.getException() instanceof FJException);
332 return NoResult;
333 }
334 };
335 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
336 }
337
338 /**
339 * invoke task throws exception when task cancelled
340 */
341 public void testCancelledInvoke() {
342 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
343 public Integer compute() {
344 try {
345 FibTask f = new FibTask(8);
346 f.cancel(true);
347 Integer r = f.invoke();
348 shouldThrow();
349 return r;
350 } catch (CancellationException success) {
351 }
352 return NoResult;
353 }
354 };
355 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
356 }
357
358 /**
359 * join of a forked task throws exception when task cancelled
360 */
361 public void testCancelledForkJoin() {
362 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
363 public Integer compute() {
364 try {
365 FibTask f = new FibTask(8);
366 f.cancel(true);
367 f.fork();
368 Integer r = f.join();
369 shouldThrow();
370 return r;
371 } catch (CancellationException success) {
372 }
373 return NoResult;
374 }
375 };
376 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
377 }
378
379 /**
380 * get of a forked task throws exception when task cancelled
381 */
382 public void testCancelledForkGet() {
383 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
384 public Integer compute() {
385 try {
386 FibTask f = new FibTask(8);
387 f.cancel(true);
388 f.fork();
389 Integer r = f.get();
390 shouldThrow();
391 return r;
392 } catch (CancellationException success) {
393 } catch (Exception ex) {
394 unexpectedException(ex);
395 }
396 return NoResult;
397 }
398 };
399 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
400 }
401
402 /**
403 * timed get of a forked task throws exception when task cancelled
404 */
405 public void testCancelledForkTimedGet() {
406 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
407 public Integer compute() {
408 try {
409 FibTask f = new FibTask(8);
410 f.cancel(true);
411 f.fork();
412 Integer r = f.get(5L, TimeUnit.SECONDS);
413 shouldThrow();
414 return r;
415 } catch (CancellationException success) {
416 } catch (Exception ex) {
417 unexpectedException(ex);
418 }
419 return NoResult;
420 }
421 };
422 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
423 }
424
425 /**
426 * quietlyJoin of a forked task returns when task cancelled
427 */
428 public void testCancelledForkQuietlyJoin() {
429 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
430 public Integer compute() {
431 FibTask f = new FibTask(8);
432 f.cancel(true);
433 f.fork();
434 f.quietlyJoin();
435 threadAssertTrue(f.isDone());
436 threadAssertTrue(f.isCompletedAbnormally());
437 threadAssertTrue(f.getException() instanceof CancellationException);
438 return NoResult;
439 }
440 };
441 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
442 }
443
444 /**
445 * getPool of executing task returns its pool
446 */
447 public void testGetPool() {
448 final ForkJoinPool mainPool = mainPool();
449 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
450 public Integer compute() {
451 threadAssertTrue(getPool() == mainPool);
452 return NoResult;
453 }
454 };
455 assertSame(NoResult, testInvokeOnPool(mainPool, a));
456 }
457
458 /**
459 * getPool of non-FJ task returns null
460 */
461 public void testGetPool2() {
462 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
463 public Integer compute() {
464 threadAssertTrue(getPool() == null);
465 return NoResult;
466 }
467 };
468 a.invoke();
469 }
470
471 /**
472 * inForkJoinPool of executing task returns true
473 */
474 public void testInForkJoinPool() {
475 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
476 public Integer compute() {
477 threadAssertTrue(inForkJoinPool());
478 return NoResult;
479 }
480 };
481 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
482 }
483
484 /**
485 * inForkJoinPool of non-FJ task returns false
486 */
487 public void testInForkJoinPool2() {
488 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
489 public Integer compute() {
490 threadAssertTrue(!inForkJoinPool());
491 return NoResult;
492 }
493 };
494 a.invoke();
495 }
496
497 /**
498 * The value set by setRawResult is returned by invoke
499 */
500 public void testSetRawResult() {
501 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
502 public Integer compute() {
503 setRawResult(NoResult);
504 return NoResult;
505 }
506 };
507 assertSame(NoResult, a.invoke());
508 }
509
510 /**
511 * A reinitialized task may be re-invoked
512 */
513 public void testReinitialize() {
514 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
515 public Integer compute() {
516 FibTask f = new FibTask(8);
517 Integer r = f.invoke();
518 threadAssertTrue(r == 21);
519 threadAssertTrue(f.isDone());
520 threadAssertFalse(f.isCancelled());
521 threadAssertFalse(f.isCompletedAbnormally());
522 f.reinitialize();
523 r = f.invoke();
524 threadAssertTrue(r == 21);
525 return NoResult;
526 }
527 };
528 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
529 }
530
531 /**
532 * invoke task throws exception after invoking completeExceptionally
533 */
534 public void testCompleteExceptionally() {
535 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
536 public Integer compute() {
537 try {
538 FibTask f = new FibTask(8);
539 f.completeExceptionally(new FJException());
540 Integer r = f.invoke();
541 shouldThrow();
542 return r;
543 } catch (FJException success) {
544 }
545 return NoResult;
546 }
547 };
548 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
549 }
550
551 /**
552 * invoke task suppresses execution invoking complete
553 */
554 public void testComplete() {
555 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
556 public Integer compute() {
557 FibTask f = new FibTask(8);
558 f.complete(NoResult);
559 Integer r = f.invoke();
560 threadAssertTrue(f.isDone());
561 threadAssertTrue(r == NoResult);
562 return r;
563 }
564 };
565 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
566 }
567
568 /**
569 * invokeAll(t1, t2) invokes all task arguments
570 */
571 public void testInvokeAll2() {
572 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
573 public Integer compute() {
574 FibTask f = new FibTask(8);
575 FibTask g = new FibTask(9);
576 invokeAll(f, g);
577 threadAssertTrue(f.isDone());
578 threadAssertTrue(f.join() == 21);
579 threadAssertTrue(g.isDone());
580 threadAssertTrue(g.join() == 34);
581 return NoResult;
582 }
583 };
584 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
585 }
586
587 /**
588 * invokeAll(tasks) with 1 argument invokes task
589 */
590 public void testInvokeAll1() {
591 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
592 public Integer compute() {
593 FibTask f = new FibTask(8);
594 invokeAll(f);
595 threadAssertTrue(f.isDone());
596 threadAssertTrue(f.join() == 21);
597 return NoResult;
598 }
599 };
600 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
601 }
602
603 /**
604 * invokeAll(tasks) with > 2 argument invokes tasks
605 */
606 public void testInvokeAll3() {
607 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
608 public Integer compute() {
609 FibTask f = new FibTask(8);
610 FibTask g = new FibTask(9);
611 FibTask h = new FibTask(7);
612 invokeAll(f, g, h);
613 threadAssertTrue(f.isDone());
614 threadAssertTrue(f.join() == 21);
615 threadAssertTrue(g.isDone());
616 threadAssertTrue(g.join() == 34);
617 threadAssertTrue(h.isDone());
618 threadAssertTrue(h.join() == 13);
619 return NoResult;
620 }
621 };
622 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
623 }
624
625 /**
626 * invokeAll(collection) invokes all tasks in the collection
627 */
628 public void testInvokeAllCollection() {
629 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
630 public Integer compute() {
631 FibTask f = new FibTask(8);
632 FibTask g = new FibTask(9);
633 FibTask h = new FibTask(7);
634 HashSet set = new HashSet();
635 set.add(f);
636 set.add(g);
637 set.add(h);
638 invokeAll(set);
639 threadAssertTrue(f.isDone());
640 threadAssertTrue(f.join() == 21);
641 threadAssertTrue(g.isDone());
642 threadAssertTrue(g.join() == 34);
643 threadAssertTrue(h.isDone());
644 threadAssertTrue(h.join() == 13);
645 return NoResult;
646 }
647 };
648 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
649 }
650
651
652 /**
653 * invokeAll(t1, t2) throw exception if any task does
654 */
655 public void testAbnormalInvokeAll2() {
656 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
657 public Integer compute() {
658 try {
659 FibTask f = new FibTask(8);
660 FailingFibTask g = new FailingFibTask(9);
661 invokeAll(f, g);
662 shouldThrow();
663 return NoResult;
664 } catch (FJException success) {
665 }
666 return NoResult;
667 }
668 };
669 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
670 }
671
672 /**
673 * invokeAll(tasks) with 1 argument throws exception if task does
674 */
675 public void testAbnormalInvokeAll1() {
676 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
677 public Integer compute() {
678 try {
679 FailingFibTask g = new FailingFibTask(9);
680 invokeAll(g);
681 shouldThrow();
682 return NoResult;
683 } catch (FJException success) {
684 }
685 return NoResult;
686 }
687 };
688 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
689 }
690
691 /**
692 * invokeAll(tasks) with > 2 argument throws exception if any task does
693 */
694 public void testAbnormalInvokeAll3() {
695 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
696 public Integer compute() {
697 try {
698 FibTask f = new FibTask(8);
699 FailingFibTask g = new FailingFibTask(9);
700 FibTask h = new FibTask(7);
701 invokeAll(f, g, h);
702 shouldThrow();
703 return NoResult;
704 } catch (FJException success) {
705 }
706 return NoResult;
707 }
708 };
709 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
710 }
711
712 /**
713 * invokeAll(collection) throws exception if any task does
714 */
715 public void testAbnormalInvokeAllCollection() {
716 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
717 public Integer compute() {
718 try {
719 FailingFibTask f = new FailingFibTask(8);
720 FibTask g = new FibTask(9);
721 FibTask h = new FibTask(7);
722 HashSet set = new HashSet();
723 set.add(f);
724 set.add(g);
725 set.add(h);
726 invokeAll(set);
727 shouldThrow();
728 return NoResult;
729 } catch (FJException success) {
730 }
731 return NoResult;
732 }
733 };
734 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
735 }
736
737 /**
738 * tryUnfork returns true for most recent unexecuted task,
739 * and suppresses execution
740 */
741 public void testTryUnfork() {
742 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
743 public Integer compute() {
744 FibTask g = new FibTask(9);
745 g.fork();
746 FibTask f = new FibTask(8);
747 f.fork();
748 threadAssertTrue(f.tryUnfork());
749 helpQuiesce();
750 threadAssertFalse(f.isDone());
751 threadAssertTrue(g.isDone());
752 return NoResult;
753 }
754 };
755 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
756 }
757
758 /**
759 * getSurplusQueuedTaskCount returns > 0 when
760 * there are more tasks than threads
761 */
762 public void testGetSurplusQueuedTaskCount() {
763 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
764 public Integer compute() {
765 FibTask h = new FibTask(7);
766 h.fork();
767 FibTask g = new FibTask(9);
768 g.fork();
769 FibTask f = new FibTask(8);
770 f.fork();
771 threadAssertTrue(getSurplusQueuedTaskCount() > 0);
772 helpQuiesce();
773 return NoResult;
774 }
775 };
776 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
777 }
778
779 /**
780 * peekNextLocalTask returns most recent unexecuted task.
781 */
782 public void testPeekNextLocalTask() {
783 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
784 public Integer compute() {
785 FibTask g = new FibTask(9);
786 g.fork();
787 FibTask f = new FibTask(8);
788 f.fork();
789 threadAssertTrue(peekNextLocalTask() == f);
790 f.join();
791 threadAssertTrue(f.isDone());
792 helpQuiesce();
793 return NoResult;
794 }
795 };
796 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
797 }
798
799 /**
800 * pollNextLocalTask returns most recent unexecuted task
801 * without executing it
802 */
803 public void testPollNextLocalTask() {
804 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
805 public Integer compute() {
806 FibTask g = new FibTask(9);
807 g.fork();
808 FibTask f = new FibTask(8);
809 f.fork();
810 threadAssertTrue(pollNextLocalTask() == f);
811 helpQuiesce();
812 threadAssertFalse(f.isDone());
813 return NoResult;
814 }
815 };
816 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
817 }
818
819 /**
820 * pollTask returns an unexecuted task without executing it
821 */
822 public void testPollTask() {
823 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
824 public Integer compute() {
825 FibTask g = new FibTask(9);
826 g.fork();
827 FibTask f = new FibTask(8);
828 f.fork();
829 threadAssertTrue(pollTask() == f);
830 helpQuiesce();
831 threadAssertFalse(f.isDone());
832 threadAssertTrue(g.isDone());
833 return NoResult;
834 }
835 };
836 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
837 }
838
839 /**
840 * peekNextLocalTask returns least recent unexecuted task in async mode
841 */
842 public void testPeekNextLocalTaskAsync() {
843 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
844 public Integer compute() {
845 FibTask g = new FibTask(9);
846 g.fork();
847 FibTask f = new FibTask(8);
848 f.fork();
849 threadAssertTrue(peekNextLocalTask() == g);
850 f.join();
851 helpQuiesce();
852 threadAssertTrue(f.isDone());
853 return NoResult;
854 }
855 };
856 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
857 }
858
859 /**
860 * pollNextLocalTask returns least recent unexecuted task
861 * without executing it, in async mode
862 */
863 public void testPollNextLocalTaskAsync() {
864 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
865 public Integer compute() {
866 FibTask g = new FibTask(9);
867 g.fork();
868 FibTask f = new FibTask(8);
869 f.fork();
870 threadAssertTrue(pollNextLocalTask() == g);
871 helpQuiesce();
872 threadAssertTrue(f.isDone());
873 threadAssertFalse(g.isDone());
874 return NoResult;
875 }
876 };
877 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
878 }
879
880 /**
881 * pollTask returns an unexecuted task
882 * without executing it, in async mode
883 */
884 public void testPollTaskAsync() {
885 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
886 public Integer compute() {
887 FibTask g = new FibTask(9);
888 g.fork();
889 FibTask f = new FibTask(8);
890 f.fork();
891 threadAssertTrue(pollTask() == g);
892 helpQuiesce();
893 threadAssertTrue(f.isDone());
894 threadAssertFalse(g.isDone());
895 return NoResult;
896 }
897 };
898 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
899 }
900
901 }