8 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
|
|
10 |
|
import java.util.Arrays; |
11 |
– |
import java.util.Collections; |
11 |
|
import java.util.concurrent.CountDownLatch; |
12 |
|
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
125 |
|
|
126 |
|
{ |
127 |
|
Thread.currentThread().interrupt(); |
128 |
< |
long t0 = System.nanoTime(); |
128 |
> |
long startTime = System.nanoTime(); |
129 |
|
assertSame(expected, a.join()); |
130 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
130 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
131 |
|
Thread.interrupted(); |
132 |
|
} |
133 |
|
|
134 |
|
{ |
135 |
|
Thread.currentThread().interrupt(); |
136 |
< |
long t0 = System.nanoTime(); |
136 |
> |
long startTime = System.nanoTime(); |
137 |
|
a.quietlyJoin(); // should be no-op |
138 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
138 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
139 |
|
Thread.interrupted(); |
140 |
|
} |
141 |
|
|
171 |
|
Thread.interrupted(); |
172 |
|
|
173 |
|
{ |
174 |
< |
long t0 = System.nanoTime(); |
174 |
> |
long startTime = System.nanoTime(); |
175 |
|
a.quietlyJoin(); // should be no-op |
176 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
176 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
177 |
|
} |
178 |
|
|
179 |
|
try { |
242 |
|
super.completeExceptionally(ex); |
243 |
|
} |
244 |
|
|
245 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
246 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
247 |
+ |
completeExceptionally(new FJException()); |
248 |
+ |
return true; |
249 |
+ |
} |
250 |
+ |
return false; |
251 |
+ |
} |
252 |
+ |
|
253 |
|
public final void complete() { |
254 |
|
BinaryAsyncAction a = this; |
255 |
|
for (;;) { |
272 |
|
} |
273 |
|
|
274 |
|
public final void completeExceptionally(Throwable ex) { |
275 |
< |
BinaryAsyncAction a = this; |
269 |
< |
while (!a.isCompletedAbnormally()) { |
275 |
> |
for (BinaryAsyncAction a = this;;) { |
276 |
|
a.completeThisExceptionally(ex); |
277 |
|
BinaryAsyncAction s = a.sibling; |
278 |
< |
if (s != null) |
279 |
< |
s.cancel(false); |
280 |
< |
if (!a.onException() || (a = a.parent) == null) |
278 |
> |
if (s != null && !s.isDone()) |
279 |
> |
s.completeExceptionally(ex); |
280 |
> |
if ((a = a.parent) == null) |
281 |
|
break; |
282 |
|
} |
283 |
|
} |
309 |
|
try { |
310 |
|
AsyncFib f = this; |
311 |
|
int n = f.number; |
312 |
< |
if (n > 1) { |
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); |
312 |
< |
r.fork(); |
313 |
< |
} |
314 |
< |
f.number = n; |
312 |
> |
while (n > 1) { |
313 |
> |
AsyncFib p = f; |
314 |
> |
AsyncFib r = new AsyncFib(n - 2); |
315 |
> |
f = new AsyncFib(--n); |
316 |
> |
p.linkSubtasks(r, f); |
317 |
> |
r.fork(); |
318 |
|
} |
319 |
|
f.complete(); |
320 |
|
} |
321 |
|
catch (Throwable ex) { |
322 |
|
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
323 |
|
} |
324 |
+ |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
325 |
+ |
throw new FJException(); |
326 |
|
return false; |
327 |
|
} |
328 |
|
|
344 |
|
} |
345 |
|
|
346 |
|
public final boolean exec() { |
347 |
< |
FailingAsyncFib f = this; |
348 |
< |
int n = f.number; |
349 |
< |
if (n > 1) { |
347 |
> |
try { |
348 |
> |
FailingAsyncFib f = this; |
349 |
> |
int n = f.number; |
350 |
|
while (n > 1) { |
351 |
|
FailingAsyncFib p = f; |
352 |
|
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
354 |
|
p.linkSubtasks(r, f); |
355 |
|
r.fork(); |
356 |
|
} |
357 |
< |
f.number = n; |
357 |
> |
f.complete(); |
358 |
> |
} |
359 |
> |
catch (Throwable ex) { |
360 |
> |
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
361 |
|
} |
362 |
< |
f.complete(); |
362 |
> |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
363 |
> |
throw new FJException(); |
364 |
|
return false; |
365 |
|
} |
366 |
|
|
920 |
|
AsyncFib f = new AsyncFib(8); |
921 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
922 |
|
ForkJoinTask[] tasks = { f, g }; |
923 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
923 |
> |
shuffle(tasks); |
924 |
|
try { |
925 |
|
invokeAll(tasks[0], tasks[1]); |
926 |
|
shouldThrow(); |
947 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
948 |
|
AsyncFib h = new AsyncFib(7); |
949 |
|
ForkJoinTask[] tasks = { f, g, h }; |
950 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
950 |
> |
shuffle(tasks); |
951 |
|
try { |
952 |
|
invokeAll(tasks[0], tasks[1], tasks[2]); |
953 |
|
shouldThrow(); |
974 |
|
AsyncFib g = new AsyncFib(9); |
975 |
|
AsyncFib h = new AsyncFib(7); |
976 |
|
ForkJoinTask[] tasks = { f, g, h }; |
977 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
977 |
> |
shuffle(tasks); |
978 |
|
try { |
979 |
|
invokeAll(Arrays.asList(tasks)); |
980 |
|
shouldThrow(); |
1172 |
|
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1173 |
|
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1174 |
|
final ForkJoinPool p = singletonPool(); |
1175 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1175 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1176 |
|
Thread external = new Thread(new CheckedRunnable() { |
1177 |
|
public void realRun() { |
1178 |
|
p.execute(a); |
1194 |
|
assertFalse(r.isDone()); |
1195 |
|
}}; |
1196 |
|
p.invoke(s); |
1188 |
– |
done.countDown(); |
1197 |
|
} |
1198 |
|
} |
1199 |
|
|