4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
> |
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
11 |
+ |
import java.util.concurrent.CountedCompleter; |
12 |
|
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
import java.util.concurrent.ForkJoinTask; |
15 |
|
import java.util.concurrent.RecursiveAction; |
13 |
– |
import java.util.concurrent.CountedCompleter; |
16 |
|
import java.util.concurrent.TimeoutException; |
17 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
18 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
19 |
< |
import java.util.HashSet; |
17 |
> |
|
18 |
> |
import junit.framework.Test; |
19 |
> |
import junit.framework.TestSuite; |
20 |
|
|
21 |
|
public class ForkJoinPool8Test extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
|
26 |
|
public static Test suite() { |
59 |
|
* checkInvoke. |
60 |
|
*/ |
61 |
|
|
62 |
< |
private void checkInvoke(ForkJoinTask a) { |
62 |
> |
private void checkInvoke(ForkJoinTask<?> a) { |
63 |
|
checkNotDone(a); |
64 |
|
assertNull(a.invoke()); |
65 |
|
checkCompletedNormally(a); |
66 |
|
} |
67 |
|
|
68 |
< |
void checkNotDone(ForkJoinTask a) { |
68 |
> |
void checkNotDone(ForkJoinTask<?> a) { |
69 |
|
assertFalse(a.isDone()); |
70 |
|
assertFalse(a.isCompletedNormally()); |
71 |
|
assertFalse(a.isCompletedAbnormally()); |
83 |
|
|
84 |
|
Thread.currentThread().interrupt(); |
85 |
|
try { |
86 |
< |
a.get(5L, SECONDS); |
86 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
87 |
|
shouldThrow(); |
88 |
|
} catch (InterruptedException success) { |
89 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
90 |
|
} |
91 |
|
|
92 |
|
try { |
93 |
< |
a.get(0L, SECONDS); |
93 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
94 |
|
shouldThrow(); |
95 |
|
} catch (TimeoutException success) { |
96 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
97 |
|
} |
98 |
|
|
99 |
< |
void checkCompletedNormally(ForkJoinTask a) { |
99 |
> |
void checkCompletedNormally(ForkJoinTask<?> a) { |
100 |
|
assertTrue(a.isDone()); |
101 |
|
assertFalse(a.isCancelled()); |
102 |
|
assertTrue(a.isCompletedNormally()); |
106 |
|
assertNull(a.join()); |
107 |
|
assertFalse(a.cancel(false)); |
108 |
|
assertFalse(a.cancel(true)); |
109 |
+ |
|
110 |
+ |
Object v1 = null, v2 = null; |
111 |
|
try { |
112 |
< |
assertNull(a.get()); |
113 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
110 |
< |
try { |
111 |
< |
assertNull(a.get(5L, SECONDS)); |
112 |
> |
v1 = a.get(); |
113 |
> |
v2 = a.get(randomTimeout(), randomTimeUnit()); |
114 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
+ |
assertNull(v1); |
116 |
+ |
assertNull(v2); |
117 |
|
} |
118 |
|
|
119 |
< |
void checkCancelled(ForkJoinTask a) { |
119 |
> |
void checkCancelled(ForkJoinTask<?> a) { |
120 |
|
assertTrue(a.isDone()); |
121 |
|
assertTrue(a.isCancelled()); |
122 |
|
assertFalse(a.isCompletedNormally()); |
137 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
138 |
|
|
139 |
|
try { |
140 |
< |
a.get(5L, SECONDS); |
140 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
141 |
|
shouldThrow(); |
142 |
|
} catch (CancellationException success) { |
143 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
144 |
|
} |
145 |
|
|
146 |
< |
void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { |
146 |
> |
void checkCompletedAbnormally(ForkJoinTask<?> a, Throwable t) { |
147 |
|
assertTrue(a.isDone()); |
148 |
|
assertFalse(a.isCancelled()); |
149 |
|
assertFalse(a.isCompletedNormally()); |
168 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
169 |
|
|
170 |
|
try { |
171 |
< |
a.get(5L, SECONDS); |
171 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
172 |
|
shouldThrow(); |
173 |
|
} catch (ExecutionException success) { |
174 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
180 |
|
public FJException(Throwable cause) { super(cause); } |
181 |
|
} |
182 |
|
|
183 |
< |
// A simple recursive action for testing |
183 |
> |
/** A simple recursive action for testing. */ |
184 |
|
final class FibAction extends CheckedRecursiveAction { |
185 |
|
final int number; |
186 |
|
int result; |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
< |
// A recursive action failing in base case |
201 |
> |
/** A recursive action failing in base case. */ |
202 |
|
static final class FailingFibAction extends RecursiveAction { |
203 |
|
final int number; |
204 |
|
int result; |
270 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
271 |
|
protected void realCompute() { |
272 |
|
FibAction f = new FibAction(8); |
273 |
< |
final Thread myself = Thread.currentThread(); |
273 |
> |
final Thread currentThread = Thread.currentThread(); |
274 |
|
|
275 |
|
// test join() |
276 |
|
assertSame(f, f.fork()); |
277 |
< |
myself.interrupt(); |
274 |
< |
assertTrue(myself.isInterrupted()); |
277 |
> |
currentThread.interrupt(); |
278 |
|
assertNull(f.join()); |
279 |
|
Thread.interrupted(); |
280 |
|
assertEquals(21, f.result); |
283 |
|
f = new FibAction(8); |
284 |
|
f.cancel(true); |
285 |
|
assertSame(f, f.fork()); |
286 |
< |
myself.interrupt(); |
284 |
< |
assertTrue(myself.isInterrupted()); |
286 |
> |
currentThread.interrupt(); |
287 |
|
try { |
288 |
|
f.join(); |
289 |
|
shouldThrow(); |
295 |
|
f = new FibAction(8); |
296 |
|
f.completeExceptionally(new FJException()); |
297 |
|
assertSame(f, f.fork()); |
298 |
< |
myself.interrupt(); |
297 |
< |
assertTrue(myself.isInterrupted()); |
298 |
> |
currentThread.interrupt(); |
299 |
|
try { |
300 |
|
f.join(); |
301 |
|
shouldThrow(); |
307 |
|
// test quietlyJoin() |
308 |
|
f = new FibAction(8); |
309 |
|
assertSame(f, f.fork()); |
310 |
< |
myself.interrupt(); |
310 |
< |
assertTrue(myself.isInterrupted()); |
310 |
> |
currentThread.interrupt(); |
311 |
|
f.quietlyJoin(); |
312 |
|
Thread.interrupted(); |
313 |
|
assertEquals(21, f.result); |
316 |
|
f = new FibAction(8); |
317 |
|
f.cancel(true); |
318 |
|
assertSame(f, f.fork()); |
319 |
< |
myself.interrupt(); |
320 |
< |
assertTrue(myself.isInterrupted()); |
319 |
> |
currentThread.interrupt(); |
320 |
|
f.quietlyJoin(); |
321 |
|
Thread.interrupted(); |
322 |
|
checkCancelled(f); |
324 |
|
f = new FibAction(8); |
325 |
|
f.completeExceptionally(new FJException()); |
326 |
|
assertSame(f, f.fork()); |
327 |
< |
myself.interrupt(); |
329 |
< |
assertTrue(myself.isInterrupted()); |
327 |
> |
currentThread.interrupt(); |
328 |
|
f.quietlyJoin(); |
329 |
|
Thread.interrupted(); |
330 |
|
checkCompletedAbnormally(f, f.getException()); |
357 |
|
protected void realCompute() throws Exception { |
358 |
|
FibAction f = new FibAction(8); |
359 |
|
assertSame(f, f.fork()); |
360 |
< |
assertNull(f.get(5L, SECONDS)); |
360 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
361 |
|
assertEquals(21, f.result); |
362 |
|
checkCompletedNormally(f); |
363 |
|
}}; |
373 |
|
FibAction f = new FibAction(8); |
374 |
|
assertSame(f, f.fork()); |
375 |
|
try { |
376 |
< |
f.get(5L, null); |
376 |
> |
f.get(randomTimeout(), null); |
377 |
|
shouldThrow(); |
378 |
|
} catch (NullPointerException success) {} |
379 |
|
}}; |
473 |
|
FailingFibAction f = new FailingFibAction(8); |
474 |
|
assertSame(f, f.fork()); |
475 |
|
try { |
476 |
< |
f.get(5L, SECONDS); |
476 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
477 |
|
shouldThrow(); |
478 |
|
} catch (ExecutionException success) { |
479 |
|
Throwable cause = success.getCause(); |
565 |
|
assertTrue(f.cancel(true)); |
566 |
|
assertSame(f, f.fork()); |
567 |
|
try { |
568 |
< |
f.get(5L, SECONDS); |
568 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
569 |
|
shouldThrow(); |
570 |
|
} catch (CancellationException success) { |
571 |
|
checkCancelled(f); |
739 |
|
FibAction f = new FibAction(8); |
740 |
|
FibAction g = new FibAction(9); |
741 |
|
FibAction h = new FibAction(7); |
742 |
< |
HashSet set = new HashSet(); |
742 |
> |
HashSet<ForkJoinTask<?>> set = new HashSet<>(); |
743 |
|
set.add(f); |
744 |
|
set.add(g); |
745 |
|
set.add(h); |
837 |
|
FailingFibAction f = new FailingFibAction(8); |
838 |
|
FibAction g = new FibAction(9); |
839 |
|
FibAction h = new FibAction(7); |
840 |
< |
HashSet set = new HashSet(); |
840 |
> |
HashSet<ForkJoinTask<?>> set = new HashSet<>(); |
841 |
|
set.add(f); |
842 |
|
set.add(g); |
843 |
|
set.add(h); |
853 |
|
|
854 |
|
// CountedCompleter versions |
855 |
|
|
856 |
< |
abstract static class CCF extends CountedCompleter { |
856 |
> |
abstract static class CCF extends CountedCompleter<Void> { |
857 |
|
int number; |
858 |
|
int rnumber; |
859 |
|
|
860 |
< |
public CCF(CountedCompleter parent, int n) { |
860 |
> |
public CCF(CountedCompleter<?> parent, int n) { |
861 |
|
super(parent, 1); |
862 |
|
this.number = n; |
863 |
|
} |
864 |
|
|
865 |
|
public final void compute() { |
866 |
< |
CountedCompleter p; |
866 |
> |
CountedCompleter<?> p; |
867 |
|
CCF f = this; |
868 |
|
int n = number; |
869 |
|
while (n >= 2) { |
880 |
|
} |
881 |
|
|
882 |
|
static final class LCCF extends CCF { |
883 |
< |
public LCCF(CountedCompleter parent, int n) { |
883 |
> |
public LCCF(CountedCompleter<?> parent, int n) { |
884 |
|
super(parent, n); |
885 |
|
} |
886 |
< |
public final void onCompletion(CountedCompleter caller) { |
886 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
887 |
|
CCF p = (CCF)getCompleter(); |
888 |
|
int n = number + rnumber; |
889 |
|
if (p != null) |
896 |
|
public RCCF(CountedCompleter parent, int n) { |
897 |
|
super(parent, n); |
898 |
|
} |
899 |
< |
public final void onCompletion(CountedCompleter caller) { |
899 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
900 |
|
CCF p = (CCF)getCompleter(); |
901 |
|
int n = number + rnumber; |
902 |
|
if (p != null) |
906 |
|
} |
907 |
|
} |
908 |
|
|
909 |
< |
// Version of CCF with forced failure in left completions |
910 |
< |
abstract static class FailingCCF extends CountedCompleter { |
909 |
> |
/** Version of CCF with forced failure in left completions. */ |
910 |
> |
abstract static class FailingCCF extends CountedCompleter<Void> { |
911 |
|
int number; |
912 |
|
int rnumber; |
913 |
|
|
914 |
< |
public FailingCCF(CountedCompleter parent, int n) { |
914 |
> |
public FailingCCF(CountedCompleter<?> parent, int n) { |
915 |
|
super(parent, 1); |
916 |
|
this.number = n; |
917 |
|
} |
918 |
|
|
919 |
|
public final void compute() { |
920 |
< |
CountedCompleter p; |
920 |
> |
CountedCompleter<?> p; |
921 |
|
FailingCCF f = this; |
922 |
|
int n = number; |
923 |
|
while (n >= 2) { |
934 |
|
} |
935 |
|
|
936 |
|
static final class LFCCF extends FailingCCF { |
937 |
< |
public LFCCF(CountedCompleter parent, int n) { |
937 |
> |
public LFCCF(CountedCompleter<?> parent, int n) { |
938 |
|
super(parent, n); |
939 |
|
} |
940 |
< |
public final void onCompletion(CountedCompleter caller) { |
940 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
941 |
|
FailingCCF p = (FailingCCF)getCompleter(); |
942 |
|
int n = number + rnumber; |
943 |
|
if (p != null) |
947 |
|
} |
948 |
|
} |
949 |
|
static final class RFCCF extends FailingCCF { |
950 |
< |
public RFCCF(CountedCompleter parent, int n) { |
950 |
> |
public RFCCF(CountedCompleter<?> parent, int n) { |
951 |
|
super(parent, n); |
952 |
|
} |
953 |
< |
public final void onCompletion(CountedCompleter caller) { |
953 |
> |
public final void onCompletion(CountedCompleter<?> caller) { |
954 |
|
completeExceptionally(new FJException()); |
955 |
|
} |
956 |
|
} |
961 |
|
* completed tasks; getRawResult returns null. |
962 |
|
*/ |
963 |
|
public void testInvokeCC() { |
964 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
964 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
965 |
|
protected void realCompute() { |
966 |
|
CCF f = new LCCF(null, 8); |
967 |
|
assertNull(f.invoke()); |
977 |
|
* completed tasks |
978 |
|
*/ |
979 |
|
public void testQuietlyInvokeCC() { |
980 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
980 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
981 |
|
protected void realCompute() { |
982 |
|
CCF f = new LCCF(null, 8); |
983 |
|
f.quietlyInvoke(); |
991 |
|
* join of a forked task returns when task completes |
992 |
|
*/ |
993 |
|
public void testForkJoinCC() { |
994 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
994 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
995 |
|
protected void realCompute() { |
996 |
|
CCF f = new LCCF(null, 8); |
997 |
|
assertSame(f, f.fork()); |
1006 |
|
* get of a forked task returns when task completes |
1007 |
|
*/ |
1008 |
|
public void testForkGetCC() { |
1009 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1009 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1010 |
|
protected void realCompute() throws Exception { |
1011 |
|
CCF f = new LCCF(null, 8); |
1012 |
|
assertSame(f, f.fork()); |
1021 |
|
* timed get of a forked task returns when task completes |
1022 |
|
*/ |
1023 |
|
public void testForkTimedGetCC() { |
1024 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1024 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1025 |
|
protected void realCompute() throws Exception { |
1026 |
|
CCF f = new LCCF(null, 8); |
1027 |
|
assertSame(f, f.fork()); |
1036 |
|
* timed get with null time unit throws NPE |
1037 |
|
*/ |
1038 |
|
public void testForkTimedGetNPECC() { |
1039 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1039 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1040 |
|
protected void realCompute() throws Exception { |
1041 |
|
CCF f = new LCCF(null, 8); |
1042 |
|
assertSame(f, f.fork()); |
1043 |
|
try { |
1044 |
< |
f.get(5L, null); |
1044 |
> |
f.get(randomTimeout(), null); |
1045 |
|
shouldThrow(); |
1046 |
|
} catch (NullPointerException success) {} |
1047 |
|
}}; |
1052 |
|
* quietlyJoin of a forked task returns when task completes |
1053 |
|
*/ |
1054 |
|
public void testForkQuietlyJoinCC() { |
1055 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1055 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1056 |
|
protected void realCompute() { |
1057 |
|
CCF f = new LCCF(null, 8); |
1058 |
|
assertSame(f, f.fork()); |
1067 |
|
* invoke task throws exception when task completes abnormally |
1068 |
|
*/ |
1069 |
|
public void testAbnormalInvokeCC() { |
1070 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1070 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1071 |
|
protected void realCompute() { |
1072 |
|
FailingCCF f = new LFCCF(null, 8); |
1073 |
|
try { |
1084 |
|
* quietlyInvoke task returns when task completes abnormally |
1085 |
|
*/ |
1086 |
|
public void testAbnormalQuietlyInvokeCC() { |
1087 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1087 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1088 |
|
protected void realCompute() { |
1089 |
|
FailingCCF f = new LFCCF(null, 8); |
1090 |
|
f.quietlyInvoke(); |
1098 |
|
* join of a forked task throws exception when task completes abnormally |
1099 |
|
*/ |
1100 |
|
public void testAbnormalForkJoinCC() { |
1101 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1101 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1102 |
|
protected void realCompute() { |
1103 |
|
FailingCCF f = new LFCCF(null, 8); |
1104 |
|
assertSame(f, f.fork()); |
1116 |
|
* get of a forked task throws exception when task completes abnormally |
1117 |
|
*/ |
1118 |
|
public void testAbnormalForkGetCC() { |
1119 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1119 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1120 |
|
protected void realCompute() throws Exception { |
1121 |
|
FailingCCF f = new LFCCF(null, 8); |
1122 |
|
assertSame(f, f.fork()); |
1136 |
|
* timed get of a forked task throws exception when task completes abnormally |
1137 |
|
*/ |
1138 |
|
public void testAbnormalForkTimedGetCC() { |
1139 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1139 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1140 |
|
protected void realCompute() throws Exception { |
1141 |
|
FailingCCF f = new LFCCF(null, 8); |
1142 |
|
assertSame(f, f.fork()); |
1156 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
1157 |
|
*/ |
1158 |
|
public void testAbnormalForkQuietlyJoinCC() { |
1159 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1159 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1160 |
|
protected void realCompute() { |
1161 |
|
FailingCCF f = new LFCCF(null, 8); |
1162 |
|
assertSame(f, f.fork()); |
1171 |
|
* invoke task throws exception when task cancelled |
1172 |
|
*/ |
1173 |
|
public void testCancelledInvokeCC() { |
1174 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1174 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1175 |
|
protected void realCompute() { |
1176 |
|
CCF f = new LCCF(null, 8); |
1177 |
|
assertTrue(f.cancel(true)); |
1189 |
|
* join of a forked task throws exception when task cancelled |
1190 |
|
*/ |
1191 |
|
public void testCancelledForkJoinCC() { |
1192 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1192 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1193 |
|
protected void realCompute() { |
1194 |
|
CCF f = new LCCF(null, 8); |
1195 |
|
assertTrue(f.cancel(true)); |
1208 |
|
* get of a forked task throws exception when task cancelled |
1209 |
|
*/ |
1210 |
|
public void testCancelledForkGetCC() { |
1211 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1211 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1212 |
|
protected void realCompute() throws Exception { |
1213 |
|
CCF f = new LCCF(null, 8); |
1214 |
|
assertTrue(f.cancel(true)); |
1227 |
|
* timed get of a forked task throws exception when task cancelled |
1228 |
|
*/ |
1229 |
|
public void testCancelledForkTimedGetCC() throws Exception { |
1230 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1230 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1231 |
|
protected void realCompute() throws Exception { |
1232 |
|
CCF f = new LCCF(null, 8); |
1233 |
|
assertTrue(f.cancel(true)); |
1246 |
|
* quietlyJoin of a forked task returns when task cancelled |
1247 |
|
*/ |
1248 |
|
public void testCancelledForkQuietlyJoinCC() { |
1249 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1249 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1250 |
|
protected void realCompute() { |
1251 |
|
CCF f = new LCCF(null, 8); |
1252 |
|
assertTrue(f.cancel(true)); |
1261 |
|
* getPool of non-FJ task returns null |
1262 |
|
*/ |
1263 |
|
public void testGetPool2CC() { |
1264 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1264 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1265 |
|
protected void realCompute() { |
1266 |
|
assertNull(getPool()); |
1267 |
|
}}; |
1272 |
|
* inForkJoinPool of non-FJ task returns false |
1273 |
|
*/ |
1274 |
|
public void testInForkJoinPool2CC() { |
1275 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1275 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1276 |
|
protected void realCompute() { |
1277 |
|
assertFalse(inForkJoinPool()); |
1278 |
|
}}; |
1283 |
|
* setRawResult(null) succeeds |
1284 |
|
*/ |
1285 |
|
public void testSetRawResultCC() { |
1286 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1286 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1287 |
|
protected void realCompute() { |
1288 |
|
setRawResult(null); |
1289 |
|
assertNull(getRawResult()); |
1295 |
|
* invoke task throws exception after invoking completeExceptionally |
1296 |
|
*/ |
1297 |
|
public void testCompleteExceptionally2CC() { |
1298 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1298 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1299 |
|
protected void realCompute() { |
1300 |
|
CCF f = new LCCF(null, 8); |
1301 |
|
f.completeExceptionally(new FJException()); |
1313 |
|
* invokeAll(t1, t2) invokes all task arguments |
1314 |
|
*/ |
1315 |
|
public void testInvokeAll2CC() { |
1316 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1316 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1317 |
|
protected void realCompute() { |
1318 |
|
CCF f = new LCCF(null, 8); |
1319 |
|
CCF g = new LCCF(null, 9); |
1330 |
|
* invokeAll(tasks) with 1 argument invokes task |
1331 |
|
*/ |
1332 |
|
public void testInvokeAll1CC() { |
1333 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1333 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1334 |
|
protected void realCompute() { |
1335 |
|
CCF f = new LCCF(null, 8); |
1336 |
|
invokeAll(f); |
1344 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1345 |
|
*/ |
1346 |
|
public void testInvokeAll3CC() { |
1347 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1347 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1348 |
|
protected void realCompute() { |
1349 |
|
CCF f = new LCCF(null, 8); |
1350 |
|
CCF g = new LCCF(null, 9); |
1364 |
|
* invokeAll(collection) invokes all tasks in the collection |
1365 |
|
*/ |
1366 |
|
public void testInvokeAllCollectionCC() { |
1367 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1367 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1368 |
|
protected void realCompute() { |
1369 |
|
CCF f = new LCCF(null, 8); |
1370 |
|
CCF g = new LCCF(null, 9); |
1371 |
|
CCF h = new LCCF(null, 7); |
1372 |
< |
HashSet set = new HashSet(); |
1372 |
> |
HashSet<ForkJoinTask<?>> set = new HashSet<>(); |
1373 |
|
set.add(f); |
1374 |
|
set.add(g); |
1375 |
|
set.add(h); |
1388 |
|
* invokeAll(tasks) with any null task throws NPE |
1389 |
|
*/ |
1390 |
|
public void testInvokeAllNPECC() { |
1391 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1391 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1392 |
|
protected void realCompute() { |
1393 |
|
CCF f = new LCCF(null, 8); |
1394 |
|
CCF g = new LCCF(null, 9); |
1405 |
|
* invokeAll(t1, t2) throw exception if any task does |
1406 |
|
*/ |
1407 |
|
public void testAbnormalInvokeAll2CC() { |
1408 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1408 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1409 |
|
protected void realCompute() { |
1410 |
|
CCF f = new LCCF(null, 8); |
1411 |
|
FailingCCF g = new LFCCF(null, 9); |
1423 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1424 |
|
*/ |
1425 |
|
public void testAbnormalInvokeAll1CC() { |
1426 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1426 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1427 |
|
protected void realCompute() { |
1428 |
|
FailingCCF g = new LFCCF(null, 9); |
1429 |
|
try { |
1440 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1441 |
|
*/ |
1442 |
|
public void testAbnormalInvokeAll3CC() { |
1443 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1443 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1444 |
|
protected void realCompute() { |
1445 |
|
CCF f = new LCCF(null, 8); |
1446 |
|
FailingCCF g = new LFCCF(null, 9); |
1459 |
|
* invokeAll(collection) throws exception if any task does |
1460 |
|
*/ |
1461 |
|
public void testAbnormalInvokeAllCollectionCC() { |
1462 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1462 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1463 |
|
protected void realCompute() { |
1464 |
|
FailingCCF f = new LFCCF(null, 8); |
1465 |
|
CCF g = new LCCF(null, 9); |
1466 |
|
CCF h = new LCCF(null, 7); |
1467 |
< |
HashSet set = new HashSet(); |
1467 |
> |
HashSet<ForkJoinTask<?>> set = new HashSet<>(); |
1468 |
|
set.add(f); |
1469 |
|
set.add(g); |
1470 |
|
set.add(h); |
1484 |
|
*/ |
1485 |
|
public void testAwaitQuiescence1() throws Exception { |
1486 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1487 |
< |
try { |
1487 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1488 |
|
final long startTime = System.nanoTime(); |
1489 |
|
assertTrue(p.isQuiescent()); |
1490 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1490 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1491 |
|
protected void realCompute() { |
1492 |
|
FibAction f = new FibAction(8); |
1493 |
|
assertSame(f, f.fork()); |
1515 |
|
assertFalse(p.isTerminated()); |
1516 |
|
Thread.yield(); |
1517 |
|
} |
1520 |
– |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1518 |
|
assertEquals(0, p.getQueuedTaskCount()); |
1519 |
|
assertFalse(p.getAsyncMode()); |
1523 |
– |
assertEquals(0, p.getActiveThreadCount()); |
1524 |
– |
assertEquals(0, p.getQueuedTaskCount()); |
1520 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
1521 |
|
assertFalse(p.hasQueuedSubmissions()); |
1522 |
+ |
while (p.getActiveThreadCount() != 0 |
1523 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1524 |
+ |
Thread.yield(); |
1525 |
|
assertFalse(p.isShutdown()); |
1526 |
|
assertFalse(p.isTerminating()); |
1527 |
|
assertFalse(p.isTerminated()); |
1528 |
< |
} finally { |
1531 |
< |
joinPool(p); |
1528 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1529 |
|
} |
1530 |
|
} |
1531 |
|
|
1534 |
|
* timeout elapsed |
1535 |
|
*/ |
1536 |
|
public void testAwaitQuiescence2() throws Exception { |
1537 |
< |
/** |
1537 |
> |
/* |
1538 |
|
* """It is possible to disable or limit the use of threads in the |
1539 |
|
* common pool by setting the parallelism property to zero. However |
1540 |
|
* doing so may cause unjoined tasks to never be executed.""" |
1543 |
|
"java.util.concurrent.ForkJoinPool.common.parallelism"))) |
1544 |
|
return; |
1545 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1546 |
< |
try { |
1546 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1547 |
|
assertTrue(p.isQuiescent()); |
1548 |
|
final long startTime = System.nanoTime(); |
1549 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1549 |
> |
CheckedRecursiveAction a = new CheckedRecursiveAction() { |
1550 |
|
protected void realCompute() { |
1551 |
|
FibAction f = new FibAction(8); |
1552 |
|
assertSame(f, f.fork()); |
1564 |
|
}}; |
1565 |
|
p.execute(a); |
1566 |
|
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); |
1570 |
– |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1567 |
|
assertTrue(p.isQuiescent()); |
1568 |
|
assertTrue(a.isDone()); |
1569 |
|
assertEquals(0, p.getQueuedTaskCount()); |
1570 |
|
assertFalse(p.getAsyncMode()); |
1575 |
– |
assertEquals(0, p.getActiveThreadCount()); |
1576 |
– |
assertEquals(0, p.getQueuedTaskCount()); |
1571 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
1572 |
|
assertFalse(p.hasQueuedSubmissions()); |
1573 |
+ |
while (p.getActiveThreadCount() != 0 |
1574 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1575 |
+ |
Thread.yield(); |
1576 |
|
assertFalse(p.isShutdown()); |
1577 |
|
assertFalse(p.isTerminating()); |
1578 |
|
assertFalse(p.isTerminated()); |
1579 |
< |
} finally { |
1583 |
< |
joinPool(p); |
1579 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1580 |
|
} |
1581 |
|
} |
1582 |
|
|