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.Collections; |
12 |
|
import java.util.HashSet; |
13 |
+ |
import java.util.List; |
14 |
|
import java.util.concurrent.CancellationException; |
15 |
|
import java.util.concurrent.ExecutionException; |
16 |
|
import java.util.concurrent.ForkJoinPool; |
256 |
|
super.completeExceptionally(ex); |
257 |
|
} |
258 |
|
|
259 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
260 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
261 |
+ |
completeExceptionally(new FJException()); |
262 |
+ |
return true; |
263 |
+ |
} |
264 |
+ |
return false; |
265 |
+ |
} |
266 |
+ |
|
267 |
|
public final void complete() { |
268 |
|
BinaryAsyncAction a = this; |
269 |
|
for (;;) { |
285 |
|
} |
286 |
|
|
287 |
|
public final void completeExceptionally(Throwable ex) { |
288 |
< |
BinaryAsyncAction a = this; |
278 |
< |
while (!a.isCompletedAbnormally()) { |
288 |
> |
for (BinaryAsyncAction a = this;;) { |
289 |
|
a.completeThisExceptionally(ex); |
290 |
|
BinaryAsyncAction s = a.sibling; |
291 |
< |
if (s != null) |
292 |
< |
s.cancel(false); |
293 |
< |
if (!a.onException() || (a = a.parent) == null) |
291 |
> |
if (s != null && !s.isDone()) |
292 |
> |
s.completeExceptionally(ex); |
293 |
> |
if ((a = a.parent) == null) |
294 |
|
break; |
295 |
|
} |
296 |
|
} |
340 |
|
public final boolean exec() { |
341 |
|
AsyncFib f = this; |
342 |
|
int n = f.number; |
343 |
< |
if (n > 1) { |
344 |
< |
while (n > 1) { |
345 |
< |
AsyncFib p = f; |
346 |
< |
AsyncFib r = new AsyncFib(n - 2); |
347 |
< |
f = new AsyncFib(--n); |
348 |
< |
p.linkSubtasks(r, f); |
339 |
< |
r.fork(); |
340 |
< |
} |
341 |
< |
f.number = n; |
343 |
> |
while (n > 1) { |
344 |
> |
AsyncFib p = f; |
345 |
> |
AsyncFib r = new AsyncFib(n - 2); |
346 |
> |
f = new AsyncFib(--n); |
347 |
> |
p.linkSubtasks(r, f); |
348 |
> |
r.fork(); |
349 |
|
} |
350 |
|
f.complete(); |
351 |
|
return false; |
365 |
|
public final boolean exec() { |
366 |
|
FailingAsyncFib f = this; |
367 |
|
int n = f.number; |
368 |
< |
if (n > 1) { |
369 |
< |
while (n > 1) { |
370 |
< |
FailingAsyncFib p = f; |
371 |
< |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
372 |
< |
f = new FailingAsyncFib(--n); |
373 |
< |
p.linkSubtasks(r, f); |
367 |
< |
r.fork(); |
368 |
< |
} |
369 |
< |
f.number = n; |
368 |
> |
while (n > 1) { |
369 |
> |
FailingAsyncFib p = f; |
370 |
> |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
371 |
> |
f = new FailingAsyncFib(--n); |
372 |
> |
p.linkSubtasks(r, f); |
373 |
> |
r.fork(); |
374 |
|
} |
375 |
|
f.complete(); |
376 |
|
return false; |
776 |
|
} |
777 |
|
|
778 |
|
/** |
779 |
+ |
* completeExceptionally(null) surprisingly has the same effect as |
780 |
+ |
* completeExceptionally(new RuntimeException()) |
781 |
+ |
*/ |
782 |
+ |
public void testCompleteExceptionally_null() { |
783 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
784 |
+ |
protected void realCompute() { |
785 |
+ |
AsyncFib f = new AsyncFib(8); |
786 |
+ |
f.completeExceptionally(null); |
787 |
+ |
try { |
788 |
+ |
f.invoke(); |
789 |
+ |
shouldThrow(); |
790 |
+ |
} catch (RuntimeException success) { |
791 |
+ |
assertSame(success.getClass(), RuntimeException.class); |
792 |
+ |
assertNull(success.getCause()); |
793 |
+ |
checkCompletedAbnormally(f, success); |
794 |
+ |
} |
795 |
+ |
}}; |
796 |
+ |
testInvokeOnPool(mainPool(), a); |
797 |
+ |
} |
798 |
+ |
|
799 |
+ |
/** |
800 |
|
* invokeAll(t1, t2) invokes all task arguments |
801 |
|
*/ |
802 |
|
public void testInvokeAll2() { |
896 |
|
protected void realCompute() { |
897 |
|
AsyncFib f = new AsyncFib(8); |
898 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
899 |
+ |
ForkJoinTask[] tasks = { f, g }; |
900 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
901 |
|
try { |
902 |
< |
invokeAll(f, g); |
902 |
> |
invokeAll(tasks); |
903 |
|
shouldThrow(); |
904 |
|
} catch (FJException success) { |
905 |
|
checkCompletedAbnormally(g, success); |
934 |
|
AsyncFib f = new AsyncFib(8); |
935 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
936 |
|
AsyncFib h = new AsyncFib(7); |
937 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
938 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
939 |
|
try { |
940 |
< |
invokeAll(f, g, h); |
940 |
> |
invokeAll(tasks); |
941 |
|
shouldThrow(); |
942 |
|
} catch (FJException success) { |
943 |
|
checkCompletedAbnormally(g, success); |
955 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
956 |
|
AsyncFib g = new AsyncFib(9); |
957 |
|
AsyncFib h = new AsyncFib(7); |
958 |
< |
HashSet set = new HashSet(); |
959 |
< |
set.add(f); |
960 |
< |
set.add(g); |
932 |
< |
set.add(h); |
958 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
959 |
> |
List taskList = Arrays.asList(tasks); |
960 |
> |
Collections.shuffle(taskList); |
961 |
|
try { |
962 |
< |
invokeAll(set); |
962 |
> |
invokeAll(taskList); |
963 |
|
shouldThrow(); |
964 |
|
} catch (FJException success) { |
965 |
|
checkCompletedAbnormally(f, success); |
1566 |
|
protected void realCompute() { |
1567 |
|
AsyncFib f = new AsyncFib(8); |
1568 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1569 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1570 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1571 |
|
try { |
1572 |
< |
invokeAll(f, g); |
1572 |
> |
invokeAll(tasks); |
1573 |
|
shouldThrow(); |
1574 |
|
} catch (FJException success) { |
1575 |
|
checkCompletedAbnormally(g, success); |
1604 |
|
AsyncFib f = new AsyncFib(8); |
1605 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1606 |
|
AsyncFib h = new AsyncFib(7); |
1607 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1608 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1609 |
|
try { |
1610 |
< |
invokeAll(f, g, h); |
1610 |
> |
invokeAll(tasks); |
1611 |
|
shouldThrow(); |
1612 |
|
} catch (FJException success) { |
1613 |
|
checkCompletedAbnormally(g, success); |
1625 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1626 |
|
AsyncFib g = new AsyncFib(9); |
1627 |
|
AsyncFib h = new AsyncFib(7); |
1628 |
< |
HashSet set = new HashSet(); |
1629 |
< |
set.add(f); |
1630 |
< |
set.add(g); |
1599 |
< |
set.add(h); |
1628 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1629 |
> |
List taskList = Arrays.asList(tasks); |
1630 |
> |
Collections.shuffle(taskList); |
1631 |
|
try { |
1632 |
< |
invokeAll(set); |
1632 |
> |
invokeAll(taskList); |
1633 |
|
shouldThrow(); |
1634 |
|
} catch (FJException success) { |
1635 |
|
checkCompletedAbnormally(f, success); |