3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
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; |
18 |
|
import java.util.concurrent.RecursiveAction; |
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 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()); |
63 |
– |
} finally { |
64 |
– |
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; |
276 |
< |
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); |
337 |
< |
r.fork(); |
338 |
< |
} |
339 |
< |
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); |
365 |
< |
r.fork(); |
366 |
< |
} |
367 |
< |
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); |
930 |
< |
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); |
1597 |
< |
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); |