1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
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.ThreadLocalRandom; |
16 |
|
import java.util.concurrent.TimeUnit; |
17 |
|
import java.util.concurrent.TimeoutException; |
18 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
+ |
import java.util.Arrays; |
20 |
|
import java.util.HashSet; |
21 |
|
|
22 |
|
public class RecursiveActionTest extends JSR166TestCase { |
63 |
|
assertNull(a.getException()); |
64 |
|
assertNull(a.getRawResult()); |
65 |
|
|
66 |
< |
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
66 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
67 |
|
Thread.currentThread().interrupt(); |
68 |
|
try { |
69 |
|
a.get(); |
136 |
|
assertFalse(a.isCancelled()); |
137 |
|
assertFalse(a.isCompletedNormally()); |
138 |
|
assertTrue(a.isCompletedAbnormally()); |
139 |
< |
assertSame(t, a.getException()); |
139 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
140 |
|
assertNull(a.getRawResult()); |
141 |
|
assertFalse(a.cancel(false)); |
142 |
|
assertFalse(a.cancel(true)); |
145 |
|
a.join(); |
146 |
|
shouldThrow(); |
147 |
|
} catch (Throwable expected) { |
148 |
< |
assertSame(t, expected); |
148 |
> |
assertSame(expected.getClass(), t.getClass()); |
149 |
|
} |
150 |
|
|
151 |
|
try { |
152 |
|
a.get(); |
153 |
|
shouldThrow(); |
154 |
|
} catch (ExecutionException success) { |
155 |
< |
assertSame(t, success.getCause()); |
155 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
156 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
157 |
|
|
158 |
|
try { |
159 |
|
a.get(5L, SECONDS); |
160 |
|
shouldThrow(); |
161 |
|
} catch (ExecutionException success) { |
162 |
< |
assertSame(t, success.getCause()); |
162 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
163 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
164 |
|
} |
165 |
|
|
166 |
< |
static final class FJException extends RuntimeException { |
167 |
< |
FJException() { super(); } |
166 |
> |
public static final class FJException extends RuntimeException { |
167 |
> |
public FJException() { super(); } |
168 |
> |
public FJException(Throwable cause) { super(cause); } |
169 |
|
} |
170 |
|
|
171 |
|
// A simple recursive action for testing |
173 |
|
final int number; |
174 |
|
int result; |
175 |
|
FibAction(int n) { number = n; } |
176 |
< |
public void realCompute() { |
176 |
> |
protected void realCompute() { |
177 |
|
int n = number; |
178 |
|
if (n <= 1) |
179 |
|
result = n; |
211 |
|
*/ |
212 |
|
public void testInvoke() { |
213 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
214 |
< |
public void realCompute() { |
214 |
> |
protected void realCompute() { |
215 |
|
FibAction f = new FibAction(8); |
216 |
|
assertNull(f.invoke()); |
217 |
|
assertEquals(21, f.result); |
227 |
|
*/ |
228 |
|
public void testQuietlyInvoke() { |
229 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
230 |
< |
public void realCompute() { |
230 |
> |
protected void realCompute() { |
231 |
|
FibAction f = new FibAction(8); |
232 |
|
f.quietlyInvoke(); |
233 |
|
assertEquals(21, f.result); |
241 |
|
*/ |
242 |
|
public void testForkJoin() { |
243 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
244 |
< |
public void realCompute() { |
244 |
> |
protected void realCompute() { |
245 |
|
FibAction f = new FibAction(8); |
246 |
|
assertSame(f, f.fork()); |
247 |
|
assertNull(f.join()); |
256 |
|
*/ |
257 |
|
public void testJoinIgnoresInterrupts() { |
258 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
259 |
< |
public void realCompute() { |
259 |
> |
protected void realCompute() { |
260 |
|
FibAction f = new FibAction(8); |
261 |
+ |
final Thread myself = Thread.currentThread(); |
262 |
|
|
263 |
|
// test join() |
264 |
|
assertSame(f, f.fork()); |
265 |
< |
Thread.currentThread().interrupt(); |
265 |
> |
myself.interrupt(); |
266 |
> |
assertTrue(myself.isInterrupted()); |
267 |
|
assertNull(f.join()); |
268 |
|
Thread.interrupted(); |
269 |
|
assertEquals(21, f.result); |
270 |
|
checkCompletedNormally(f); |
271 |
|
|
272 |
< |
f.reinitialize(); |
272 |
> |
f = new FibAction(8); |
273 |
|
f.cancel(true); |
274 |
|
assertSame(f, f.fork()); |
275 |
< |
Thread.currentThread().interrupt(); |
275 |
> |
myself.interrupt(); |
276 |
> |
assertTrue(myself.isInterrupted()); |
277 |
|
try { |
278 |
|
f.join(); |
279 |
|
shouldThrow(); |
282 |
|
checkCancelled(f); |
283 |
|
} |
284 |
|
|
285 |
< |
f.reinitialize(); |
285 |
> |
f = new FibAction(8); |
286 |
|
f.completeExceptionally(new FJException()); |
287 |
|
assertSame(f, f.fork()); |
288 |
< |
Thread.currentThread().interrupt(); |
288 |
> |
myself.interrupt(); |
289 |
> |
assertTrue(myself.isInterrupted()); |
290 |
|
try { |
291 |
|
f.join(); |
292 |
|
shouldThrow(); |
296 |
|
} |
297 |
|
|
298 |
|
// test quietlyJoin() |
299 |
< |
f.reinitialize(); |
299 |
> |
f = new FibAction(8); |
300 |
|
assertSame(f, f.fork()); |
301 |
< |
Thread.currentThread().interrupt(); |
301 |
> |
myself.interrupt(); |
302 |
> |
assertTrue(myself.isInterrupted()); |
303 |
|
f.quietlyJoin(); |
304 |
|
Thread.interrupted(); |
305 |
|
assertEquals(21, f.result); |
306 |
|
checkCompletedNormally(f); |
307 |
|
|
308 |
< |
f.reinitialize(); |
308 |
> |
f = new FibAction(8); |
309 |
|
f.cancel(true); |
310 |
|
assertSame(f, f.fork()); |
311 |
< |
Thread.currentThread().interrupt(); |
311 |
> |
myself.interrupt(); |
312 |
> |
assertTrue(myself.isInterrupted()); |
313 |
|
f.quietlyJoin(); |
314 |
|
Thread.interrupted(); |
315 |
|
checkCancelled(f); |
316 |
|
|
317 |
< |
f.reinitialize(); |
317 |
> |
f = new FibAction(8); |
318 |
|
f.completeExceptionally(new FJException()); |
319 |
|
assertSame(f, f.fork()); |
320 |
< |
Thread.currentThread().interrupt(); |
320 |
> |
myself.interrupt(); |
321 |
> |
assertTrue(myself.isInterrupted()); |
322 |
|
f.quietlyJoin(); |
323 |
|
Thread.interrupted(); |
324 |
|
checkCompletedAbnormally(f, f.getException()); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
+ |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
333 |
+ |
* succeeds in the presence of interrupts |
334 |
+ |
*/ |
335 |
+ |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
336 |
+ |
final SynchronousQueue<FibAction[]> sq = |
337 |
+ |
new SynchronousQueue<FibAction[]>(); |
338 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
339 |
+ |
protected void realCompute() throws InterruptedException { |
340 |
+ |
FibAction[] fibActions = new FibAction[6]; |
341 |
+ |
for (int i = 0; i < fibActions.length; i++) |
342 |
+ |
fibActions[i] = new FibAction(8); |
343 |
+ |
|
344 |
+ |
fibActions[1].cancel(false); |
345 |
+ |
fibActions[2].completeExceptionally(new FJException()); |
346 |
+ |
fibActions[4].cancel(true); |
347 |
+ |
fibActions[5].completeExceptionally(new FJException()); |
348 |
+ |
|
349 |
+ |
for (int i = 0; i < fibActions.length; i++) |
350 |
+ |
fibActions[i].fork(); |
351 |
+ |
|
352 |
+ |
sq.put(fibActions); |
353 |
+ |
|
354 |
+ |
helpQuiesce(); |
355 |
+ |
}}; |
356 |
+ |
|
357 |
+ |
Runnable r = new CheckedRunnable() { |
358 |
+ |
public void realRun() throws InterruptedException { |
359 |
+ |
FibAction[] fibActions = sq.take(); |
360 |
+ |
FibAction f; |
361 |
+ |
final Thread myself = Thread.currentThread(); |
362 |
+ |
|
363 |
+ |
// test join() ------------ |
364 |
+ |
|
365 |
+ |
f = fibActions[0]; |
366 |
+ |
assertFalse(ForkJoinTask.inForkJoinPool()); |
367 |
+ |
myself.interrupt(); |
368 |
+ |
assertTrue(myself.isInterrupted()); |
369 |
+ |
assertNull(f.join()); |
370 |
+ |
assertTrue(Thread.interrupted()); |
371 |
+ |
assertEquals(21, f.result); |
372 |
+ |
checkCompletedNormally(f); |
373 |
+ |
|
374 |
+ |
f = fibActions[1]; |
375 |
+ |
myself.interrupt(); |
376 |
+ |
assertTrue(myself.isInterrupted()); |
377 |
+ |
try { |
378 |
+ |
f.join(); |
379 |
+ |
shouldThrow(); |
380 |
+ |
} catch (CancellationException success) { |
381 |
+ |
assertTrue(Thread.interrupted()); |
382 |
+ |
checkCancelled(f); |
383 |
+ |
} |
384 |
+ |
|
385 |
+ |
f = fibActions[2]; |
386 |
+ |
myself.interrupt(); |
387 |
+ |
assertTrue(myself.isInterrupted()); |
388 |
+ |
try { |
389 |
+ |
f.join(); |
390 |
+ |
shouldThrow(); |
391 |
+ |
} catch (FJException success) { |
392 |
+ |
assertTrue(Thread.interrupted()); |
393 |
+ |
checkCompletedAbnormally(f, success); |
394 |
+ |
} |
395 |
+ |
|
396 |
+ |
// test quietlyJoin() --------- |
397 |
+ |
|
398 |
+ |
f = fibActions[3]; |
399 |
+ |
myself.interrupt(); |
400 |
+ |
assertTrue(myself.isInterrupted()); |
401 |
+ |
f.quietlyJoin(); |
402 |
+ |
assertTrue(Thread.interrupted()); |
403 |
+ |
assertEquals(21, f.result); |
404 |
+ |
checkCompletedNormally(f); |
405 |
+ |
|
406 |
+ |
f = fibActions[4]; |
407 |
+ |
myself.interrupt(); |
408 |
+ |
assertTrue(myself.isInterrupted()); |
409 |
+ |
f.quietlyJoin(); |
410 |
+ |
assertTrue(Thread.interrupted()); |
411 |
+ |
checkCancelled(f); |
412 |
+ |
|
413 |
+ |
f = fibActions[5]; |
414 |
+ |
myself.interrupt(); |
415 |
+ |
assertTrue(myself.isInterrupted()); |
416 |
+ |
f.quietlyJoin(); |
417 |
+ |
assertTrue(Thread.interrupted()); |
418 |
+ |
assertTrue(f.getException() instanceof FJException); |
419 |
+ |
checkCompletedAbnormally(f, f.getException()); |
420 |
+ |
}}; |
421 |
+ |
|
422 |
+ |
Thread t; |
423 |
+ |
|
424 |
+ |
t = newStartedThread(r); |
425 |
+ |
testInvokeOnPool(mainPool(), a); |
426 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
427 |
+ |
|
428 |
+ |
a.reinitialize(); |
429 |
+ |
t = newStartedThread(r); |
430 |
+ |
testInvokeOnPool(singletonPool(), a); |
431 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
432 |
+ |
} |
433 |
+ |
|
434 |
+ |
/** |
435 |
|
* get of a forked task returns when task completes |
436 |
|
*/ |
437 |
|
public void testForkGet() { |
438 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
439 |
< |
public void realCompute() throws Exception { |
439 |
> |
protected void realCompute() throws Exception { |
440 |
|
FibAction f = new FibAction(8); |
441 |
|
assertSame(f, f.fork()); |
442 |
|
assertNull(f.get()); |
451 |
|
*/ |
452 |
|
public void testForkTimedGet() { |
453 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
454 |
< |
public void realCompute() throws Exception { |
454 |
> |
protected void realCompute() throws Exception { |
455 |
|
FibAction f = new FibAction(8); |
456 |
|
assertSame(f, f.fork()); |
457 |
|
assertNull(f.get(5L, SECONDS)); |
466 |
|
*/ |
467 |
|
public void testForkTimedGetNPE() { |
468 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
469 |
< |
public void realCompute() throws Exception { |
469 |
> |
protected void realCompute() throws Exception { |
470 |
|
FibAction f = new FibAction(8); |
471 |
|
assertSame(f, f.fork()); |
472 |
|
try { |
482 |
|
*/ |
483 |
|
public void testForkQuietlyJoin() { |
484 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
485 |
< |
public void realCompute() { |
485 |
> |
protected void realCompute() { |
486 |
|
FibAction f = new FibAction(8); |
487 |
|
assertSame(f, f.fork()); |
488 |
|
f.quietlyJoin(); |
492 |
|
testInvokeOnPool(mainPool(), a); |
493 |
|
} |
494 |
|
|
380 |
– |
|
495 |
|
/** |
496 |
|
* helpQuiesce returns when tasks are complete. |
497 |
|
* getQueuedTaskCount returns 0 when quiescent |
498 |
|
*/ |
499 |
|
public void testForkHelpQuiesce() { |
500 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
501 |
< |
public void realCompute() { |
501 |
> |
protected void realCompute() { |
502 |
|
FibAction f = new FibAction(8); |
503 |
|
assertSame(f, f.fork()); |
504 |
< |
f.helpQuiesce(); |
504 |
> |
helpQuiesce(); |
505 |
|
assertEquals(21, f.result); |
506 |
|
assertEquals(0, getQueuedTaskCount()); |
507 |
|
checkCompletedNormally(f); |
509 |
|
testInvokeOnPool(mainPool(), a); |
510 |
|
} |
511 |
|
|
398 |
– |
|
512 |
|
/** |
513 |
|
* invoke task throws exception when task completes abnormally |
514 |
|
*/ |
515 |
|
public void testAbnormalInvoke() { |
516 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
517 |
< |
public void realCompute() { |
517 |
> |
protected void realCompute() { |
518 |
|
FailingFibAction f = new FailingFibAction(8); |
519 |
|
try { |
520 |
|
f.invoke(); |
531 |
|
*/ |
532 |
|
public void testAbnormalQuietlyInvoke() { |
533 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
534 |
< |
public void realCompute() { |
534 |
> |
protected void realCompute() { |
535 |
|
FailingFibAction f = new FailingFibAction(8); |
536 |
|
f.quietlyInvoke(); |
537 |
|
assertTrue(f.getException() instanceof FJException); |
545 |
|
*/ |
546 |
|
public void testAbnormalForkJoin() { |
547 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
548 |
< |
public void realCompute() { |
548 |
> |
protected void realCompute() { |
549 |
|
FailingFibAction f = new FailingFibAction(8); |
550 |
|
assertSame(f, f.fork()); |
551 |
|
try { |
563 |
|
*/ |
564 |
|
public void testAbnormalForkGet() { |
565 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
566 |
< |
public void realCompute() throws Exception { |
566 |
> |
protected void realCompute() throws Exception { |
567 |
|
FailingFibAction f = new FailingFibAction(8); |
568 |
|
assertSame(f, f.fork()); |
569 |
|
try { |
583 |
|
*/ |
584 |
|
public void testAbnormalForkTimedGet() { |
585 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
586 |
< |
public void realCompute() throws Exception { |
586 |
> |
protected void realCompute() throws Exception { |
587 |
|
FailingFibAction f = new FailingFibAction(8); |
588 |
|
assertSame(f, f.fork()); |
589 |
|
try { |
603 |
|
*/ |
604 |
|
public void testAbnormalForkQuietlyJoin() { |
605 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
606 |
< |
public void realCompute() { |
606 |
> |
protected void realCompute() { |
607 |
|
FailingFibAction f = new FailingFibAction(8); |
608 |
|
assertSame(f, f.fork()); |
609 |
|
f.quietlyJoin(); |
618 |
|
*/ |
619 |
|
public void testCancelledInvoke() { |
620 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
621 |
< |
public void realCompute() { |
621 |
> |
protected void realCompute() { |
622 |
|
FibAction f = new FibAction(8); |
623 |
|
assertTrue(f.cancel(true)); |
624 |
|
try { |
636 |
|
*/ |
637 |
|
public void testCancelledForkJoin() { |
638 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
639 |
< |
public void realCompute() { |
639 |
> |
protected void realCompute() { |
640 |
|
FibAction f = new FibAction(8); |
641 |
|
assertTrue(f.cancel(true)); |
642 |
|
assertSame(f, f.fork()); |
655 |
|
*/ |
656 |
|
public void testCancelledForkGet() { |
657 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
658 |
< |
public void realCompute() throws Exception { |
658 |
> |
protected void realCompute() throws Exception { |
659 |
|
FibAction f = new FibAction(8); |
660 |
|
assertTrue(f.cancel(true)); |
661 |
|
assertSame(f, f.fork()); |
674 |
|
*/ |
675 |
|
public void testCancelledForkTimedGet() { |
676 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
677 |
< |
public void realCompute() throws Exception { |
677 |
> |
protected void realCompute() throws Exception { |
678 |
|
FibAction f = new FibAction(8); |
679 |
|
assertTrue(f.cancel(true)); |
680 |
|
assertSame(f, f.fork()); |
693 |
|
*/ |
694 |
|
public void testCancelledForkQuietlyJoin() { |
695 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
696 |
< |
public void realCompute() { |
696 |
> |
protected void realCompute() { |
697 |
|
FibAction f = new FibAction(8); |
698 |
|
assertTrue(f.cancel(true)); |
699 |
|
assertSame(f, f.fork()); |
709 |
|
public void testGetPool() { |
710 |
|
final ForkJoinPool mainPool = mainPool(); |
711 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
712 |
< |
public void realCompute() { |
712 |
> |
protected void realCompute() { |
713 |
|
assertSame(mainPool, getPool()); |
714 |
|
}}; |
715 |
|
testInvokeOnPool(mainPool, a); |
720 |
|
*/ |
721 |
|
public void testGetPool2() { |
722 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
723 |
< |
public void realCompute() { |
723 |
> |
protected void realCompute() { |
724 |
|
assertNull(getPool()); |
725 |
|
}}; |
726 |
|
assertNull(a.invoke()); |
731 |
|
*/ |
732 |
|
public void testInForkJoinPool() { |
733 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
734 |
< |
public void realCompute() { |
734 |
> |
protected void realCompute() { |
735 |
|
assertTrue(inForkJoinPool()); |
736 |
|
}}; |
737 |
|
testInvokeOnPool(mainPool(), a); |
742 |
|
*/ |
743 |
|
public void testInForkJoinPool2() { |
744 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
745 |
< |
public void realCompute() { |
745 |
> |
protected void realCompute() { |
746 |
|
assertFalse(inForkJoinPool()); |
747 |
|
}}; |
748 |
|
assertNull(a.invoke()); |
754 |
|
public void testWorkerGetPool() { |
755 |
|
final ForkJoinPool mainPool = mainPool(); |
756 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
757 |
< |
public void realCompute() { |
757 |
> |
protected void realCompute() { |
758 |
|
ForkJoinWorkerThread w = |
759 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
760 |
|
assertSame(mainPool, w.getPool()); |
768 |
|
public void testWorkerGetPoolIndex() { |
769 |
|
final ForkJoinPool mainPool = mainPool(); |
770 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
771 |
< |
public void realCompute() { |
771 |
> |
protected void realCompute() { |
772 |
|
ForkJoinWorkerThread w = |
773 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
773 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
774 |
|
assertTrue(w.getPoolIndex() >= 0); |
775 |
< |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
775 |
> |
// pool size can shrink after assigning index, so cannot check |
776 |
> |
// assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
777 |
|
}}; |
778 |
|
testInvokeOnPool(mainPool, a); |
779 |
|
} |
780 |
|
|
667 |
– |
|
781 |
|
/** |
782 |
|
* setRawResult(null) succeeds |
783 |
|
*/ |
784 |
|
public void testSetRawResult() { |
785 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
786 |
< |
public void realCompute() { |
786 |
> |
protected void realCompute() { |
787 |
|
setRawResult(null); |
788 |
|
assertNull(getRawResult()); |
789 |
|
}}; |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
< |
* A reinitialized task may be re-invoked |
794 |
> |
* A reinitialized normally completed task may be re-invoked |
795 |
|
*/ |
796 |
|
public void testReinitialize() { |
797 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
798 |
< |
public void realCompute() { |
798 |
> |
protected void realCompute() { |
799 |
|
FibAction f = new FibAction(8); |
800 |
|
checkNotDone(f); |
801 |
|
|
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
+ |
* A reinitialized abnormally completed task may be re-invoked |
815 |
+ |
*/ |
816 |
+ |
public void testReinitializeAbnormal() { |
817 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
818 |
+ |
protected void realCompute() { |
819 |
+ |
FailingFibAction f = new FailingFibAction(8); |
820 |
+ |
checkNotDone(f); |
821 |
+ |
|
822 |
+ |
for (int i = 0; i < 3; i++) { |
823 |
+ |
try { |
824 |
+ |
f.invoke(); |
825 |
+ |
shouldThrow(); |
826 |
+ |
} catch (FJException success) { |
827 |
+ |
checkCompletedAbnormally(f, success); |
828 |
+ |
} |
829 |
+ |
f.reinitialize(); |
830 |
+ |
checkNotDone(f); |
831 |
+ |
} |
832 |
+ |
}}; |
833 |
+ |
testInvokeOnPool(mainPool(), a); |
834 |
+ |
} |
835 |
+ |
|
836 |
+ |
/** |
837 |
|
* invoke task throws exception after invoking completeExceptionally |
838 |
|
*/ |
839 |
|
public void testCompleteExceptionally() { |
840 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
841 |
< |
public void realCompute() { |
841 |
> |
protected void realCompute() { |
842 |
|
FibAction f = new FibAction(8); |
843 |
|
f.completeExceptionally(new FJException()); |
844 |
|
try { |
856 |
|
*/ |
857 |
|
public void testComplete() { |
858 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
859 |
< |
public void realCompute() { |
859 |
> |
protected void realCompute() { |
860 |
|
FibAction f = new FibAction(8); |
861 |
|
f.complete(null); |
862 |
|
assertNull(f.invoke()); |
871 |
|
*/ |
872 |
|
public void testInvokeAll2() { |
873 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
874 |
< |
public void realCompute() { |
874 |
> |
protected void realCompute() { |
875 |
|
FibAction f = new FibAction(8); |
876 |
|
FibAction g = new FibAction(9); |
877 |
|
invokeAll(f, g); |
888 |
|
*/ |
889 |
|
public void testInvokeAll1() { |
890 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
891 |
< |
public void realCompute() { |
891 |
> |
protected void realCompute() { |
892 |
|
FibAction f = new FibAction(8); |
893 |
|
invokeAll(f); |
894 |
|
checkCompletedNormally(f); |
902 |
|
*/ |
903 |
|
public void testInvokeAll3() { |
904 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
905 |
< |
public void realCompute() { |
905 |
> |
protected void realCompute() { |
906 |
|
FibAction f = new FibAction(8); |
907 |
|
FibAction g = new FibAction(9); |
908 |
|
FibAction h = new FibAction(7); |
925 |
|
*/ |
926 |
|
public void testInvokeAllCollection() { |
927 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
928 |
< |
public void realCompute() { |
928 |
> |
protected void realCompute() { |
929 |
|
FibAction f = new FibAction(8); |
930 |
|
FibAction g = new FibAction(9); |
931 |
|
FibAction h = new FibAction(7); |
947 |
|
testInvokeOnPool(mainPool(), a); |
948 |
|
} |
949 |
|
|
814 |
– |
|
950 |
|
/** |
951 |
|
* invokeAll(tasks) with any null task throws NPE |
952 |
|
*/ |
953 |
|
public void testInvokeAllNPE() { |
954 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
955 |
< |
public void realCompute() { |
955 |
> |
protected void realCompute() { |
956 |
|
FibAction f = new FibAction(8); |
957 |
|
FibAction g = new FibAction(9); |
958 |
|
FibAction h = null; |
969 |
|
*/ |
970 |
|
public void testAbnormalInvokeAll2() { |
971 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
972 |
< |
public void realCompute() { |
972 |
> |
protected void realCompute() { |
973 |
|
FibAction f = new FibAction(8); |
974 |
|
FailingFibAction g = new FailingFibAction(9); |
975 |
|
try { |
987 |
|
*/ |
988 |
|
public void testAbnormalInvokeAll1() { |
989 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
990 |
< |
public void realCompute() { |
990 |
> |
protected void realCompute() { |
991 |
|
FailingFibAction g = new FailingFibAction(9); |
992 |
|
try { |
993 |
|
invokeAll(g); |
1004 |
|
*/ |
1005 |
|
public void testAbnormalInvokeAll3() { |
1006 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1007 |
< |
public void realCompute() { |
1007 |
> |
protected void realCompute() { |
1008 |
|
FibAction f = new FibAction(8); |
1009 |
|
FailingFibAction g = new FailingFibAction(9); |
1010 |
|
FibAction h = new FibAction(7); |
1023 |
|
*/ |
1024 |
|
public void testAbnormalInvokeAllCollection() { |
1025 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1026 |
< |
public void realCompute() { |
1026 |
> |
protected void realCompute() { |
1027 |
|
FailingFibAction f = new FailingFibAction(8); |
1028 |
|
FibAction g = new FibAction(9); |
1029 |
|
FibAction h = new FibAction(7); |
1047 |
|
*/ |
1048 |
|
public void testTryUnfork() { |
1049 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1050 |
< |
public void realCompute() { |
1050 |
> |
protected void realCompute() { |
1051 |
|
FibAction g = new FibAction(9); |
1052 |
|
assertSame(g, g.fork()); |
1053 |
|
FibAction f = new FibAction(8); |
1066 |
|
*/ |
1067 |
|
public void testGetSurplusQueuedTaskCount() { |
1068 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1069 |
< |
public void realCompute() { |
1069 |
> |
protected void realCompute() { |
1070 |
|
FibAction h = new FibAction(7); |
1071 |
|
assertSame(h, h.fork()); |
1072 |
|
FibAction g = new FibAction(9); |
1088 |
|
*/ |
1089 |
|
public void testPeekNextLocalTask() { |
1090 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1091 |
< |
public void realCompute() { |
1091 |
> |
protected void realCompute() { |
1092 |
|
FibAction g = new FibAction(9); |
1093 |
|
assertSame(g, g.fork()); |
1094 |
|
FibAction f = new FibAction(8); |
1109 |
|
*/ |
1110 |
|
public void testPollNextLocalTask() { |
1111 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1112 |
< |
public void realCompute() { |
1112 |
> |
protected void realCompute() { |
1113 |
|
FibAction g = new FibAction(9); |
1114 |
|
assertSame(g, g.fork()); |
1115 |
|
FibAction f = new FibAction(8); |
1127 |
|
*/ |
1128 |
|
public void testPollTask() { |
1129 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1130 |
< |
public void realCompute() { |
1130 |
> |
protected void realCompute() { |
1131 |
|
FibAction g = new FibAction(9); |
1132 |
|
assertSame(g, g.fork()); |
1133 |
|
FibAction f = new FibAction(8); |
1145 |
|
*/ |
1146 |
|
public void testPeekNextLocalTaskAsync() { |
1147 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1148 |
< |
public void realCompute() { |
1148 |
> |
protected void realCompute() { |
1149 |
|
FibAction g = new FibAction(9); |
1150 |
|
assertSame(g, g.fork()); |
1151 |
|
FibAction f = new FibAction(8); |
1165 |
|
*/ |
1166 |
|
public void testPollNextLocalTaskAsync() { |
1167 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1168 |
< |
public void realCompute() { |
1168 |
> |
protected void realCompute() { |
1169 |
|
FibAction g = new FibAction(9); |
1170 |
|
assertSame(g, g.fork()); |
1171 |
|
FibAction f = new FibAction(8); |
1184 |
|
*/ |
1185 |
|
public void testPollTaskAsync() { |
1186 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1187 |
< |
public void realCompute() { |
1187 |
> |
protected void realCompute() { |
1188 |
|
FibAction g = new FibAction(9); |
1189 |
|
assertSame(g, g.fork()); |
1190 |
|
FibAction f = new FibAction(8); |
1197 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1198 |
|
} |
1199 |
|
|
1200 |
+ |
/** Demo from RecursiveAction javadoc */ |
1201 |
+ |
static class SortTask extends RecursiveAction { |
1202 |
+ |
final long[] array; final int lo, hi; |
1203 |
+ |
SortTask(long[] array, int lo, int hi) { |
1204 |
+ |
this.array = array; this.lo = lo; this.hi = hi; |
1205 |
+ |
} |
1206 |
+ |
SortTask(long[] array) { this(array, 0, array.length); } |
1207 |
+ |
protected void compute() { |
1208 |
+ |
if (hi - lo < THRESHOLD) |
1209 |
+ |
sortSequentially(lo, hi); |
1210 |
+ |
else { |
1211 |
+ |
int mid = (lo + hi) >>> 1; |
1212 |
+ |
invokeAll(new SortTask(array, lo, mid), |
1213 |
+ |
new SortTask(array, mid, hi)); |
1214 |
+ |
merge(lo, mid, hi); |
1215 |
+ |
} |
1216 |
+ |
} |
1217 |
+ |
// implementation details follow: |
1218 |
+ |
static final int THRESHOLD = 100; |
1219 |
+ |
void sortSequentially(int lo, int hi) { |
1220 |
+ |
Arrays.sort(array, lo, hi); |
1221 |
+ |
} |
1222 |
+ |
void merge(int lo, int mid, int hi) { |
1223 |
+ |
long[] buf = Arrays.copyOfRange(array, lo, mid); |
1224 |
+ |
for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
1225 |
+ |
array[j] = (k == hi || buf[i] < array[k]) ? |
1226 |
+ |
buf[i++] : array[k++]; |
1227 |
+ |
} |
1228 |
+ |
} |
1229 |
+ |
|
1230 |
+ |
/** |
1231 |
+ |
* SortTask demo works as advertised |
1232 |
+ |
*/ |
1233 |
+ |
public void testSortTaskDemo() { |
1234 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1235 |
+ |
long[] array = new long[1007]; |
1236 |
+ |
for (int i = 0; i < array.length; i++) |
1237 |
+ |
array[i] = rnd.nextLong(); |
1238 |
+ |
long[] arrayClone = array.clone(); |
1239 |
+ |
testInvokeOnPool(mainPool(), new SortTask(array)); |
1240 |
+ |
Arrays.sort(arrayClone); |
1241 |
+ |
assertTrue(Arrays.equals(array, arrayClone)); |
1242 |
+ |
} |
1243 |
|
} |