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 |
|
|
126 |
|
|
127 |
|
{ |
128 |
|
Thread.currentThread().interrupt(); |
129 |
< |
long t0 = System.nanoTime(); |
129 |
> |
long startTime = System.nanoTime(); |
130 |
|
assertSame(expected, a.join()); |
131 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
131 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
132 |
|
Thread.interrupted(); |
133 |
|
} |
134 |
|
|
135 |
|
{ |
136 |
|
Thread.currentThread().interrupt(); |
137 |
< |
long t0 = System.nanoTime(); |
137 |
> |
long startTime = System.nanoTime(); |
138 |
|
a.quietlyJoin(); // should be no-op |
139 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
139 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
140 |
|
Thread.interrupted(); |
141 |
|
} |
142 |
|
|
172 |
|
Thread.interrupted(); |
173 |
|
|
174 |
|
{ |
175 |
< |
long t0 = System.nanoTime(); |
175 |
> |
long startTime = System.nanoTime(); |
176 |
|
a.quietlyJoin(); // should be no-op |
177 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
177 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
178 |
|
} |
179 |
|
|
180 |
|
try { |
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); |
243 |
|
super.completeExceptionally(ex); |
244 |
|
} |
245 |
|
|
246 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
247 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
248 |
+ |
completeExceptionally(new FJException()); |
249 |
+ |
return true; |
250 |
+ |
} |
251 |
+ |
return false; |
252 |
+ |
} |
253 |
+ |
|
254 |
|
public final void complete() { |
255 |
|
BinaryAsyncAction a = this; |
256 |
|
for (;;) { |
273 |
|
} |
274 |
|
|
275 |
|
public final void completeExceptionally(Throwable ex) { |
276 |
< |
BinaryAsyncAction a = this; |
269 |
< |
while (!a.isCompletedAbnormally()) { |
276 |
> |
for (BinaryAsyncAction a = this;;) { |
277 |
|
a.completeThisExceptionally(ex); |
278 |
|
BinaryAsyncAction s = a.sibling; |
279 |
< |
if (s != null) |
280 |
< |
s.cancel(false); |
281 |
< |
if (!a.onException() || (a = a.parent) == null) |
279 |
> |
if (s != null && !s.isDone()) |
280 |
> |
s.completeExceptionally(ex); |
281 |
> |
if ((a = a.parent) == null) |
282 |
|
break; |
283 |
|
} |
284 |
|
} |
310 |
|
try { |
311 |
|
AsyncFib f = this; |
312 |
|
int n = f.number; |
313 |
< |
if (n > 1) { |
314 |
< |
while (n > 1) { |
315 |
< |
AsyncFib p = f; |
316 |
< |
AsyncFib r = new AsyncFib(n - 2); |
317 |
< |
f = new AsyncFib(--n); |
318 |
< |
p.linkSubtasks(r, f); |
312 |
< |
r.fork(); |
313 |
< |
} |
314 |
< |
f.number = n; |
313 |
> |
while (n > 1) { |
314 |
> |
AsyncFib p = f; |
315 |
> |
AsyncFib r = new AsyncFib(n - 2); |
316 |
> |
f = new AsyncFib(--n); |
317 |
> |
p.linkSubtasks(r, f); |
318 |
> |
r.fork(); |
319 |
|
} |
320 |
|
f.complete(); |
321 |
|
} |
322 |
|
catch (Throwable ex) { |
323 |
|
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
324 |
|
} |
325 |
+ |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
326 |
+ |
throw new FJException(); |
327 |
|
return false; |
328 |
|
} |
329 |
|
|
345 |
|
} |
346 |
|
|
347 |
|
public final boolean exec() { |
348 |
< |
FailingAsyncFib f = this; |
349 |
< |
int n = f.number; |
350 |
< |
if (n > 1) { |
348 |
> |
try { |
349 |
> |
FailingAsyncFib f = this; |
350 |
> |
int n = f.number; |
351 |
|
while (n > 1) { |
352 |
|
FailingAsyncFib p = f; |
353 |
|
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
355 |
|
p.linkSubtasks(r, f); |
356 |
|
r.fork(); |
357 |
|
} |
358 |
< |
f.number = n; |
358 |
> |
f.complete(); |
359 |
|
} |
360 |
< |
f.complete(); |
360 |
> |
catch (Throwable ex) { |
361 |
> |
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
362 |
> |
} |
363 |
> |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
364 |
> |
throw new FJException(); |
365 |
|
return false; |
366 |
|
} |
367 |
|
|
921 |
|
AsyncFib f = new AsyncFib(8); |
922 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
923 |
|
ForkJoinTask[] tasks = { f, g }; |
924 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
924 |
> |
shuffle(tasks); |
925 |
|
try { |
926 |
|
invokeAll(tasks[0], tasks[1]); |
927 |
|
shouldThrow(); |
948 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
949 |
|
AsyncFib h = new AsyncFib(7); |
950 |
|
ForkJoinTask[] tasks = { f, g, h }; |
951 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
951 |
> |
shuffle(tasks); |
952 |
|
try { |
953 |
|
invokeAll(tasks[0], tasks[1], tasks[2]); |
954 |
|
shouldThrow(); |
975 |
|
AsyncFib g = new AsyncFib(9); |
976 |
|
AsyncFib h = new AsyncFib(7); |
977 |
|
ForkJoinTask[] tasks = { f, g, h }; |
978 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
978 |
> |
shuffle(tasks); |
979 |
|
try { |
980 |
|
invokeAll(Arrays.asList(tasks)); |
981 |
|
shouldThrow(); |
1162 |
|
testInvokeOnPool(mainPool(), a); |
1163 |
|
} |
1164 |
|
|
1165 |
+ |
// jdk9 |
1166 |
+ |
|
1167 |
+ |
/** |
1168 |
+ |
* pollSubmission returns unexecuted submitted task, if present |
1169 |
+ |
*/ |
1170 |
+ |
public void testPollSubmission() { |
1171 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1172 |
+ |
final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); |
1173 |
+ |
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1174 |
+ |
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1175 |
+ |
final ForkJoinPool p = singletonPool(); |
1176 |
+ |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1177 |
+ |
Thread external = new Thread(new CheckedRunnable() { |
1178 |
+ |
public void realRun() { |
1179 |
+ |
p.execute(a); |
1180 |
+ |
p.execute(b); |
1181 |
+ |
p.execute(c); |
1182 |
+ |
}}); |
1183 |
+ |
RecursiveAction s = new CheckedRecursiveAction() { |
1184 |
+ |
protected void realCompute() { |
1185 |
+ |
external.start(); |
1186 |
+ |
try { |
1187 |
+ |
external.join(); |
1188 |
+ |
} catch (Exception ex) { |
1189 |
+ |
threadUnexpectedException(ex); |
1190 |
+ |
} |
1191 |
+ |
assertTrue(p.hasQueuedSubmissions()); |
1192 |
+ |
assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); |
1193 |
+ |
ForkJoinTask r = ForkJoinTask.pollSubmission(); |
1194 |
+ |
assertTrue(r == a || r == b || r == c); |
1195 |
+ |
assertFalse(r.isDone()); |
1196 |
+ |
}}; |
1197 |
+ |
p.invoke(s); |
1198 |
+ |
} |
1199 |
+ |
} |
1200 |
+ |
|
1201 |
|
} |