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.ForkJoinTask; |
13 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
14 |
|
import java.util.concurrent.RecursiveAction; |
15 |
|
import java.util.concurrent.TimeUnit; |
61 |
|
assertNull(a.getException()); |
62 |
|
assertNull(a.getRawResult()); |
63 |
|
|
64 |
< |
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
64 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
65 |
|
Thread.currentThread().interrupt(); |
66 |
|
try { |
67 |
|
a.get(); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
+ |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
323 |
+ |
* succeeds in the presence of interrupts |
324 |
+ |
*/ |
325 |
+ |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
326 |
+ |
final SynchronousQueue<FibAction[]> sq = |
327 |
+ |
new SynchronousQueue<FibAction[]>(); |
328 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
329 |
+ |
public void realCompute() throws InterruptedException { |
330 |
+ |
FibAction[] fibActions = new FibAction[6]; |
331 |
+ |
for (int i = 0; i < fibActions.length; i++) |
332 |
+ |
fibActions[i] = new FibAction(8); |
333 |
+ |
|
334 |
+ |
fibActions[1].cancel(false); |
335 |
+ |
fibActions[2].completeExceptionally(new FJException()); |
336 |
+ |
fibActions[4].cancel(true); |
337 |
+ |
fibActions[5].completeExceptionally(new FJException()); |
338 |
+ |
|
339 |
+ |
for (int i = 0; i < fibActions.length; i++) |
340 |
+ |
fibActions[i].fork(); |
341 |
+ |
|
342 |
+ |
sq.put(fibActions); |
343 |
+ |
|
344 |
+ |
helpQuiesce(); |
345 |
+ |
}}; |
346 |
+ |
|
347 |
+ |
Runnable r = new CheckedRunnable() { |
348 |
+ |
public void realRun() throws InterruptedException { |
349 |
+ |
FibAction[] fibActions = sq.take(); |
350 |
+ |
FibAction f; |
351 |
+ |
|
352 |
+ |
// test join() ------------ |
353 |
+ |
|
354 |
+ |
f = fibActions[0]; |
355 |
+ |
assertFalse(ForkJoinTask.inForkJoinPool()); |
356 |
+ |
Thread.currentThread().interrupt(); |
357 |
+ |
assertNull(f.join()); |
358 |
+ |
assertTrue(Thread.interrupted()); |
359 |
+ |
assertEquals(21, f.result); |
360 |
+ |
checkCompletedNormally(f); |
361 |
+ |
|
362 |
+ |
f = fibActions[1]; |
363 |
+ |
Thread.currentThread().interrupt(); |
364 |
+ |
try { |
365 |
+ |
f.join(); |
366 |
+ |
shouldThrow(); |
367 |
+ |
} catch (CancellationException success) { |
368 |
+ |
assertTrue(Thread.interrupted()); |
369 |
+ |
checkCancelled(f); |
370 |
+ |
} |
371 |
+ |
|
372 |
+ |
f = fibActions[2]; |
373 |
+ |
Thread.currentThread().interrupt(); |
374 |
+ |
try { |
375 |
+ |
f.join(); |
376 |
+ |
shouldThrow(); |
377 |
+ |
} catch (FJException success) { |
378 |
+ |
assertTrue(Thread.interrupted()); |
379 |
+ |
checkCompletedAbnormally(f, success); |
380 |
+ |
} |
381 |
+ |
|
382 |
+ |
// test quietlyJoin() --------- |
383 |
+ |
|
384 |
+ |
f = fibActions[3]; |
385 |
+ |
Thread.currentThread().interrupt(); |
386 |
+ |
f.quietlyJoin(); |
387 |
+ |
assertTrue(Thread.interrupted()); |
388 |
+ |
assertEquals(21, f.result); |
389 |
+ |
checkCompletedNormally(f); |
390 |
+ |
|
391 |
+ |
f = fibActions[4]; |
392 |
+ |
Thread.currentThread().interrupt(); |
393 |
+ |
f.quietlyJoin(); |
394 |
+ |
assertTrue(Thread.interrupted()); |
395 |
+ |
checkCancelled(f); |
396 |
+ |
|
397 |
+ |
f = fibActions[5]; |
398 |
+ |
Thread.currentThread().interrupt(); |
399 |
+ |
f.quietlyJoin(); |
400 |
+ |
assertTrue(Thread.interrupted()); |
401 |
+ |
assertTrue(f.getException() instanceof FJException); |
402 |
+ |
checkCompletedAbnormally(f, f.getException()); |
403 |
+ |
}}; |
404 |
+ |
|
405 |
+ |
Thread t; |
406 |
+ |
|
407 |
+ |
t = newStartedThread(r); |
408 |
+ |
testInvokeOnPool(mainPool(), a); |
409 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
410 |
+ |
|
411 |
+ |
a.reinitialize(); |
412 |
+ |
t = newStartedThread(r); |
413 |
+ |
testInvokeOnPool(singletonPool(), a); |
414 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
415 |
+ |
} |
416 |
+ |
|
417 |
+ |
/** |
418 |
|
* get of a forked task returns when task completes |
419 |
|
*/ |
420 |
|
public void testForkGet() { |