8 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
|
|
10 |
|
import java.util.Arrays; |
11 |
+ |
import java.util.Collections; |
12 |
+ |
import java.util.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
16 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
17 |
|
import java.util.concurrent.RecursiveAction; |
18 |
|
import java.util.concurrent.TimeoutException; |
19 |
|
|
37 |
|
static final short EXCEPTION_STATE = 1; |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
74 |
|
} |
75 |
|
|
76 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
77 |
< |
try { |
77 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
78 |
|
assertFalse(a.isDone()); |
79 |
|
assertFalse(a.isCompletedNormally()); |
80 |
|
assertFalse(a.isCompletedAbnormally()); |
90 |
|
assertFalse(a.isCancelled()); |
91 |
|
assertNull(a.getException()); |
92 |
|
assertNull(a.getRawResult()); |
90 |
– |
} finally { |
91 |
– |
joinPool(pool); |
93 |
|
} |
94 |
|
} |
95 |
|
|
198 |
|
|
199 |
|
abstract static class BinaryAsyncAction extends ForkJoinTask<Void> { |
200 |
|
|
201 |
< |
private BinaryAsyncAction parent; |
201 |
> |
private volatile BinaryAsyncAction parent; |
202 |
|
|
203 |
< |
private BinaryAsyncAction sibling; |
203 |
> |
private volatile BinaryAsyncAction sibling; |
204 |
|
|
205 |
|
protected BinaryAsyncAction() { |
206 |
|
setForkJoinTaskTag(INITIAL_STATE); |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
< |
* invokeAll(t1, t2) throw exception if any task does |
877 |
> |
* invokeAll(tasks) with 1 argument throws exception if task does |
878 |
|
*/ |
879 |
< |
public void testAbnormalInvokeAll2() { |
880 |
< |
testAbnormalInvokeAll2(mainPool()); |
879 |
> |
public void testAbnormalInvokeAll1() { |
880 |
> |
testAbnormalInvokeAll1(mainPool()); |
881 |
|
} |
882 |
< |
public void testAbnormalInvokeAll2_Singleton() { |
883 |
< |
testAbnormalInvokeAll2(singletonPool()); |
882 |
> |
public void testAbnormalInvokeAll1_Singleton() { |
883 |
> |
testAbnormalInvokeAll1(singletonPool()); |
884 |
|
} |
885 |
< |
public void testAbnormalInvokeAll2(ForkJoinPool pool) { |
885 |
> |
public void testAbnormalInvokeAll1(ForkJoinPool pool) { |
886 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
887 |
|
protected void realCompute() { |
887 |
– |
AsyncFib f = new AsyncFib(8); |
888 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
889 |
|
try { |
890 |
< |
invokeAll(f, g); |
890 |
> |
invokeAll(g); |
891 |
|
shouldThrow(); |
892 |
|
} catch (FJException success) { |
893 |
|
checkCompletedAbnormally(g, success); |
897 |
|
} |
898 |
|
|
899 |
|
/** |
900 |
< |
* invokeAll(tasks) with 1 argument throws exception if task does |
900 |
> |
* invokeAll(t1, t2) throw exception if any task does |
901 |
|
*/ |
902 |
< |
public void testAbnormalInvokeAll1() { |
903 |
< |
testAbnormalInvokeAll1(mainPool()); |
902 |
> |
public void testAbnormalInvokeAll2() { |
903 |
> |
testAbnormalInvokeAll2(mainPool()); |
904 |
|
} |
905 |
< |
public void testAbnormalInvokeAll1_Singleton() { |
906 |
< |
testAbnormalInvokeAll1(singletonPool()); |
905 |
> |
public void testAbnormalInvokeAll2_Singleton() { |
906 |
> |
testAbnormalInvokeAll2(singletonPool()); |
907 |
|
} |
908 |
< |
public void testAbnormalInvokeAll1(ForkJoinPool pool) { |
908 |
> |
public void testAbnormalInvokeAll2(ForkJoinPool pool) { |
909 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
910 |
|
protected void realCompute() { |
911 |
+ |
AsyncFib f = new AsyncFib(8); |
912 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
913 |
+ |
ForkJoinTask[] tasks = { f, g }; |
914 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
915 |
|
try { |
916 |
< |
invokeAll(g); |
916 |
> |
invokeAll(tasks[0], tasks[1]); |
917 |
|
shouldThrow(); |
918 |
|
} catch (FJException success) { |
919 |
|
checkCompletedAbnormally(g, success); |
937 |
|
AsyncFib f = new AsyncFib(8); |
938 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
939 |
|
AsyncFib h = new AsyncFib(7); |
940 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
941 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
942 |
|
try { |
943 |
< |
invokeAll(f, g, h); |
943 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
944 |
|
shouldThrow(); |
945 |
|
} catch (FJException success) { |
946 |
|
checkCompletedAbnormally(g, success); |
965 |
|
AsyncFib g = new AsyncFib(9); |
966 |
|
AsyncFib h = new AsyncFib(7); |
967 |
|
ForkJoinTask[] tasks = { f, g, h }; |
968 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
969 |
|
try { |
970 |
|
invokeAll(Arrays.asList(tasks)); |
971 |
|
shouldThrow(); |
1152 |
|
testInvokeOnPool(mainPool(), a); |
1153 |
|
} |
1154 |
|
|
1155 |
+ |
// jdk9 |
1156 |
+ |
|
1157 |
+ |
/** |
1158 |
+ |
* pollSubmission returns unexecuted submitted task, if present |
1159 |
+ |
*/ |
1160 |
+ |
public void testPollSubmission() { |
1161 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1162 |
+ |
final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); |
1163 |
+ |
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1164 |
+ |
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1165 |
+ |
final ForkJoinPool p = singletonPool(); |
1166 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1167 |
+ |
Thread external = new Thread(new CheckedRunnable() { |
1168 |
+ |
public void realRun() { |
1169 |
+ |
p.execute(a); |
1170 |
+ |
p.execute(b); |
1171 |
+ |
p.execute(c); |
1172 |
+ |
}}); |
1173 |
+ |
RecursiveAction s = new CheckedRecursiveAction() { |
1174 |
+ |
protected void realCompute() { |
1175 |
+ |
external.start(); |
1176 |
+ |
try { |
1177 |
+ |
external.join(); |
1178 |
+ |
} catch (Exception ex) { |
1179 |
+ |
threadUnexpectedException(ex); |
1180 |
+ |
} |
1181 |
+ |
assertTrue(p.hasQueuedSubmissions()); |
1182 |
+ |
assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); |
1183 |
+ |
ForkJoinTask r = ForkJoinTask.pollSubmission(); |
1184 |
+ |
assertTrue(r == a || r == b || r == c); |
1185 |
+ |
assertFalse(r.isDone()); |
1186 |
+ |
}}; |
1187 |
+ |
p.invoke(s); |
1188 |
+ |
done.countDown(); |
1189 |
+ |
} |
1190 |
+ |
} |
1191 |
+ |
|
1192 |
|
} |