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 |
|
} |
874 |
|
protected void realCompute() { |
875 |
|
AsyncFib f = new AsyncFib(8); |
876 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
877 |
+ |
ForkJoinTask[] tasks = { f, g }; |
878 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
879 |
|
try { |
880 |
< |
invokeAll(f, g); |
880 |
> |
invokeAll(tasks); |
881 |
|
shouldThrow(); |
882 |
|
} catch (FJException success) { |
883 |
|
checkCompletedAbnormally(g, success); |
912 |
|
AsyncFib f = new AsyncFib(8); |
913 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
914 |
|
AsyncFib h = new AsyncFib(7); |
915 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
916 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
917 |
|
try { |
918 |
< |
invokeAll(f, g, h); |
918 |
> |
invokeAll(tasks); |
919 |
|
shouldThrow(); |
920 |
|
} catch (FJException success) { |
921 |
|
checkCompletedAbnormally(g, success); |
933 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
934 |
|
AsyncFib g = new AsyncFib(9); |
935 |
|
AsyncFib h = new AsyncFib(7); |
936 |
< |
HashSet set = new HashSet(); |
937 |
< |
set.add(f); |
938 |
< |
set.add(g); |
932 |
< |
set.add(h); |
936 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
937 |
> |
List taskList = Arrays.asList(tasks); |
938 |
> |
Collections.shuffle(taskList); |
939 |
|
try { |
940 |
< |
invokeAll(set); |
940 |
> |
invokeAll(taskList); |
941 |
|
shouldThrow(); |
942 |
|
} catch (FJException success) { |
943 |
|
checkCompletedAbnormally(f, success); |
1544 |
|
protected void realCompute() { |
1545 |
|
AsyncFib f = new AsyncFib(8); |
1546 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1547 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1548 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1549 |
|
try { |
1550 |
< |
invokeAll(f, g); |
1550 |
> |
invokeAll(tasks); |
1551 |
|
shouldThrow(); |
1552 |
|
} catch (FJException success) { |
1553 |
|
checkCompletedAbnormally(g, success); |
1582 |
|
AsyncFib f = new AsyncFib(8); |
1583 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1584 |
|
AsyncFib h = new AsyncFib(7); |
1585 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1586 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1587 |
|
try { |
1588 |
< |
invokeAll(f, g, h); |
1588 |
> |
invokeAll(tasks); |
1589 |
|
shouldThrow(); |
1590 |
|
} catch (FJException success) { |
1591 |
|
checkCompletedAbnormally(g, success); |
1603 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1604 |
|
AsyncFib g = new AsyncFib(9); |
1605 |
|
AsyncFib h = new AsyncFib(7); |
1606 |
< |
HashSet set = new HashSet(); |
1607 |
< |
set.add(f); |
1608 |
< |
set.add(g); |
1599 |
< |
set.add(h); |
1606 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1607 |
> |
List taskList = Arrays.asList(tasks); |
1608 |
> |
Collections.shuffle(taskList); |
1609 |
|
try { |
1610 |
< |
invokeAll(set); |
1610 |
> |
invokeAll(taskList); |
1611 |
|
shouldThrow(); |
1612 |
|
} catch (FJException success) { |
1613 |
|
checkCompletedAbnormally(f, success); |