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