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; |
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() { |