6 |
|
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.concurrent.CancellationException; |
9 |
+ |
import java.util.concurrent.SynchronousQueue; |
10 |
|
import java.util.concurrent.ExecutionException; |
11 |
|
import java.util.concurrent.ForkJoinPool; |
12 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
91 |
|
assertNull(a.getException()); |
92 |
|
assertNull(a.getRawResult()); |
93 |
|
assertNull(a.join()); |
94 |
+ |
assertFalse(a.cancel(false)); |
95 |
+ |
assertFalse(a.cancel(true)); |
96 |
|
try { |
97 |
|
assertNull(a.get()); |
98 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
135 |
|
assertTrue(a.isCompletedAbnormally()); |
136 |
|
assertSame(t, a.getException()); |
137 |
|
assertNull(a.getRawResult()); |
138 |
+ |
assertFalse(a.cancel(false)); |
139 |
+ |
assertFalse(a.cancel(true)); |
140 |
|
|
141 |
|
try { |
142 |
|
a.join(); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
+ |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
252 |
+ |
*/ |
253 |
+ |
public void testJoinIgnoresInterrupts() { |
254 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
255 |
+ |
public void realCompute() { |
256 |
+ |
FibAction f = new FibAction(8); |
257 |
+ |
|
258 |
+ |
// test join() |
259 |
+ |
assertSame(f, f.fork()); |
260 |
+ |
Thread.currentThread().interrupt(); |
261 |
+ |
assertNull(f.join()); |
262 |
+ |
Thread.interrupted(); |
263 |
+ |
assertEquals(21, f.result); |
264 |
+ |
checkCompletedNormally(f); |
265 |
+ |
|
266 |
+ |
f.reinitialize(); |
267 |
+ |
f.cancel(true); |
268 |
+ |
assertSame(f, f.fork()); |
269 |
+ |
Thread.currentThread().interrupt(); |
270 |
+ |
try { |
271 |
+ |
f.join(); |
272 |
+ |
shouldThrow(); |
273 |
+ |
} catch (CancellationException success) { |
274 |
+ |
Thread.interrupted(); |
275 |
+ |
checkCancelled(f); |
276 |
+ |
} |
277 |
+ |
|
278 |
+ |
f.reinitialize(); |
279 |
+ |
f.completeExceptionally(new FJException()); |
280 |
+ |
assertSame(f, f.fork()); |
281 |
+ |
Thread.currentThread().interrupt(); |
282 |
+ |
try { |
283 |
+ |
f.join(); |
284 |
+ |
shouldThrow(); |
285 |
+ |
} catch (FJException success) { |
286 |
+ |
Thread.interrupted(); |
287 |
+ |
checkCompletedAbnormally(f, success); |
288 |
+ |
} |
289 |
+ |
|
290 |
+ |
// test quietlyJoin() |
291 |
+ |
f.reinitialize(); |
292 |
+ |
assertSame(f, f.fork()); |
293 |
+ |
Thread.currentThread().interrupt(); |
294 |
+ |
f.quietlyJoin(); |
295 |
+ |
Thread.interrupted(); |
296 |
+ |
assertEquals(21, f.result); |
297 |
+ |
checkCompletedNormally(f); |
298 |
+ |
|
299 |
+ |
f.reinitialize(); |
300 |
+ |
f.cancel(true); |
301 |
+ |
assertSame(f, f.fork()); |
302 |
+ |
Thread.currentThread().interrupt(); |
303 |
+ |
f.quietlyJoin(); |
304 |
+ |
Thread.interrupted(); |
305 |
+ |
checkCancelled(f); |
306 |
+ |
|
307 |
+ |
f.reinitialize(); |
308 |
+ |
f.completeExceptionally(new FJException()); |
309 |
+ |
assertSame(f, f.fork()); |
310 |
+ |
Thread.currentThread().interrupt(); |
311 |
+ |
f.quietlyJoin(); |
312 |
+ |
Thread.interrupted(); |
313 |
+ |
checkCompletedAbnormally(f, f.getException()); |
314 |
+ |
}}; |
315 |
+ |
testInvokeOnPool(mainPool(), a); |
316 |
+ |
a.reinitialize(); |
317 |
+ |
testInvokeOnPool(singletonPool(), a); |
318 |
+ |
} |
319 |
+ |
|
320 |
+ |
/** |
321 |
+ |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
322 |
+ |
* succeeds in the presence of interrupts |
323 |
+ |
*/ |
324 |
+ |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
325 |
+ |
final SynchronousQueue<FibAction[]> sq = |
326 |
+ |
new SynchronousQueue<FibAction[]>(); |
327 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
328 |
+ |
public void realCompute() throws InterruptedException { |
329 |
+ |
FibAction[] fibActions = new FibAction[6]; |
330 |
+ |
for (int i = 0; i < fibActions.length; i++) |
331 |
+ |
fibActions[i] = new FibAction(8); |
332 |
+ |
|
333 |
+ |
fibActions[1].cancel(false); |
334 |
+ |
fibActions[2].completeExceptionally(new FJException()); |
335 |
+ |
fibActions[4].cancel(true); |
336 |
+ |
fibActions[5].completeExceptionally(new FJException()); |
337 |
+ |
|
338 |
+ |
for (int i = 0; i < fibActions.length; i++) |
339 |
+ |
fibActions[i].fork(); |
340 |
+ |
|
341 |
+ |
sq.put(fibActions); |
342 |
+ |
|
343 |
+ |
helpQuiesce(); |
344 |
+ |
}}; |
345 |
+ |
|
346 |
+ |
Runnable r = new CheckedRunnable() { |
347 |
+ |
public void realRun() throws InterruptedException { |
348 |
+ |
FibAction[] fibActions = sq.take(); |
349 |
+ |
FibAction f; |
350 |
+ |
|
351 |
+ |
// test join() ------------ |
352 |
+ |
|
353 |
+ |
f = fibActions[0]; |
354 |
+ |
assertFalse(f.inForkJoinPool()); |
355 |
+ |
Thread.currentThread().interrupt(); |
356 |
+ |
assertNull(f.join()); |
357 |
+ |
assertTrue(Thread.interrupted()); |
358 |
+ |
assertEquals(21, f.result); |
359 |
+ |
checkCompletedNormally(f); |
360 |
+ |
|
361 |
+ |
f = fibActions[1]; |
362 |
+ |
Thread.currentThread().interrupt(); |
363 |
+ |
try { |
364 |
+ |
f.join(); |
365 |
+ |
shouldThrow(); |
366 |
+ |
} catch (CancellationException success) { |
367 |
+ |
assertTrue(Thread.interrupted()); |
368 |
+ |
checkCancelled(f); |
369 |
+ |
} |
370 |
+ |
|
371 |
+ |
f = fibActions[2]; |
372 |
+ |
Thread.currentThread().interrupt(); |
373 |
+ |
try { |
374 |
+ |
f.join(); |
375 |
+ |
shouldThrow(); |
376 |
+ |
} catch (FJException success) { |
377 |
+ |
assertTrue(Thread.interrupted()); |
378 |
+ |
checkCompletedAbnormally(f, success); |
379 |
+ |
} |
380 |
+ |
|
381 |
+ |
// test quietlyJoin() --------- |
382 |
+ |
|
383 |
+ |
f = fibActions[3]; |
384 |
+ |
Thread.currentThread().interrupt(); |
385 |
+ |
f.quietlyJoin(); |
386 |
+ |
assertTrue(Thread.interrupted()); |
387 |
+ |
assertEquals(21, f.result); |
388 |
+ |
checkCompletedNormally(f); |
389 |
+ |
|
390 |
+ |
f = fibActions[4]; |
391 |
+ |
Thread.currentThread().interrupt(); |
392 |
+ |
f.quietlyJoin(); |
393 |
+ |
assertTrue(Thread.interrupted()); |
394 |
+ |
checkCancelled(f); |
395 |
+ |
|
396 |
+ |
f = fibActions[5]; |
397 |
+ |
Thread.currentThread().interrupt(); |
398 |
+ |
f.quietlyJoin(); |
399 |
+ |
assertTrue(Thread.interrupted()); |
400 |
+ |
assertTrue(f.getException() instanceof FJException); |
401 |
+ |
checkCompletedAbnormally(f, f.getException()); |
402 |
+ |
}}; |
403 |
+ |
|
404 |
+ |
Thread t; |
405 |
+ |
|
406 |
+ |
t = newStartedThread(r); |
407 |
+ |
testInvokeOnPool(mainPool(), a); |
408 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
409 |
+ |
|
410 |
+ |
a.reinitialize(); |
411 |
+ |
t = newStartedThread(r); |
412 |
+ |
testInvokeOnPool(singletonPool(), a); |
413 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
414 |
+ |
} |
415 |
+ |
|
416 |
+ |
/** |
417 |
|
* get of a forked task returns when task completes |
418 |
|
*/ |
419 |
|
public void testForkGet() { |
769 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
|
public void realCompute() { |
771 |
|
setRawResult(null); |
772 |
+ |
assertNull(getRawResult()); |
773 |
|
}}; |
774 |
|
assertNull(a.invoke()); |
775 |
|
} |