8 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
|
|
10 |
|
import java.util.Arrays; |
11 |
+ |
import java.util.Collections; |
12 |
+ |
import java.util.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
16 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
17 |
|
import java.util.concurrent.RecursiveAction; |
18 |
|
import java.util.concurrent.TimeoutException; |
19 |
|
|
37 |
|
static final short EXCEPTION_STATE = 1; |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
74 |
|
} |
75 |
|
|
76 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
77 |
< |
try { |
77 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
78 |
|
assertFalse(a.isDone()); |
79 |
|
assertFalse(a.isCompletedNormally()); |
80 |
|
assertFalse(a.isCompletedAbnormally()); |
90 |
|
assertFalse(a.isCancelled()); |
91 |
|
assertNull(a.getException()); |
92 |
|
assertNull(a.getRawResult()); |
90 |
– |
} finally { |
91 |
– |
joinPool(pool); |
93 |
|
} |
94 |
|
} |
95 |
|
|
126 |
|
|
127 |
|
{ |
128 |
|
Thread.currentThread().interrupt(); |
129 |
< |
long t0 = System.nanoTime(); |
129 |
> |
long startTime = System.nanoTime(); |
130 |
|
assertSame(expected, a.join()); |
131 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
131 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
132 |
|
Thread.interrupted(); |
133 |
|
} |
134 |
|
|
135 |
|
{ |
136 |
|
Thread.currentThread().interrupt(); |
137 |
< |
long t0 = System.nanoTime(); |
137 |
> |
long startTime = System.nanoTime(); |
138 |
|
a.quietlyJoin(); // should be no-op |
139 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
139 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
140 |
|
Thread.interrupted(); |
141 |
|
} |
142 |
|
|
172 |
|
Thread.interrupted(); |
173 |
|
|
174 |
|
{ |
175 |
< |
long t0 = System.nanoTime(); |
175 |
> |
long startTime = System.nanoTime(); |
176 |
|
a.quietlyJoin(); // should be no-op |
177 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
177 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
178 |
|
} |
179 |
|
|
180 |
|
try { |
198 |
|
|
199 |
|
abstract static class BinaryAsyncAction extends ForkJoinTask<Void> { |
200 |
|
|
201 |
< |
private BinaryAsyncAction parent; |
201 |
> |
private volatile BinaryAsyncAction parent; |
202 |
|
|
203 |
< |
private BinaryAsyncAction sibling; |
203 |
> |
private volatile BinaryAsyncAction sibling; |
204 |
|
|
205 |
|
protected BinaryAsyncAction() { |
206 |
|
setForkJoinTaskTag(INITIAL_STATE); |
243 |
|
super.completeExceptionally(ex); |
244 |
|
} |
245 |
|
|
246 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
247 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
248 |
+ |
completeExceptionally(new FJException()); |
249 |
+ |
return true; |
250 |
+ |
} |
251 |
+ |
return false; |
252 |
+ |
} |
253 |
+ |
|
254 |
|
public final void complete() { |
255 |
|
BinaryAsyncAction a = this; |
256 |
|
for (;;) { |
273 |
|
} |
274 |
|
|
275 |
|
public final void completeExceptionally(Throwable ex) { |
276 |
< |
BinaryAsyncAction a = this; |
268 |
< |
while (!a.isCompletedAbnormally()) { |
276 |
> |
for (BinaryAsyncAction a = this;;) { |
277 |
|
a.completeThisExceptionally(ex); |
278 |
|
BinaryAsyncAction s = a.sibling; |
279 |
< |
if (s != null) |
280 |
< |
s.cancel(false); |
281 |
< |
if (!a.onException() || (a = a.parent) == null) |
279 |
> |
if (s != null && !s.isDone()) |
280 |
> |
s.completeExceptionally(ex); |
281 |
> |
if ((a = a.parent) == null) |
282 |
|
break; |
283 |
|
} |
284 |
|
} |
325 |
|
catch (Throwable ex) { |
326 |
|
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
327 |
|
} |
328 |
+ |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
329 |
+ |
throw new FJException(); |
330 |
|
return false; |
331 |
|
} |
332 |
|
|
348 |
|
} |
349 |
|
|
350 |
|
public final boolean exec() { |
351 |
< |
FailingAsyncFib f = this; |
352 |
< |
int n = f.number; |
353 |
< |
if (n > 1) { |
354 |
< |
while (n > 1) { |
355 |
< |
FailingAsyncFib p = f; |
356 |
< |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
357 |
< |
f = new FailingAsyncFib(--n); |
358 |
< |
p.linkSubtasks(r, f); |
359 |
< |
r.fork(); |
351 |
> |
try { |
352 |
> |
FailingAsyncFib f = this; |
353 |
> |
int n = f.number; |
354 |
> |
if (n > 1) { |
355 |
> |
while (n > 1) { |
356 |
> |
FailingAsyncFib p = f; |
357 |
> |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
358 |
> |
f = new FailingAsyncFib(--n); |
359 |
> |
p.linkSubtasks(r, f); |
360 |
> |
r.fork(); |
361 |
> |
} |
362 |
> |
f.number = n; |
363 |
|
} |
364 |
< |
f.number = n; |
364 |
> |
f.complete(); |
365 |
|
} |
366 |
< |
f.complete(); |
366 |
> |
catch (Throwable ex) { |
367 |
> |
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
368 |
> |
} |
369 |
> |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
370 |
> |
throw new FJException(); |
371 |
|
return false; |
372 |
|
} |
373 |
|
|
890 |
|
} |
891 |
|
|
892 |
|
/** |
893 |
< |
* invokeAll(t1, t2) throw exception if any task does |
893 |
> |
* invokeAll(tasks) with 1 argument throws exception if task does |
894 |
|
*/ |
895 |
< |
public void testAbnormalInvokeAll2() { |
896 |
< |
testAbnormalInvokeAll2(mainPool()); |
895 |
> |
public void testAbnormalInvokeAll1() { |
896 |
> |
testAbnormalInvokeAll1(mainPool()); |
897 |
|
} |
898 |
< |
public void testAbnormalInvokeAll2_Singleton() { |
899 |
< |
testAbnormalInvokeAll2(singletonPool()); |
898 |
> |
public void testAbnormalInvokeAll1_Singleton() { |
899 |
> |
testAbnormalInvokeAll1(singletonPool()); |
900 |
|
} |
901 |
< |
public void testAbnormalInvokeAll2(ForkJoinPool pool) { |
901 |
> |
public void testAbnormalInvokeAll1(ForkJoinPool pool) { |
902 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
903 |
|
protected void realCompute() { |
887 |
– |
AsyncFib f = new AsyncFib(8); |
904 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
905 |
|
try { |
906 |
< |
invokeAll(f, g); |
906 |
> |
invokeAll(g); |
907 |
|
shouldThrow(); |
908 |
|
} catch (FJException success) { |
909 |
|
checkCompletedAbnormally(g, success); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
< |
* invokeAll(tasks) with 1 argument throws exception if task does |
916 |
> |
* invokeAll(t1, t2) throw exception if any task does |
917 |
|
*/ |
918 |
< |
public void testAbnormalInvokeAll1() { |
919 |
< |
testAbnormalInvokeAll1(mainPool()); |
918 |
> |
public void testAbnormalInvokeAll2() { |
919 |
> |
testAbnormalInvokeAll2(mainPool()); |
920 |
|
} |
921 |
< |
public void testAbnormalInvokeAll1_Singleton() { |
922 |
< |
testAbnormalInvokeAll1(singletonPool()); |
921 |
> |
public void testAbnormalInvokeAll2_Singleton() { |
922 |
> |
testAbnormalInvokeAll2(singletonPool()); |
923 |
|
} |
924 |
< |
public void testAbnormalInvokeAll1(ForkJoinPool pool) { |
924 |
> |
public void testAbnormalInvokeAll2(ForkJoinPool pool) { |
925 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
926 |
|
protected void realCompute() { |
927 |
+ |
AsyncFib f = new AsyncFib(8); |
928 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
929 |
+ |
ForkJoinTask[] tasks = { f, g }; |
930 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
931 |
|
try { |
932 |
< |
invokeAll(g); |
932 |
> |
invokeAll(tasks[0], tasks[1]); |
933 |
|
shouldThrow(); |
934 |
|
} catch (FJException success) { |
935 |
|
checkCompletedAbnormally(g, success); |
953 |
|
AsyncFib f = new AsyncFib(8); |
954 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
955 |
|
AsyncFib h = new AsyncFib(7); |
956 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
957 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
958 |
|
try { |
959 |
< |
invokeAll(f, g, h); |
959 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
960 |
|
shouldThrow(); |
961 |
|
} catch (FJException success) { |
962 |
|
checkCompletedAbnormally(g, success); |
981 |
|
AsyncFib g = new AsyncFib(9); |
982 |
|
AsyncFib h = new AsyncFib(7); |
983 |
|
ForkJoinTask[] tasks = { f, g, h }; |
984 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
985 |
|
try { |
986 |
|
invokeAll(Arrays.asList(tasks)); |
987 |
|
shouldThrow(); |
1168 |
|
testInvokeOnPool(mainPool(), a); |
1169 |
|
} |
1170 |
|
|
1171 |
+ |
// jdk9 |
1172 |
+ |
|
1173 |
+ |
/** |
1174 |
+ |
* pollSubmission returns unexecuted submitted task, if present |
1175 |
+ |
*/ |
1176 |
+ |
public void testPollSubmission() { |
1177 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1178 |
+ |
final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); |
1179 |
+ |
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1180 |
+ |
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1181 |
+ |
final ForkJoinPool p = singletonPool(); |
1182 |
+ |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1183 |
+ |
Thread external = new Thread(new CheckedRunnable() { |
1184 |
+ |
public void realRun() { |
1185 |
+ |
p.execute(a); |
1186 |
+ |
p.execute(b); |
1187 |
+ |
p.execute(c); |
1188 |
+ |
}}); |
1189 |
+ |
RecursiveAction s = new CheckedRecursiveAction() { |
1190 |
+ |
protected void realCompute() { |
1191 |
+ |
external.start(); |
1192 |
+ |
try { |
1193 |
+ |
external.join(); |
1194 |
+ |
} catch (Exception ex) { |
1195 |
+ |
threadUnexpectedException(ex); |
1196 |
+ |
} |
1197 |
+ |
assertTrue(p.hasQueuedSubmissions()); |
1198 |
+ |
assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); |
1199 |
+ |
ForkJoinTask r = ForkJoinTask.pollSubmission(); |
1200 |
+ |
assertTrue(r == a || r == b || r == c); |
1201 |
+ |
assertFalse(r.isDone()); |
1202 |
+ |
}}; |
1203 |
+ |
p.invoke(s); |
1204 |
+ |
} |
1205 |
+ |
} |
1206 |
+ |
|
1207 |
|
} |