3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
< |
import java.util.concurrent.ExecutionException; |
6 |
> |
|
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; |
17 |
|
import java.util.concurrent.ForkJoinTask; |
10 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
18 |
|
import java.util.concurrent.RecursiveAction; |
12 |
– |
import java.util.concurrent.TimeUnit; |
19 |
|
import java.util.concurrent.TimeoutException; |
20 |
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
21 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
23 |
< |
import java.util.HashSet; |
18 |
< |
import junit.framework.*; |
21 |
> |
|
22 |
> |
import junit.framework.Test; |
23 |
> |
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
public class ForkJoinTaskTest extends JSR166TestCase { |
26 |
|
|
27 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run(suite()); |
28 |
> |
main(suite(), args); |
29 |
|
} |
30 |
|
|
31 |
|
public static Test suite() { |
51 |
|
} |
52 |
|
|
53 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
54 |
< |
try { |
54 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
55 |
|
assertFalse(a.isDone()); |
56 |
|
assertFalse(a.isCompletedNormally()); |
57 |
|
assertFalse(a.isCompletedAbnormally()); |
67 |
|
assertFalse(a.isCancelled()); |
68 |
|
assertNull(a.getException()); |
69 |
|
assertNull(a.getRawResult()); |
65 |
– |
} finally { |
66 |
– |
joinPool(pool); |
70 |
|
} |
71 |
|
} |
72 |
|
|
99 |
|
|
100 |
|
{ |
101 |
|
Thread.currentThread().interrupt(); |
102 |
< |
long t0 = System.nanoTime(); |
102 |
> |
long startTime = System.nanoTime(); |
103 |
|
assertSame(expected, a.join()); |
104 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
104 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
105 |
|
Thread.interrupted(); |
106 |
|
} |
107 |
|
|
108 |
|
{ |
109 |
|
Thread.currentThread().interrupt(); |
110 |
< |
long t0 = System.nanoTime(); |
110 |
> |
long startTime = System.nanoTime(); |
111 |
|
a.quietlyJoin(); // should be no-op |
112 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
112 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
113 |
|
Thread.interrupted(); |
114 |
|
} |
115 |
|
|
142 |
|
Thread.interrupted(); |
143 |
|
|
144 |
|
{ |
145 |
< |
long t0 = System.nanoTime(); |
145 |
> |
long startTime = System.nanoTime(); |
146 |
|
a.quietlyJoin(); // should be no-op |
147 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
147 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
148 |
|
} |
149 |
|
|
150 |
|
try { |
180 |
|
Thread.interrupted(); |
181 |
|
|
182 |
|
{ |
183 |
< |
long t0 = System.nanoTime(); |
183 |
> |
long startTime = System.nanoTime(); |
184 |
|
a.quietlyJoin(); // should be no-op |
185 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
185 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
186 |
|
} |
187 |
|
|
188 |
|
try { |
219 |
|
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
220 |
|
"controlState"); |
221 |
|
|
222 |
< |
private BinaryAsyncAction parent; |
222 |
> |
private volatile BinaryAsyncAction parent; |
223 |
|
|
224 |
< |
private BinaryAsyncAction sibling; |
224 |
> |
private volatile BinaryAsyncAction sibling; |
225 |
|
|
226 |
|
protected BinaryAsyncAction() { |
227 |
|
} |
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 |
|
} |
881 |
|
protected void realCompute() { |
882 |
|
AsyncFib f = new AsyncFib(8); |
883 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
884 |
+ |
ForkJoinTask[] tasks = { f, g }; |
885 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
886 |
|
try { |
887 |
< |
invokeAll(f, g); |
887 |
> |
invokeAll(tasks); |
888 |
|
shouldThrow(); |
889 |
|
} catch (FJException success) { |
890 |
|
checkCompletedAbnormally(g, success); |
919 |
|
AsyncFib f = new AsyncFib(8); |
920 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
921 |
|
AsyncFib h = new AsyncFib(7); |
922 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
923 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
924 |
|
try { |
925 |
< |
invokeAll(f, g, h); |
925 |
> |
invokeAll(tasks); |
926 |
|
shouldThrow(); |
927 |
|
} catch (FJException success) { |
928 |
|
checkCompletedAbnormally(g, success); |
932 |
|
} |
933 |
|
|
934 |
|
/** |
935 |
< |
* invokeAll(collection) throws exception if any task does |
935 |
> |
* invokeAll(collection) throws exception if any task does |
936 |
|
*/ |
937 |
|
public void testAbnormalInvokeAllCollection() { |
938 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
940 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
941 |
|
AsyncFib g = new AsyncFib(9); |
942 |
|
AsyncFib h = new AsyncFib(7); |
943 |
< |
HashSet set = new HashSet(); |
944 |
< |
set.add(f); |
945 |
< |
set.add(g); |
932 |
< |
set.add(h); |
943 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
944 |
> |
List taskList = Arrays.asList(tasks); |
945 |
> |
Collections.shuffle(taskList); |
946 |
|
try { |
947 |
< |
invokeAll(set); |
947 |
> |
invokeAll(taskList); |
948 |
|
shouldThrow(); |
949 |
|
} catch (FJException success) { |
950 |
|
checkCompletedAbnormally(f, success); |
1551 |
|
protected void realCompute() { |
1552 |
|
AsyncFib f = new AsyncFib(8); |
1553 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1554 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1555 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1556 |
|
try { |
1557 |
< |
invokeAll(f, g); |
1557 |
> |
invokeAll(tasks); |
1558 |
|
shouldThrow(); |
1559 |
|
} catch (FJException success) { |
1560 |
|
checkCompletedAbnormally(g, success); |
1589 |
|
AsyncFib f = new AsyncFib(8); |
1590 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1591 |
|
AsyncFib h = new AsyncFib(7); |
1592 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1593 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1594 |
|
try { |
1595 |
< |
invokeAll(f, g, h); |
1595 |
> |
invokeAll(tasks); |
1596 |
|
shouldThrow(); |
1597 |
|
} catch (FJException success) { |
1598 |
|
checkCompletedAbnormally(g, success); |
1602 |
|
} |
1603 |
|
|
1604 |
|
/** |
1605 |
< |
* invokeAll(collection) throws exception if any task does |
1605 |
> |
* invokeAll(collection) throws exception if any task does |
1606 |
|
*/ |
1607 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1608 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1610 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1611 |
|
AsyncFib g = new AsyncFib(9); |
1612 |
|
AsyncFib h = new AsyncFib(7); |
1613 |
< |
HashSet set = new HashSet(); |
1614 |
< |
set.add(f); |
1615 |
< |
set.add(g); |
1599 |
< |
set.add(h); |
1613 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1614 |
> |
List taskList = Arrays.asList(tasks); |
1615 |
> |
Collections.shuffle(taskList); |
1616 |
|
try { |
1617 |
< |
invokeAll(set); |
1617 |
> |
invokeAll(taskList); |
1618 |
|
shouldThrow(); |
1619 |
|
} catch (FJException success) { |
1620 |
|
checkCompletedAbnormally(f, success); |
1623 |
|
testInvokeOnPool(singletonPool(), a); |
1624 |
|
} |
1625 |
|
|
1610 |
– |
|
1626 |
|
/** |
1627 |
|
* ForkJoinTask.quietlyComplete returns when task completes |
1628 |
|
* normally without setting a value. The most recent value |