4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
> |
|
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
+ |
import java.util.concurrent.CountedCompleter; |
13 |
|
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
12 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
16 |
|
import java.util.concurrent.RecursiveAction; |
14 |
– |
import java.util.concurrent.CountedCompleter; |
15 |
– |
import java.util.concurrent.ThreadLocalRandom; |
16 |
– |
import java.util.concurrent.TimeUnit; |
17 |
|
import java.util.concurrent.TimeoutException; |
18 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import java.util.Arrays; |
21 |
< |
import java.util.HashSet; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class ForkJoinPool8Test extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
|
27 |
|
public static Test suite() { |
184 |
|
final int number; |
185 |
|
int result; |
186 |
|
FibAction(int n) { number = n; } |
187 |
< |
public void realCompute() { |
187 |
> |
protected void realCompute() { |
188 |
|
int n = number; |
189 |
|
if (n <= 1) |
190 |
|
result = n; |
222 |
|
*/ |
223 |
|
public void testInvoke() { |
224 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
225 |
< |
public void realCompute() { |
225 |
> |
protected void realCompute() { |
226 |
|
FibAction f = new FibAction(8); |
227 |
|
assertNull(f.invoke()); |
228 |
|
assertEquals(21, f.result); |
238 |
|
*/ |
239 |
|
public void testQuietlyInvoke() { |
240 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
241 |
< |
public void realCompute() { |
241 |
> |
protected void realCompute() { |
242 |
|
FibAction f = new FibAction(8); |
243 |
|
f.quietlyInvoke(); |
244 |
|
assertEquals(21, f.result); |
252 |
|
*/ |
253 |
|
public void testForkJoin() { |
254 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
255 |
< |
public void realCompute() { |
255 |
> |
protected void realCompute() { |
256 |
|
FibAction f = new FibAction(8); |
257 |
|
assertSame(f, f.fork()); |
258 |
|
assertNull(f.join()); |
267 |
|
*/ |
268 |
|
public void testJoinIgnoresInterrupts() { |
269 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
270 |
< |
public void realCompute() { |
270 |
> |
protected void realCompute() { |
271 |
|
FibAction f = new FibAction(8); |
272 |
|
final Thread myself = Thread.currentThread(); |
273 |
|
|
339 |
|
checkInvoke(a); |
340 |
|
} |
341 |
|
|
343 |
– |
|
342 |
|
/** |
343 |
|
* get of a forked task returns when task completes |
344 |
|
*/ |
345 |
|
public void testForkGet() { |
346 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
347 |
< |
public void realCompute() throws Exception { |
347 |
> |
protected void realCompute() throws Exception { |
348 |
|
FibAction f = new FibAction(8); |
349 |
|
assertSame(f, f.fork()); |
350 |
|
assertNull(f.get()); |
359 |
|
*/ |
360 |
|
public void testForkTimedGet() { |
361 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
362 |
< |
public void realCompute() throws Exception { |
362 |
> |
protected void realCompute() throws Exception { |
363 |
|
FibAction f = new FibAction(8); |
364 |
|
assertSame(f, f.fork()); |
365 |
|
assertNull(f.get(5L, SECONDS)); |
374 |
|
*/ |
375 |
|
public void testForkTimedGetNPE() { |
376 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
377 |
< |
public void realCompute() throws Exception { |
377 |
> |
protected void realCompute() throws Exception { |
378 |
|
FibAction f = new FibAction(8); |
379 |
|
assertSame(f, f.fork()); |
380 |
|
try { |
390 |
|
*/ |
391 |
|
public void testForkQuietlyJoin() { |
392 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
393 |
< |
public void realCompute() { |
393 |
> |
protected void realCompute() { |
394 |
|
FibAction f = new FibAction(8); |
395 |
|
assertSame(f, f.fork()); |
396 |
|
f.quietlyJoin(); |
405 |
|
*/ |
406 |
|
public void testAbnormalInvoke() { |
407 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
408 |
< |
public void realCompute() { |
408 |
> |
protected void realCompute() { |
409 |
|
FailingFibAction f = new FailingFibAction(8); |
410 |
|
try { |
411 |
|
f.invoke(); |
422 |
|
*/ |
423 |
|
public void testAbnormalQuietlyInvoke() { |
424 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
425 |
< |
public void realCompute() { |
425 |
> |
protected void realCompute() { |
426 |
|
FailingFibAction f = new FailingFibAction(8); |
427 |
|
f.quietlyInvoke(); |
428 |
|
assertTrue(f.getException() instanceof FJException); |
436 |
|
*/ |
437 |
|
public void testAbnormalForkJoin() { |
438 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
439 |
< |
public void realCompute() { |
439 |
> |
protected void realCompute() { |
440 |
|
FailingFibAction f = new FailingFibAction(8); |
441 |
|
assertSame(f, f.fork()); |
442 |
|
try { |
454 |
|
*/ |
455 |
|
public void testAbnormalForkGet() { |
456 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
457 |
< |
public void realCompute() throws Exception { |
457 |
> |
protected void realCompute() throws Exception { |
458 |
|
FailingFibAction f = new FailingFibAction(8); |
459 |
|
assertSame(f, f.fork()); |
460 |
|
try { |
474 |
|
*/ |
475 |
|
public void testAbnormalForkTimedGet() { |
476 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
477 |
< |
public void realCompute() throws Exception { |
477 |
> |
protected void realCompute() throws Exception { |
478 |
|
FailingFibAction f = new FailingFibAction(8); |
479 |
|
assertSame(f, f.fork()); |
480 |
|
try { |
481 |
< |
f.get(5L, TimeUnit.SECONDS); |
481 |
> |
f.get(5L, SECONDS); |
482 |
|
shouldThrow(); |
483 |
|
} catch (ExecutionException success) { |
484 |
|
Throwable cause = success.getCause(); |
494 |
|
*/ |
495 |
|
public void testAbnormalForkQuietlyJoin() { |
496 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
497 |
< |
public void realCompute() { |
497 |
> |
protected void realCompute() { |
498 |
|
FailingFibAction f = new FailingFibAction(8); |
499 |
|
assertSame(f, f.fork()); |
500 |
|
f.quietlyJoin(); |
509 |
|
*/ |
510 |
|
public void testCancelledInvoke() { |
511 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
512 |
< |
public void realCompute() { |
512 |
> |
protected void realCompute() { |
513 |
|
FibAction f = new FibAction(8); |
514 |
|
assertTrue(f.cancel(true)); |
515 |
|
try { |
527 |
|
*/ |
528 |
|
public void testCancelledForkJoin() { |
529 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
530 |
< |
public void realCompute() { |
530 |
> |
protected void realCompute() { |
531 |
|
FibAction f = new FibAction(8); |
532 |
|
assertTrue(f.cancel(true)); |
533 |
|
assertSame(f, f.fork()); |
546 |
|
*/ |
547 |
|
public void testCancelledForkGet() { |
548 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
549 |
< |
public void realCompute() throws Exception { |
549 |
> |
protected void realCompute() throws Exception { |
550 |
|
FibAction f = new FibAction(8); |
551 |
|
assertTrue(f.cancel(true)); |
552 |
|
assertSame(f, f.fork()); |
565 |
|
*/ |
566 |
|
public void testCancelledForkTimedGet() { |
567 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
568 |
< |
public void realCompute() throws Exception { |
568 |
> |
protected void realCompute() throws Exception { |
569 |
|
FibAction f = new FibAction(8); |
570 |
|
assertTrue(f.cancel(true)); |
571 |
|
assertSame(f, f.fork()); |
584 |
|
*/ |
585 |
|
public void testCancelledForkQuietlyJoin() { |
586 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
587 |
< |
public void realCompute() { |
587 |
> |
protected void realCompute() { |
588 |
|
FibAction f = new FibAction(8); |
589 |
|
assertTrue(f.cancel(true)); |
590 |
|
assertSame(f, f.fork()); |
599 |
|
*/ |
600 |
|
public void testInForkJoinPool2() { |
601 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
602 |
< |
public void realCompute() { |
602 |
> |
protected void realCompute() { |
603 |
|
assertFalse(inForkJoinPool()); |
604 |
|
}}; |
605 |
|
assertNull(a.invoke()); |
610 |
|
*/ |
611 |
|
public void testReinitialize() { |
612 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
613 |
< |
public void realCompute() { |
613 |
> |
protected void realCompute() { |
614 |
|
FibAction f = new FibAction(8); |
615 |
|
checkNotDone(f); |
616 |
|
|
630 |
|
*/ |
631 |
|
public void testReinitializeAbnormal() { |
632 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
633 |
< |
public void realCompute() { |
633 |
> |
protected void realCompute() { |
634 |
|
FailingFibAction f = new FailingFibAction(8); |
635 |
|
checkNotDone(f); |
636 |
|
|
653 |
|
*/ |
654 |
|
public void testCompleteExceptionally() { |
655 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
656 |
< |
public void realCompute() { |
656 |
> |
protected void realCompute() { |
657 |
|
FibAction f = new FibAction(8); |
658 |
|
f.completeExceptionally(new FJException()); |
659 |
|
try { |
671 |
|
*/ |
672 |
|
public void testComplete() { |
673 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
674 |
< |
public void realCompute() { |
674 |
> |
protected void realCompute() { |
675 |
|
FibAction f = new FibAction(8); |
676 |
|
f.complete(null); |
677 |
|
assertNull(f.invoke()); |
686 |
|
*/ |
687 |
|
public void testInvokeAll2() { |
688 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
689 |
< |
public void realCompute() { |
689 |
> |
protected void realCompute() { |
690 |
|
FibAction f = new FibAction(8); |
691 |
|
FibAction g = new FibAction(9); |
692 |
|
invokeAll(f, g); |
703 |
|
*/ |
704 |
|
public void testInvokeAll1() { |
705 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
706 |
< |
public void realCompute() { |
706 |
> |
protected void realCompute() { |
707 |
|
FibAction f = new FibAction(8); |
708 |
|
invokeAll(f); |
709 |
|
checkCompletedNormally(f); |
717 |
|
*/ |
718 |
|
public void testInvokeAll3() { |
719 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
720 |
< |
public void realCompute() { |
720 |
> |
protected void realCompute() { |
721 |
|
FibAction f = new FibAction(8); |
722 |
|
FibAction g = new FibAction(9); |
723 |
|
FibAction h = new FibAction(7); |
740 |
|
*/ |
741 |
|
public void testInvokeAllCollection() { |
742 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
743 |
< |
public void realCompute() { |
743 |
> |
protected void realCompute() { |
744 |
|
FibAction f = new FibAction(8); |
745 |
|
FibAction g = new FibAction(9); |
746 |
|
FibAction h = new FibAction(7); |
767 |
|
*/ |
768 |
|
public void testInvokeAllNPE() { |
769 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
< |
public void realCompute() { |
770 |
> |
protected void realCompute() { |
771 |
|
FibAction f = new FibAction(8); |
772 |
|
FibAction g = new FibAction(9); |
773 |
|
FibAction h = null; |
784 |
|
*/ |
785 |
|
public void testAbnormalInvokeAll2() { |
786 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
787 |
< |
public void realCompute() { |
787 |
> |
protected void realCompute() { |
788 |
|
FibAction f = new FibAction(8); |
789 |
|
FailingFibAction g = new FailingFibAction(9); |
790 |
|
try { |
802 |
|
*/ |
803 |
|
public void testAbnormalInvokeAll1() { |
804 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
805 |
< |
public void realCompute() { |
805 |
> |
protected void realCompute() { |
806 |
|
FailingFibAction g = new FailingFibAction(9); |
807 |
|
try { |
808 |
|
invokeAll(g); |
819 |
|
*/ |
820 |
|
public void testAbnormalInvokeAll3() { |
821 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
822 |
< |
public void realCompute() { |
822 |
> |
protected void realCompute() { |
823 |
|
FibAction f = new FibAction(8); |
824 |
|
FailingFibAction g = new FailingFibAction(9); |
825 |
|
FibAction h = new FibAction(7); |
838 |
|
*/ |
839 |
|
public void testAbnormalInvokeAllCollection() { |
840 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
841 |
< |
public void realCompute() { |
841 |
> |
protected void realCompute() { |
842 |
|
FailingFibAction f = new FailingFibAction(8); |
843 |
|
FibAction g = new FibAction(9); |
844 |
|
FibAction h = new FibAction(7); |
858 |
|
|
859 |
|
// CountedCompleter versions |
860 |
|
|
863 |
– |
public abstract class CheckedFJTask extends RecursiveAction { |
864 |
– |
protected abstract void realCompute() throws Throwable; |
865 |
– |
|
866 |
– |
public final void compute() { |
867 |
– |
try { |
868 |
– |
realCompute(); |
869 |
– |
} catch (Throwable t) { |
870 |
– |
threadUnexpectedException(t); |
871 |
– |
} |
872 |
– |
} |
873 |
– |
} |
874 |
– |
|
861 |
|
abstract static class CCF extends CountedCompleter { |
862 |
|
int number; |
863 |
|
int rnumber; |
966 |
|
* completed tasks; getRawResult returns null. |
967 |
|
*/ |
968 |
|
public void testInvokeCC() { |
969 |
< |
ForkJoinTask a = new CheckedFJTask() { |
970 |
< |
public void realCompute() { |
969 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
970 |
> |
protected void realCompute() { |
971 |
|
CCF f = new LCCF(null, 8); |
972 |
|
assertNull(f.invoke()); |
973 |
|
assertEquals(21, f.number); |
982 |
|
* completed tasks |
983 |
|
*/ |
984 |
|
public void testQuietlyInvokeCC() { |
985 |
< |
ForkJoinTask a = new CheckedFJTask() { |
986 |
< |
public void realCompute() { |
985 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
986 |
> |
protected void realCompute() { |
987 |
|
CCF f = new LCCF(null, 8); |
988 |
|
f.quietlyInvoke(); |
989 |
|
assertEquals(21, f.number); |
996 |
|
* join of a forked task returns when task completes |
997 |
|
*/ |
998 |
|
public void testForkJoinCC() { |
999 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1000 |
< |
public void realCompute() { |
999 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1000 |
> |
protected void realCompute() { |
1001 |
|
CCF f = new LCCF(null, 8); |
1002 |
|
assertSame(f, f.fork()); |
1003 |
|
assertNull(f.join()); |
1011 |
|
* get of a forked task returns when task completes |
1012 |
|
*/ |
1013 |
|
public void testForkGetCC() { |
1014 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1015 |
< |
public void realCompute() throws Exception { |
1014 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1015 |
> |
protected void realCompute() throws Exception { |
1016 |
|
CCF f = new LCCF(null, 8); |
1017 |
|
assertSame(f, f.fork()); |
1018 |
|
assertNull(f.get()); |
1026 |
|
* timed get of a forked task returns when task completes |
1027 |
|
*/ |
1028 |
|
public void testForkTimedGetCC() { |
1029 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1030 |
< |
public void realCompute() throws Exception { |
1029 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1030 |
> |
protected void realCompute() throws Exception { |
1031 |
|
CCF f = new LCCF(null, 8); |
1032 |
|
assertSame(f, f.fork()); |
1033 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1041 |
|
* timed get with null time unit throws NPE |
1042 |
|
*/ |
1043 |
|
public void testForkTimedGetNPECC() { |
1044 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1045 |
< |
public void realCompute() throws Exception { |
1044 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1045 |
> |
protected void realCompute() throws Exception { |
1046 |
|
CCF f = new LCCF(null, 8); |
1047 |
|
assertSame(f, f.fork()); |
1048 |
|
try { |
1057 |
|
* quietlyJoin of a forked task returns when task completes |
1058 |
|
*/ |
1059 |
|
public void testForkQuietlyJoinCC() { |
1060 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1061 |
< |
public void realCompute() { |
1060 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1061 |
> |
protected void realCompute() { |
1062 |
|
CCF f = new LCCF(null, 8); |
1063 |
|
assertSame(f, f.fork()); |
1064 |
|
f.quietlyJoin(); |
1072 |
|
* invoke task throws exception when task completes abnormally |
1073 |
|
*/ |
1074 |
|
public void testAbnormalInvokeCC() { |
1075 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1076 |
< |
public void realCompute() { |
1075 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1076 |
> |
protected void realCompute() { |
1077 |
|
FailingCCF f = new LFCCF(null, 8); |
1078 |
|
try { |
1079 |
|
f.invoke(); |
1089 |
|
* quietlyInvoke task returns when task completes abnormally |
1090 |
|
*/ |
1091 |
|
public void testAbnormalQuietlyInvokeCC() { |
1092 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1093 |
< |
public void realCompute() { |
1092 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1093 |
> |
protected void realCompute() { |
1094 |
|
FailingCCF f = new LFCCF(null, 8); |
1095 |
|
f.quietlyInvoke(); |
1096 |
|
assertTrue(f.getException() instanceof FJException); |
1103 |
|
* join of a forked task throws exception when task completes abnormally |
1104 |
|
*/ |
1105 |
|
public void testAbnormalForkJoinCC() { |
1106 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1107 |
< |
public void realCompute() { |
1106 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1107 |
> |
protected void realCompute() { |
1108 |
|
FailingCCF f = new LFCCF(null, 8); |
1109 |
|
assertSame(f, f.fork()); |
1110 |
|
try { |
1121 |
|
* get of a forked task throws exception when task completes abnormally |
1122 |
|
*/ |
1123 |
|
public void testAbnormalForkGetCC() { |
1124 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1125 |
< |
public void realCompute() throws Exception { |
1124 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1125 |
> |
protected void realCompute() throws Exception { |
1126 |
|
FailingCCF f = new LFCCF(null, 8); |
1127 |
|
assertSame(f, f.fork()); |
1128 |
|
try { |
1141 |
|
* timed get of a forked task throws exception when task completes abnormally |
1142 |
|
*/ |
1143 |
|
public void testAbnormalForkTimedGetCC() { |
1144 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1145 |
< |
public void realCompute() throws Exception { |
1144 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1145 |
> |
protected void realCompute() throws Exception { |
1146 |
|
FailingCCF f = new LFCCF(null, 8); |
1147 |
|
assertSame(f, f.fork()); |
1148 |
|
try { |
1161 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
1162 |
|
*/ |
1163 |
|
public void testAbnormalForkQuietlyJoinCC() { |
1164 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1165 |
< |
public void realCompute() { |
1164 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1165 |
> |
protected void realCompute() { |
1166 |
|
FailingCCF f = new LFCCF(null, 8); |
1167 |
|
assertSame(f, f.fork()); |
1168 |
|
f.quietlyJoin(); |
1176 |
|
* invoke task throws exception when task cancelled |
1177 |
|
*/ |
1178 |
|
public void testCancelledInvokeCC() { |
1179 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1180 |
< |
public void realCompute() { |
1179 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1180 |
> |
protected void realCompute() { |
1181 |
|
CCF f = new LCCF(null, 8); |
1182 |
|
assertTrue(f.cancel(true)); |
1183 |
|
try { |
1194 |
|
* join of a forked task throws exception when task cancelled |
1195 |
|
*/ |
1196 |
|
public void testCancelledForkJoinCC() { |
1197 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1198 |
< |
public void realCompute() { |
1197 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1198 |
> |
protected void realCompute() { |
1199 |
|
CCF f = new LCCF(null, 8); |
1200 |
|
assertTrue(f.cancel(true)); |
1201 |
|
assertSame(f, f.fork()); |
1213 |
|
* get of a forked task throws exception when task cancelled |
1214 |
|
*/ |
1215 |
|
public void testCancelledForkGetCC() { |
1216 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1217 |
< |
public void realCompute() throws Exception { |
1216 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1217 |
> |
protected void realCompute() throws Exception { |
1218 |
|
CCF f = new LCCF(null, 8); |
1219 |
|
assertTrue(f.cancel(true)); |
1220 |
|
assertSame(f, f.fork()); |
1232 |
|
* timed get of a forked task throws exception when task cancelled |
1233 |
|
*/ |
1234 |
|
public void testCancelledForkTimedGetCC() throws Exception { |
1235 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1236 |
< |
public void realCompute() throws Exception { |
1235 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1236 |
> |
protected void realCompute() throws Exception { |
1237 |
|
CCF f = new LCCF(null, 8); |
1238 |
|
assertTrue(f.cancel(true)); |
1239 |
|
assertSame(f, f.fork()); |
1251 |
|
* quietlyJoin of a forked task returns when task cancelled |
1252 |
|
*/ |
1253 |
|
public void testCancelledForkQuietlyJoinCC() { |
1254 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1255 |
< |
public void realCompute() { |
1254 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1255 |
> |
protected void realCompute() { |
1256 |
|
CCF f = new LCCF(null, 8); |
1257 |
|
assertTrue(f.cancel(true)); |
1258 |
|
assertSame(f, f.fork()); |
1266 |
|
* getPool of non-FJ task returns null |
1267 |
|
*/ |
1268 |
|
public void testGetPool2CC() { |
1269 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1270 |
< |
public void realCompute() { |
1269 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1270 |
> |
protected void realCompute() { |
1271 |
|
assertNull(getPool()); |
1272 |
|
}}; |
1273 |
|
assertNull(a.invoke()); |
1277 |
|
* inForkJoinPool of non-FJ task returns false |
1278 |
|
*/ |
1279 |
|
public void testInForkJoinPool2CC() { |
1280 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1281 |
< |
public void realCompute() { |
1280 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1281 |
> |
protected void realCompute() { |
1282 |
|
assertFalse(inForkJoinPool()); |
1283 |
|
}}; |
1284 |
|
assertNull(a.invoke()); |
1288 |
|
* setRawResult(null) succeeds |
1289 |
|
*/ |
1290 |
|
public void testSetRawResultCC() { |
1291 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1292 |
< |
public void realCompute() { |
1291 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1292 |
> |
protected void realCompute() { |
1293 |
|
setRawResult(null); |
1294 |
|
assertNull(getRawResult()); |
1295 |
|
}}; |
1300 |
|
* invoke task throws exception after invoking completeExceptionally |
1301 |
|
*/ |
1302 |
|
public void testCompleteExceptionally2CC() { |
1303 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1304 |
< |
public void realCompute() { |
1303 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1304 |
> |
protected void realCompute() { |
1305 |
|
CCF f = new LCCF(null, 8); |
1306 |
|
f.completeExceptionally(new FJException()); |
1307 |
|
try { |
1318 |
|
* invokeAll(t1, t2) invokes all task arguments |
1319 |
|
*/ |
1320 |
|
public void testInvokeAll2CC() { |
1321 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1322 |
< |
public void realCompute() { |
1321 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1322 |
> |
protected void realCompute() { |
1323 |
|
CCF f = new LCCF(null, 8); |
1324 |
|
CCF g = new LCCF(null, 9); |
1325 |
|
invokeAll(f, g); |
1335 |
|
* invokeAll(tasks) with 1 argument invokes task |
1336 |
|
*/ |
1337 |
|
public void testInvokeAll1CC() { |
1338 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1339 |
< |
public void realCompute() { |
1338 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1339 |
> |
protected void realCompute() { |
1340 |
|
CCF f = new LCCF(null, 8); |
1341 |
|
invokeAll(f); |
1342 |
|
checkCompletedNormally(f); |
1349 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1350 |
|
*/ |
1351 |
|
public void testInvokeAll3CC() { |
1352 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1353 |
< |
public void realCompute() { |
1352 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1353 |
> |
protected void realCompute() { |
1354 |
|
CCF f = new LCCF(null, 8); |
1355 |
|
CCF g = new LCCF(null, 9); |
1356 |
|
CCF h = new LCCF(null, 7); |
1369 |
|
* invokeAll(collection) invokes all tasks in the collection |
1370 |
|
*/ |
1371 |
|
public void testInvokeAllCollectionCC() { |
1372 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1373 |
< |
public void realCompute() { |
1372 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1373 |
> |
protected void realCompute() { |
1374 |
|
CCF f = new LCCF(null, 8); |
1375 |
|
CCF g = new LCCF(null, 9); |
1376 |
|
CCF h = new LCCF(null, 7); |
1393 |
|
* invokeAll(tasks) with any null task throws NPE |
1394 |
|
*/ |
1395 |
|
public void testInvokeAllNPECC() { |
1396 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1397 |
< |
public void realCompute() { |
1396 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1397 |
> |
protected void realCompute() { |
1398 |
|
CCF f = new LCCF(null, 8); |
1399 |
|
CCF g = new LCCF(null, 9); |
1400 |
|
CCF h = null; |
1410 |
|
* invokeAll(t1, t2) throw exception if any task does |
1411 |
|
*/ |
1412 |
|
public void testAbnormalInvokeAll2CC() { |
1413 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1414 |
< |
public void realCompute() { |
1413 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1414 |
> |
protected void realCompute() { |
1415 |
|
CCF f = new LCCF(null, 8); |
1416 |
|
FailingCCF g = new LFCCF(null, 9); |
1417 |
|
try { |
1428 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1429 |
|
*/ |
1430 |
|
public void testAbnormalInvokeAll1CC() { |
1431 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1432 |
< |
public void realCompute() { |
1431 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1432 |
> |
protected void realCompute() { |
1433 |
|
FailingCCF g = new LFCCF(null, 9); |
1434 |
|
try { |
1435 |
|
invokeAll(g); |
1445 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1446 |
|
*/ |
1447 |
|
public void testAbnormalInvokeAll3CC() { |
1448 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1449 |
< |
public void realCompute() { |
1448 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1449 |
> |
protected void realCompute() { |
1450 |
|
CCF f = new LCCF(null, 8); |
1451 |
|
FailingCCF g = new LFCCF(null, 9); |
1452 |
|
CCF h = new LCCF(null, 7); |
1461 |
|
} |
1462 |
|
|
1463 |
|
/** |
1464 |
< |
* invokeAll(collection) throws exception if any task does |
1464 |
> |
* invokeAll(collection) throws exception if any task does |
1465 |
|
*/ |
1466 |
|
public void testAbnormalInvokeAllCollectionCC() { |
1467 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1468 |
< |
public void realCompute() { |
1467 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1468 |
> |
protected void realCompute() { |
1469 |
|
FailingCCF f = new LFCCF(null, 8); |
1470 |
|
CCF g = new LCCF(null, 9); |
1471 |
|
CCF h = new LCCF(null, 7); |
1483 |
|
checkInvoke(a); |
1484 |
|
} |
1485 |
|
|
1486 |
+ |
/** |
1487 |
+ |
* awaitQuiescence by a worker is equivalent in effect to |
1488 |
+ |
* ForkJoinTask.helpQuiesce() |
1489 |
+ |
*/ |
1490 |
+ |
public void testAwaitQuiescence1() throws Exception { |
1491 |
+ |
final ForkJoinPool p = new ForkJoinPool(); |
1492 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1493 |
+ |
final long startTime = System.nanoTime(); |
1494 |
+ |
assertTrue(p.isQuiescent()); |
1495 |
+ |
ForkJoinTask a = new CheckedRecursiveAction() { |
1496 |
+ |
protected void realCompute() { |
1497 |
+ |
FibAction f = new FibAction(8); |
1498 |
+ |
assertSame(f, f.fork()); |
1499 |
+ |
assertSame(p, ForkJoinTask.getPool()); |
1500 |
+ |
boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); |
1501 |
+ |
assertTrue(quiescent); |
1502 |
+ |
assertFalse(p.isQuiescent()); |
1503 |
+ |
while (!f.isDone()) { |
1504 |
+ |
assertFalse(p.getAsyncMode()); |
1505 |
+ |
assertFalse(p.isShutdown()); |
1506 |
+ |
assertFalse(p.isTerminating()); |
1507 |
+ |
assertFalse(p.isTerminated()); |
1508 |
+ |
Thread.yield(); |
1509 |
+ |
} |
1510 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1511 |
+ |
assertFalse(p.isQuiescent()); |
1512 |
+ |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1513 |
+ |
assertEquals(21, f.result); |
1514 |
+ |
}}; |
1515 |
+ |
p.execute(a); |
1516 |
+ |
while (!a.isDone() || !p.isQuiescent()) { |
1517 |
+ |
assertFalse(p.getAsyncMode()); |
1518 |
+ |
assertFalse(p.isShutdown()); |
1519 |
+ |
assertFalse(p.isTerminating()); |
1520 |
+ |
assertFalse(p.isTerminated()); |
1521 |
+ |
Thread.yield(); |
1522 |
+ |
} |
1523 |
+ |
assertEquals(0, p.getQueuedTaskCount()); |
1524 |
+ |
assertFalse(p.getAsyncMode()); |
1525 |
+ |
assertEquals(0, p.getQueuedSubmissionCount()); |
1526 |
+ |
assertFalse(p.hasQueuedSubmissions()); |
1527 |
+ |
while (p.getActiveThreadCount() != 0 |
1528 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1529 |
+ |
Thread.yield(); |
1530 |
+ |
assertFalse(p.isShutdown()); |
1531 |
+ |
assertFalse(p.isTerminating()); |
1532 |
+ |
assertFalse(p.isTerminated()); |
1533 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1534 |
+ |
} |
1535 |
+ |
} |
1536 |
+ |
|
1537 |
+ |
/** |
1538 |
+ |
* awaitQuiescence returns when pool isQuiescent() or the indicated |
1539 |
+ |
* timeout elapsed |
1540 |
+ |
*/ |
1541 |
+ |
public void testAwaitQuiescence2() throws Exception { |
1542 |
+ |
/** |
1543 |
+ |
* """It is possible to disable or limit the use of threads in the |
1544 |
+ |
* common pool by setting the parallelism property to zero. However |
1545 |
+ |
* doing so may cause unjoined tasks to never be executed.""" |
1546 |
+ |
*/ |
1547 |
+ |
if ("0".equals(System.getProperty( |
1548 |
+ |
"java.util.concurrent.ForkJoinPool.common.parallelism"))) |
1549 |
+ |
return; |
1550 |
+ |
final ForkJoinPool p = new ForkJoinPool(); |
1551 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1552 |
+ |
assertTrue(p.isQuiescent()); |
1553 |
+ |
final long startTime = System.nanoTime(); |
1554 |
+ |
ForkJoinTask a = new CheckedRecursiveAction() { |
1555 |
+ |
protected void realCompute() { |
1556 |
+ |
FibAction f = new FibAction(8); |
1557 |
+ |
assertSame(f, f.fork()); |
1558 |
+ |
while (!f.isDone() |
1559 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) { |
1560 |
+ |
assertFalse(p.getAsyncMode()); |
1561 |
+ |
assertFalse(p.isShutdown()); |
1562 |
+ |
assertFalse(p.isTerminating()); |
1563 |
+ |
assertFalse(p.isTerminated()); |
1564 |
+ |
Thread.yield(); |
1565 |
+ |
} |
1566 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1567 |
+ |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1568 |
+ |
assertEquals(21, f.result); |
1569 |
+ |
}}; |
1570 |
+ |
p.execute(a); |
1571 |
+ |
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); |
1572 |
+ |
assertTrue(p.isQuiescent()); |
1573 |
+ |
assertTrue(a.isDone()); |
1574 |
+ |
assertEquals(0, p.getQueuedTaskCount()); |
1575 |
+ |
assertFalse(p.getAsyncMode()); |
1576 |
+ |
assertEquals(0, p.getQueuedSubmissionCount()); |
1577 |
+ |
assertFalse(p.hasQueuedSubmissions()); |
1578 |
+ |
while (p.getActiveThreadCount() != 0 |
1579 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1580 |
+ |
Thread.yield(); |
1581 |
+ |
assertFalse(p.isShutdown()); |
1582 |
+ |
assertFalse(p.isTerminating()); |
1583 |
+ |
assertFalse(p.isTerminated()); |
1584 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1585 |
+ |
} |
1586 |
+ |
} |
1587 |
+ |
|
1588 |
|
} |