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()); |
91 |
– |
} finally { |
92 |
– |
joinPool(pool); |
93 |
|
} |
94 |
|
} |
95 |
|
|
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 |
|
} |