5 |
|
*/ |
6 |
|
|
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
8 |
|
|
9 |
|
import java.util.Arrays; |
10 |
< |
import java.util.Collections; |
10 |
> |
import java.util.concurrent.CountDownLatch; |
11 |
|
import java.util.concurrent.ExecutionException; |
12 |
|
import java.util.concurrent.ForkJoinPool; |
13 |
|
import java.util.concurrent.ForkJoinTask; |
14 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
15 |
|
import java.util.concurrent.RecursiveAction; |
16 |
|
import java.util.concurrent.TimeoutException; |
17 |
|
|
35 |
|
static final short EXCEPTION_STATE = 1; |
36 |
|
|
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
|
41 |
|
public static Test suite() { |
72 |
|
} |
73 |
|
|
74 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
75 |
< |
try { |
75 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
76 |
|
assertFalse(a.isDone()); |
77 |
|
assertFalse(a.isCompletedNormally()); |
78 |
|
assertFalse(a.isCompletedAbnormally()); |
88 |
|
assertFalse(a.isCancelled()); |
89 |
|
assertNull(a.getException()); |
90 |
|
assertNull(a.getRawResult()); |
91 |
– |
} finally { |
92 |
– |
joinPool(pool); |
91 |
|
} |
92 |
|
} |
93 |
|
|
99 |
|
assertNull(a.getException()); |
100 |
|
assertNull(a.getRawResult()); |
101 |
|
if (a instanceof BinaryAsyncAction) |
102 |
< |
assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE); |
102 |
> |
assertEquals(INITIAL_STATE, |
103 |
> |
((BinaryAsyncAction)a).getForkJoinTaskTag()); |
104 |
|
|
105 |
|
try { |
106 |
< |
a.get(0L, SECONDS); |
106 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
107 |
|
shouldThrow(); |
108 |
|
} catch (TimeoutException success) { |
109 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
113 |
|
checkCompletedNormally(a, null); |
114 |
|
} |
115 |
|
|
116 |
< |
<T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) { |
116 |
> |
<T> void checkCompletedNormally(ForkJoinTask<T> a, T expectedValue) { |
117 |
|
assertTrue(a.isDone()); |
118 |
|
assertFalse(a.isCancelled()); |
119 |
|
assertTrue(a.isCompletedNormally()); |
120 |
|
assertFalse(a.isCompletedAbnormally()); |
121 |
|
assertNull(a.getException()); |
122 |
< |
assertSame(expected, a.getRawResult()); |
122 |
> |
assertSame(expectedValue, a.getRawResult()); |
123 |
|
if (a instanceof BinaryAsyncAction) |
124 |
< |
assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE); |
124 |
> |
assertEquals(COMPLETE_STATE, |
125 |
> |
((BinaryAsyncAction)a).getForkJoinTaskTag()); |
126 |
|
|
127 |
|
{ |
128 |
|
Thread.currentThread().interrupt(); |
129 |
< |
long t0 = System.nanoTime(); |
130 |
< |
assertSame(expected, a.join()); |
131 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
129 |
> |
long startTime = System.nanoTime(); |
130 |
> |
assertSame(expectedValue, a.join()); |
131 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_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) < LONG_DELAY_MS); |
140 |
|
Thread.interrupted(); |
141 |
|
} |
142 |
|
|
143 |
|
assertFalse(a.cancel(false)); |
144 |
|
assertFalse(a.cancel(true)); |
145 |
+ |
|
146 |
+ |
T v1 = null, v2 = null; |
147 |
|
try { |
148 |
< |
assertSame(expected, a.get()); |
149 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
148 |
< |
try { |
149 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
148 |
> |
v1 = a.get(); |
149 |
> |
v2 = a.get(randomTimeout(), randomTimeUnit()); |
150 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
151 |
+ |
assertSame(expectedValue, v1); |
152 |
+ |
assertSame(expectedValue, v2); |
153 |
|
} |
154 |
|
|
155 |
|
void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { |
174 |
|
Thread.interrupted(); |
175 |
|
|
176 |
|
{ |
177 |
< |
long t0 = System.nanoTime(); |
177 |
> |
long startTime = System.nanoTime(); |
178 |
|
a.quietlyJoin(); // should be no-op |
179 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
179 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
180 |
|
} |
181 |
|
|
182 |
|
try { |
187 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
188 |
|
|
189 |
|
try { |
190 |
< |
a.get(5L, SECONDS); |
190 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
191 |
|
shouldThrow(); |
192 |
|
} catch (ExecutionException success) { |
193 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
200 |
|
|
201 |
|
abstract static class BinaryAsyncAction extends ForkJoinTask<Void> { |
202 |
|
|
203 |
< |
private BinaryAsyncAction parent; |
203 |
> |
private volatile BinaryAsyncAction parent; |
204 |
|
|
205 |
< |
private BinaryAsyncAction sibling; |
205 |
> |
private volatile BinaryAsyncAction sibling; |
206 |
|
|
207 |
|
protected BinaryAsyncAction() { |
208 |
|
setForkJoinTaskTag(INITIAL_STATE); |
245 |
|
super.completeExceptionally(ex); |
246 |
|
} |
247 |
|
|
248 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
249 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
250 |
+ |
completeExceptionally(new FJException()); |
251 |
+ |
return true; |
252 |
+ |
} |
253 |
+ |
return false; |
254 |
+ |
} |
255 |
+ |
|
256 |
|
public final void complete() { |
257 |
|
BinaryAsyncAction a = this; |
258 |
|
for (;;) { |
275 |
|
} |
276 |
|
|
277 |
|
public final void completeExceptionally(Throwable ex) { |
278 |
< |
BinaryAsyncAction a = this; |
269 |
< |
while (!a.isCompletedAbnormally()) { |
278 |
> |
for (BinaryAsyncAction a = this;;) { |
279 |
|
a.completeThisExceptionally(ex); |
280 |
|
BinaryAsyncAction s = a.sibling; |
281 |
< |
if (s != null) |
282 |
< |
s.cancel(false); |
283 |
< |
if (!a.onException() || (a = a.parent) == null) |
281 |
> |
if (s != null && !s.isDone()) |
282 |
> |
s.completeExceptionally(ex); |
283 |
> |
if ((a = a.parent) == null) |
284 |
|
break; |
285 |
|
} |
286 |
|
} |
312 |
|
try { |
313 |
|
AsyncFib f = this; |
314 |
|
int n = f.number; |
315 |
< |
if (n > 1) { |
316 |
< |
while (n > 1) { |
317 |
< |
AsyncFib p = f; |
318 |
< |
AsyncFib r = new AsyncFib(n - 2); |
319 |
< |
f = new AsyncFib(--n); |
320 |
< |
p.linkSubtasks(r, f); |
312 |
< |
r.fork(); |
313 |
< |
} |
314 |
< |
f.number = n; |
315 |
> |
while (n > 1) { |
316 |
> |
AsyncFib p = f; |
317 |
> |
AsyncFib r = new AsyncFib(n - 2); |
318 |
> |
f = new AsyncFib(--n); |
319 |
> |
p.linkSubtasks(r, f); |
320 |
> |
r.fork(); |
321 |
|
} |
322 |
|
f.complete(); |
323 |
|
} |
324 |
|
catch (Throwable ex) { |
325 |
|
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
326 |
|
} |
327 |
+ |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
328 |
+ |
throw new FJException(); |
329 |
|
return false; |
330 |
|
} |
331 |
|
|
347 |
|
} |
348 |
|
|
349 |
|
public final boolean exec() { |
350 |
< |
FailingAsyncFib f = this; |
351 |
< |
int n = f.number; |
352 |
< |
if (n > 1) { |
350 |
> |
try { |
351 |
> |
FailingAsyncFib f = this; |
352 |
> |
int n = f.number; |
353 |
|
while (n > 1) { |
354 |
|
FailingAsyncFib p = f; |
355 |
|
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
357 |
|
p.linkSubtasks(r, f); |
358 |
|
r.fork(); |
359 |
|
} |
360 |
< |
f.number = n; |
360 |
> |
f.complete(); |
361 |
|
} |
362 |
< |
f.complete(); |
362 |
> |
catch (Throwable ex) { |
363 |
> |
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
364 |
> |
} |
365 |
> |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
366 |
> |
throw new FJException(); |
367 |
|
return false; |
368 |
|
} |
369 |
|
|
489 |
|
AsyncFib f = new AsyncFib(8); |
490 |
|
assertSame(f, f.fork()); |
491 |
|
try { |
492 |
< |
f.get(5L, null); |
492 |
> |
f.get(randomTimeout(), null); |
493 |
|
shouldThrow(); |
494 |
|
} catch (NullPointerException success) {} |
495 |
|
}}; |
923 |
|
AsyncFib f = new AsyncFib(8); |
924 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
925 |
|
ForkJoinTask[] tasks = { f, g }; |
926 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
926 |
> |
shuffle(tasks); |
927 |
|
try { |
928 |
|
invokeAll(tasks[0], tasks[1]); |
929 |
|
shouldThrow(); |
950 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
951 |
|
AsyncFib h = new AsyncFib(7); |
952 |
|
ForkJoinTask[] tasks = { f, g, h }; |
953 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
953 |
> |
shuffle(tasks); |
954 |
|
try { |
955 |
|
invokeAll(tasks[0], tasks[1], tasks[2]); |
956 |
|
shouldThrow(); |
977 |
|
AsyncFib g = new AsyncFib(9); |
978 |
|
AsyncFib h = new AsyncFib(7); |
979 |
|
ForkJoinTask[] tasks = { f, g, h }; |
980 |
< |
Collections.shuffle(Arrays.asList(tasks)); |
980 |
> |
shuffle(tasks); |
981 |
|
try { |
982 |
|
invokeAll(Arrays.asList(tasks)); |
983 |
|
shouldThrow(); |
1164 |
|
testInvokeOnPool(mainPool(), a); |
1165 |
|
} |
1166 |
|
|
1167 |
+ |
// jdk9 |
1168 |
+ |
|
1169 |
+ |
/** |
1170 |
+ |
* pollSubmission returns unexecuted submitted task, if present |
1171 |
+ |
*/ |
1172 |
+ |
public void testPollSubmission() { |
1173 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1174 |
+ |
final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); |
1175 |
+ |
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1176 |
+ |
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1177 |
+ |
final ForkJoinPool p = singletonPool(); |
1178 |
+ |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1179 |
+ |
Thread external = new Thread(new CheckedRunnable() { |
1180 |
+ |
public void realRun() { |
1181 |
+ |
p.execute(a); |
1182 |
+ |
p.execute(b); |
1183 |
+ |
p.execute(c); |
1184 |
+ |
}}); |
1185 |
+ |
RecursiveAction s = new CheckedRecursiveAction() { |
1186 |
+ |
protected void realCompute() { |
1187 |
+ |
external.start(); |
1188 |
+ |
try { |
1189 |
+ |
external.join(); |
1190 |
+ |
} catch (Exception ex) { |
1191 |
+ |
threadUnexpectedException(ex); |
1192 |
+ |
} |
1193 |
+ |
assertTrue(p.hasQueuedSubmissions()); |
1194 |
+ |
assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); |
1195 |
+ |
ForkJoinTask r = ForkJoinTask.pollSubmission(); |
1196 |
+ |
assertTrue(r == a || r == b || r == c); |
1197 |
+ |
assertFalse(r.isDone()); |
1198 |
+ |
}}; |
1199 |
+ |
p.invoke(s); |
1200 |
+ |
} |
1201 |
+ |
} |
1202 |
+ |
|
1203 |
|
} |