7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
|
|
10 |
+ |
import java.util.Arrays; |
11 |
|
import java.util.HashSet; |
12 |
|
import java.util.concurrent.CancellationException; |
13 |
|
import java.util.concurrent.ExecutionException; |
254 |
|
super.completeExceptionally(ex); |
255 |
|
} |
256 |
|
|
257 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
258 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
259 |
+ |
completeExceptionally(new FJException()); |
260 |
+ |
return true; |
261 |
+ |
} |
262 |
+ |
return false; |
263 |
+ |
} |
264 |
+ |
|
265 |
|
public final void complete() { |
266 |
|
BinaryAsyncAction a = this; |
267 |
|
for (;;) { |
283 |
|
} |
284 |
|
|
285 |
|
public final void completeExceptionally(Throwable ex) { |
286 |
< |
BinaryAsyncAction a = this; |
278 |
< |
while (!a.isCompletedAbnormally()) { |
286 |
> |
for (BinaryAsyncAction a = this;;) { |
287 |
|
a.completeThisExceptionally(ex); |
288 |
|
BinaryAsyncAction s = a.sibling; |
289 |
< |
if (s != null) |
290 |
< |
s.cancel(false); |
291 |
< |
if (!a.onException() || (a = a.parent) == null) |
289 |
> |
if (s != null && !s.isDone()) |
290 |
> |
s.completeExceptionally(ex); |
291 |
> |
if ((a = a.parent) == null) |
292 |
|
break; |
293 |
|
} |
294 |
|
} |
338 |
|
public final boolean exec() { |
339 |
|
AsyncFib f = this; |
340 |
|
int n = f.number; |
341 |
< |
if (n > 1) { |
342 |
< |
while (n > 1) { |
343 |
< |
AsyncFib p = f; |
344 |
< |
AsyncFib r = new AsyncFib(n - 2); |
345 |
< |
f = new AsyncFib(--n); |
346 |
< |
p.linkSubtasks(r, f); |
339 |
< |
r.fork(); |
340 |
< |
} |
341 |
< |
f.number = n; |
341 |
> |
while (n > 1) { |
342 |
> |
AsyncFib p = f; |
343 |
> |
AsyncFib r = new AsyncFib(n - 2); |
344 |
> |
f = new AsyncFib(--n); |
345 |
> |
p.linkSubtasks(r, f); |
346 |
> |
r.fork(); |
347 |
|
} |
348 |
|
f.complete(); |
349 |
|
return false; |
363 |
|
public final boolean exec() { |
364 |
|
FailingAsyncFib f = this; |
365 |
|
int n = f.number; |
366 |
< |
if (n > 1) { |
367 |
< |
while (n > 1) { |
368 |
< |
FailingAsyncFib p = f; |
369 |
< |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
370 |
< |
f = new FailingAsyncFib(--n); |
371 |
< |
p.linkSubtasks(r, f); |
367 |
< |
r.fork(); |
368 |
< |
} |
369 |
< |
f.number = n; |
366 |
> |
while (n > 1) { |
367 |
> |
FailingAsyncFib p = f; |
368 |
> |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
369 |
> |
f = new FailingAsyncFib(--n); |
370 |
> |
p.linkSubtasks(r, f); |
371 |
> |
r.fork(); |
372 |
|
} |
373 |
|
f.complete(); |
374 |
|
return false; |
774 |
|
} |
775 |
|
|
776 |
|
/** |
777 |
+ |
* completeExceptionally(null) surprisingly has the same effect as |
778 |
+ |
* completeExceptionally(new RuntimeException()) |
779 |
+ |
*/ |
780 |
+ |
public void testCompleteExceptionally_null() { |
781 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
782 |
+ |
protected void realCompute() { |
783 |
+ |
AsyncFib f = new AsyncFib(8); |
784 |
+ |
f.completeExceptionally(null); |
785 |
+ |
try { |
786 |
+ |
f.invoke(); |
787 |
+ |
shouldThrow(); |
788 |
+ |
} catch (RuntimeException success) { |
789 |
+ |
assertSame(success.getClass(), RuntimeException.class); |
790 |
+ |
assertNull(success.getCause()); |
791 |
+ |
checkCompletedAbnormally(f, success); |
792 |
+ |
} |
793 |
+ |
}}; |
794 |
+ |
testInvokeOnPool(mainPool(), a); |
795 |
+ |
} |
796 |
+ |
|
797 |
+ |
/** |
798 |
|
* invokeAll(t1, t2) invokes all task arguments |
799 |
|
*/ |
800 |
|
public void testInvokeAll2() { |
894 |
|
protected void realCompute() { |
895 |
|
AsyncFib f = new AsyncFib(8); |
896 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
897 |
+ |
ForkJoinTask[] tasks = { f, g }; |
898 |
+ |
shuffle(tasks); |
899 |
|
try { |
900 |
< |
invokeAll(f, g); |
900 |
> |
invokeAll(tasks); |
901 |
|
shouldThrow(); |
902 |
|
} catch (FJException success) { |
903 |
|
checkCompletedAbnormally(g, success); |
932 |
|
AsyncFib f = new AsyncFib(8); |
933 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
934 |
|
AsyncFib h = new AsyncFib(7); |
935 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
936 |
+ |
shuffle(tasks); |
937 |
|
try { |
938 |
< |
invokeAll(f, g, h); |
938 |
> |
invokeAll(tasks); |
939 |
|
shouldThrow(); |
940 |
|
} catch (FJException success) { |
941 |
|
checkCompletedAbnormally(g, success); |
953 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
954 |
|
AsyncFib g = new AsyncFib(9); |
955 |
|
AsyncFib h = new AsyncFib(7); |
956 |
< |
HashSet set = new HashSet(); |
957 |
< |
set.add(f); |
931 |
< |
set.add(g); |
932 |
< |
set.add(h); |
956 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
957 |
> |
shuffle(tasks); |
958 |
|
try { |
959 |
< |
invokeAll(set); |
959 |
> |
invokeAll(Arrays.asList(tasks)); |
960 |
|
shouldThrow(); |
961 |
|
} catch (FJException success) { |
962 |
|
checkCompletedAbnormally(f, success); |
1563 |
|
protected void realCompute() { |
1564 |
|
AsyncFib f = new AsyncFib(8); |
1565 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1566 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1567 |
+ |
shuffle(tasks); |
1568 |
|
try { |
1569 |
< |
invokeAll(f, g); |
1569 |
> |
invokeAll(tasks); |
1570 |
|
shouldThrow(); |
1571 |
|
} catch (FJException success) { |
1572 |
|
checkCompletedAbnormally(g, success); |
1601 |
|
AsyncFib f = new AsyncFib(8); |
1602 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1603 |
|
AsyncFib h = new AsyncFib(7); |
1604 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1605 |
+ |
shuffle(tasks); |
1606 |
|
try { |
1607 |
< |
invokeAll(f, g, h); |
1607 |
> |
invokeAll(tasks); |
1608 |
|
shouldThrow(); |
1609 |
|
} catch (FJException success) { |
1610 |
|
checkCompletedAbnormally(g, success); |
1622 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1623 |
|
AsyncFib g = new AsyncFib(9); |
1624 |
|
AsyncFib h = new AsyncFib(7); |
1625 |
< |
HashSet set = new HashSet(); |
1626 |
< |
set.add(f); |
1598 |
< |
set.add(g); |
1599 |
< |
set.add(h); |
1625 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1626 |
> |
shuffle(tasks); |
1627 |
|
try { |
1628 |
< |
invokeAll(set); |
1628 |
> |
invokeAll(Arrays.asList(tasks)); |
1629 |
|
shouldThrow(); |
1630 |
|
} catch (FJException success) { |
1631 |
|
checkCompletedAbnormally(f, success); |