4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
> |
|
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
+ |
import java.util.concurrent.CountedCompleter; |
13 |
|
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
12 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
16 |
|
import java.util.concurrent.RecursiveAction; |
14 |
– |
import java.util.concurrent.CountedCompleter; |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import java.util.Arrays; |
21 |
< |
import java.util.HashSet; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class ForkJoinPool8Test extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
|
27 |
|
public static Test suite() { |
58 |
|
* RecursiveAction and CountedCompleter, but with all actions |
59 |
|
* executed in the common pool, generally implicitly via |
60 |
|
* checkInvoke. |
61 |
< |
*/ |
61 |
> |
*/ |
62 |
|
|
63 |
|
private void checkInvoke(ForkJoinTask a) { |
64 |
|
checkNotDone(a); |
184 |
|
final int number; |
185 |
|
int result; |
186 |
|
FibAction(int n) { number = n; } |
187 |
< |
public void realCompute() { |
187 |
> |
protected void realCompute() { |
188 |
|
int n = number; |
189 |
|
if (n <= 1) |
190 |
|
result = n; |
222 |
|
*/ |
223 |
|
public void testInvoke() { |
224 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
225 |
< |
public void realCompute() { |
225 |
> |
protected void realCompute() { |
226 |
|
FibAction f = new FibAction(8); |
227 |
|
assertNull(f.invoke()); |
228 |
|
assertEquals(21, f.result); |
238 |
|
*/ |
239 |
|
public void testQuietlyInvoke() { |
240 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
241 |
< |
public void realCompute() { |
241 |
> |
protected void realCompute() { |
242 |
|
FibAction f = new FibAction(8); |
243 |
|
f.quietlyInvoke(); |
244 |
|
assertEquals(21, f.result); |
252 |
|
*/ |
253 |
|
public void testForkJoin() { |
254 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
255 |
< |
public void realCompute() { |
255 |
> |
protected void realCompute() { |
256 |
|
FibAction f = new FibAction(8); |
257 |
|
assertSame(f, f.fork()); |
258 |
|
assertNull(f.join()); |
267 |
|
*/ |
268 |
|
public void testJoinIgnoresInterrupts() { |
269 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
270 |
< |
public void realCompute() { |
270 |
> |
protected void realCompute() { |
271 |
|
FibAction f = new FibAction(8); |
272 |
< |
final Thread myself = Thread.currentThread(); |
272 |
> |
final Thread currentThread = Thread.currentThread(); |
273 |
|
|
274 |
|
// test join() |
275 |
|
assertSame(f, f.fork()); |
276 |
< |
myself.interrupt(); |
278 |
< |
assertTrue(myself.isInterrupted()); |
276 |
> |
currentThread.interrupt(); |
277 |
|
assertNull(f.join()); |
278 |
|
Thread.interrupted(); |
279 |
|
assertEquals(21, f.result); |
282 |
|
f = new FibAction(8); |
283 |
|
f.cancel(true); |
284 |
|
assertSame(f, f.fork()); |
285 |
< |
myself.interrupt(); |
288 |
< |
assertTrue(myself.isInterrupted()); |
285 |
> |
currentThread.interrupt(); |
286 |
|
try { |
287 |
|
f.join(); |
288 |
|
shouldThrow(); |
294 |
|
f = new FibAction(8); |
295 |
|
f.completeExceptionally(new FJException()); |
296 |
|
assertSame(f, f.fork()); |
297 |
< |
myself.interrupt(); |
301 |
< |
assertTrue(myself.isInterrupted()); |
297 |
> |
currentThread.interrupt(); |
298 |
|
try { |
299 |
|
f.join(); |
300 |
|
shouldThrow(); |
306 |
|
// test quietlyJoin() |
307 |
|
f = new FibAction(8); |
308 |
|
assertSame(f, f.fork()); |
309 |
< |
myself.interrupt(); |
314 |
< |
assertTrue(myself.isInterrupted()); |
309 |
> |
currentThread.interrupt(); |
310 |
|
f.quietlyJoin(); |
311 |
|
Thread.interrupted(); |
312 |
|
assertEquals(21, f.result); |
315 |
|
f = new FibAction(8); |
316 |
|
f.cancel(true); |
317 |
|
assertSame(f, f.fork()); |
318 |
< |
myself.interrupt(); |
324 |
< |
assertTrue(myself.isInterrupted()); |
318 |
> |
currentThread.interrupt(); |
319 |
|
f.quietlyJoin(); |
320 |
|
Thread.interrupted(); |
321 |
|
checkCancelled(f); |
323 |
|
f = new FibAction(8); |
324 |
|
f.completeExceptionally(new FJException()); |
325 |
|
assertSame(f, f.fork()); |
326 |
< |
myself.interrupt(); |
333 |
< |
assertTrue(myself.isInterrupted()); |
326 |
> |
currentThread.interrupt(); |
327 |
|
f.quietlyJoin(); |
328 |
|
Thread.interrupted(); |
329 |
|
checkCompletedAbnormally(f, f.getException()); |
333 |
|
checkInvoke(a); |
334 |
|
} |
335 |
|
|
343 |
– |
|
336 |
|
/** |
337 |
|
* get of a forked task returns when task completes |
338 |
|
*/ |
339 |
|
public void testForkGet() { |
340 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
341 |
< |
public void realCompute() throws Exception { |
341 |
> |
protected void realCompute() throws Exception { |
342 |
|
FibAction f = new FibAction(8); |
343 |
|
assertSame(f, f.fork()); |
344 |
|
assertNull(f.get()); |
353 |
|
*/ |
354 |
|
public void testForkTimedGet() { |
355 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
356 |
< |
public void realCompute() throws Exception { |
356 |
> |
protected void realCompute() throws Exception { |
357 |
|
FibAction f = new FibAction(8); |
358 |
|
assertSame(f, f.fork()); |
359 |
|
assertNull(f.get(5L, SECONDS)); |
368 |
|
*/ |
369 |
|
public void testForkTimedGetNPE() { |
370 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
371 |
< |
public void realCompute() throws Exception { |
371 |
> |
protected void realCompute() throws Exception { |
372 |
|
FibAction f = new FibAction(8); |
373 |
|
assertSame(f, f.fork()); |
374 |
|
try { |
384 |
|
*/ |
385 |
|
public void testForkQuietlyJoin() { |
386 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
387 |
< |
public void realCompute() { |
387 |
> |
protected void realCompute() { |
388 |
|
FibAction f = new FibAction(8); |
389 |
|
assertSame(f, f.fork()); |
390 |
|
f.quietlyJoin(); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
406 |
– |
* helpQuiesce returns when tasks are complete. |
407 |
– |
* getQueuedTaskCount returns 0 when quiescent |
408 |
– |
*/ |
409 |
– |
public void testForkHelpQuiesce() { |
410 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
411 |
– |
public void realCompute() { |
412 |
– |
FibAction f = new FibAction(8); |
413 |
– |
assertSame(f, f.fork()); |
414 |
– |
helpQuiesce(); |
415 |
– |
assertEquals(21, f.result); |
416 |
– |
assertEquals(0, getQueuedTaskCount()); |
417 |
– |
checkCompletedNormally(f); |
418 |
– |
}}; |
419 |
– |
checkInvoke(a); |
420 |
– |
} |
421 |
– |
|
422 |
– |
/** |
398 |
|
* invoke task throws exception when task completes abnormally |
399 |
|
*/ |
400 |
|
public void testAbnormalInvoke() { |
401 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
402 |
< |
public void realCompute() { |
402 |
> |
protected void realCompute() { |
403 |
|
FailingFibAction f = new FailingFibAction(8); |
404 |
|
try { |
405 |
|
f.invoke(); |
416 |
|
*/ |
417 |
|
public void testAbnormalQuietlyInvoke() { |
418 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
419 |
< |
public void realCompute() { |
419 |
> |
protected void realCompute() { |
420 |
|
FailingFibAction f = new FailingFibAction(8); |
421 |
|
f.quietlyInvoke(); |
422 |
|
assertTrue(f.getException() instanceof FJException); |
430 |
|
*/ |
431 |
|
public void testAbnormalForkJoin() { |
432 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
433 |
< |
public void realCompute() { |
433 |
> |
protected void realCompute() { |
434 |
|
FailingFibAction f = new FailingFibAction(8); |
435 |
|
assertSame(f, f.fork()); |
436 |
|
try { |
448 |
|
*/ |
449 |
|
public void testAbnormalForkGet() { |
450 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
451 |
< |
public void realCompute() throws Exception { |
451 |
> |
protected void realCompute() throws Exception { |
452 |
|
FailingFibAction f = new FailingFibAction(8); |
453 |
|
assertSame(f, f.fork()); |
454 |
|
try { |
468 |
|
*/ |
469 |
|
public void testAbnormalForkTimedGet() { |
470 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
471 |
< |
public void realCompute() throws Exception { |
471 |
> |
protected void realCompute() throws Exception { |
472 |
|
FailingFibAction f = new FailingFibAction(8); |
473 |
|
assertSame(f, f.fork()); |
474 |
|
try { |
475 |
< |
f.get(5L, TimeUnit.SECONDS); |
475 |
> |
f.get(5L, SECONDS); |
476 |
|
shouldThrow(); |
477 |
|
} catch (ExecutionException success) { |
478 |
|
Throwable cause = success.getCause(); |
488 |
|
*/ |
489 |
|
public void testAbnormalForkQuietlyJoin() { |
490 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
491 |
< |
public void realCompute() { |
491 |
> |
protected void realCompute() { |
492 |
|
FailingFibAction f = new FailingFibAction(8); |
493 |
|
assertSame(f, f.fork()); |
494 |
|
f.quietlyJoin(); |
503 |
|
*/ |
504 |
|
public void testCancelledInvoke() { |
505 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
506 |
< |
public void realCompute() { |
506 |
> |
protected void realCompute() { |
507 |
|
FibAction f = new FibAction(8); |
508 |
|
assertTrue(f.cancel(true)); |
509 |
|
try { |
521 |
|
*/ |
522 |
|
public void testCancelledForkJoin() { |
523 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
524 |
< |
public void realCompute() { |
524 |
> |
protected void realCompute() { |
525 |
|
FibAction f = new FibAction(8); |
526 |
|
assertTrue(f.cancel(true)); |
527 |
|
assertSame(f, f.fork()); |
540 |
|
*/ |
541 |
|
public void testCancelledForkGet() { |
542 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
543 |
< |
public void realCompute() throws Exception { |
543 |
> |
protected void realCompute() throws Exception { |
544 |
|
FibAction f = new FibAction(8); |
545 |
|
assertTrue(f.cancel(true)); |
546 |
|
assertSame(f, f.fork()); |
559 |
|
*/ |
560 |
|
public void testCancelledForkTimedGet() { |
561 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
562 |
< |
public void realCompute() throws Exception { |
562 |
> |
protected void realCompute() throws Exception { |
563 |
|
FibAction f = new FibAction(8); |
564 |
|
assertTrue(f.cancel(true)); |
565 |
|
assertSame(f, f.fork()); |
578 |
|
*/ |
579 |
|
public void testCancelledForkQuietlyJoin() { |
580 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
581 |
< |
public void realCompute() { |
581 |
> |
protected void realCompute() { |
582 |
|
FibAction f = new FibAction(8); |
583 |
|
assertTrue(f.cancel(true)); |
584 |
|
assertSame(f, f.fork()); |
593 |
|
*/ |
594 |
|
public void testInForkJoinPool2() { |
595 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
596 |
< |
public void realCompute() { |
596 |
> |
protected void realCompute() { |
597 |
|
assertFalse(inForkJoinPool()); |
598 |
|
}}; |
599 |
|
assertNull(a.invoke()); |
604 |
|
*/ |
605 |
|
public void testReinitialize() { |
606 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
607 |
< |
public void realCompute() { |
607 |
> |
protected void realCompute() { |
608 |
|
FibAction f = new FibAction(8); |
609 |
|
checkNotDone(f); |
610 |
|
|
624 |
|
*/ |
625 |
|
public void testReinitializeAbnormal() { |
626 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
627 |
< |
public void realCompute() { |
627 |
> |
protected void realCompute() { |
628 |
|
FailingFibAction f = new FailingFibAction(8); |
629 |
|
checkNotDone(f); |
630 |
|
|
647 |
|
*/ |
648 |
|
public void testCompleteExceptionally() { |
649 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
650 |
< |
public void realCompute() { |
650 |
> |
protected void realCompute() { |
651 |
|
FibAction f = new FibAction(8); |
652 |
|
f.completeExceptionally(new FJException()); |
653 |
|
try { |
665 |
|
*/ |
666 |
|
public void testComplete() { |
667 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
668 |
< |
public void realCompute() { |
668 |
> |
protected void realCompute() { |
669 |
|
FibAction f = new FibAction(8); |
670 |
|
f.complete(null); |
671 |
|
assertNull(f.invoke()); |
680 |
|
*/ |
681 |
|
public void testInvokeAll2() { |
682 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
683 |
< |
public void realCompute() { |
683 |
> |
protected void realCompute() { |
684 |
|
FibAction f = new FibAction(8); |
685 |
|
FibAction g = new FibAction(9); |
686 |
|
invokeAll(f, g); |
697 |
|
*/ |
698 |
|
public void testInvokeAll1() { |
699 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
700 |
< |
public void realCompute() { |
700 |
> |
protected void realCompute() { |
701 |
|
FibAction f = new FibAction(8); |
702 |
|
invokeAll(f); |
703 |
|
checkCompletedNormally(f); |
711 |
|
*/ |
712 |
|
public void testInvokeAll3() { |
713 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
714 |
< |
public void realCompute() { |
714 |
> |
protected void realCompute() { |
715 |
|
FibAction f = new FibAction(8); |
716 |
|
FibAction g = new FibAction(9); |
717 |
|
FibAction h = new FibAction(7); |
734 |
|
*/ |
735 |
|
public void testInvokeAllCollection() { |
736 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
737 |
< |
public void realCompute() { |
737 |
> |
protected void realCompute() { |
738 |
|
FibAction f = new FibAction(8); |
739 |
|
FibAction g = new FibAction(9); |
740 |
|
FibAction h = new FibAction(7); |
761 |
|
*/ |
762 |
|
public void testInvokeAllNPE() { |
763 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
764 |
< |
public void realCompute() { |
764 |
> |
protected void realCompute() { |
765 |
|
FibAction f = new FibAction(8); |
766 |
|
FibAction g = new FibAction(9); |
767 |
|
FibAction h = null; |
778 |
|
*/ |
779 |
|
public void testAbnormalInvokeAll2() { |
780 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
781 |
< |
public void realCompute() { |
781 |
> |
protected void realCompute() { |
782 |
|
FibAction f = new FibAction(8); |
783 |
|
FailingFibAction g = new FailingFibAction(9); |
784 |
|
try { |
796 |
|
*/ |
797 |
|
public void testAbnormalInvokeAll1() { |
798 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
799 |
< |
public void realCompute() { |
799 |
> |
protected void realCompute() { |
800 |
|
FailingFibAction g = new FailingFibAction(9); |
801 |
|
try { |
802 |
|
invokeAll(g); |
813 |
|
*/ |
814 |
|
public void testAbnormalInvokeAll3() { |
815 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
816 |
< |
public void realCompute() { |
816 |
> |
protected void realCompute() { |
817 |
|
FibAction f = new FibAction(8); |
818 |
|
FailingFibAction g = new FailingFibAction(9); |
819 |
|
FibAction h = new FibAction(7); |
832 |
|
*/ |
833 |
|
public void testAbnormalInvokeAllCollection() { |
834 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
835 |
< |
public void realCompute() { |
835 |
> |
protected void realCompute() { |
836 |
|
FailingFibAction f = new FailingFibAction(8); |
837 |
|
FibAction g = new FibAction(9); |
838 |
|
FibAction h = new FibAction(7); |
852 |
|
|
853 |
|
// CountedCompleter versions |
854 |
|
|
855 |
< |
public abstract class CheckedFJTask extends RecursiveAction { |
881 |
< |
protected abstract void realCompute() throws Throwable; |
882 |
< |
|
883 |
< |
public final void compute() { |
884 |
< |
try { |
885 |
< |
realCompute(); |
886 |
< |
} catch (Throwable t) { |
887 |
< |
threadUnexpectedException(t); |
888 |
< |
} |
889 |
< |
} |
890 |
< |
} |
891 |
< |
|
892 |
< |
static abstract class CCF extends CountedCompleter { |
855 |
> |
abstract static class CCF extends CountedCompleter { |
856 |
|
int number; |
857 |
|
int rnumber; |
858 |
|
|
873 |
|
f.onCompletion(f); |
874 |
|
if ((p = f.getCompleter()) != null) |
875 |
|
p.tryComplete(); |
876 |
< |
else |
877 |
< |
f.quietlyComplete(); |
876 |
> |
else |
877 |
> |
f.quietlyComplete(); |
878 |
|
} |
879 |
|
} |
880 |
|
|
906 |
|
} |
907 |
|
|
908 |
|
// Version of CCF with forced failure in left completions |
909 |
< |
static abstract class FailingCCF extends CountedCompleter { |
909 |
> |
abstract static class FailingCCF extends CountedCompleter { |
910 |
|
int number; |
911 |
|
int rnumber; |
912 |
|
|
927 |
|
f.onCompletion(f); |
928 |
|
if ((p = f.getCompleter()) != null) |
929 |
|
p.tryComplete(); |
930 |
< |
else |
931 |
< |
f.quietlyComplete(); |
930 |
> |
else |
931 |
> |
f.quietlyComplete(); |
932 |
|
} |
933 |
|
} |
934 |
|
|
953 |
|
completeExceptionally(new FJException()); |
954 |
|
} |
955 |
|
} |
956 |
< |
|
956 |
> |
|
957 |
|
/** |
958 |
|
* invoke returns when task completes normally. |
959 |
|
* isCompletedAbnormally and isCancelled return false for normally |
960 |
|
* completed tasks; getRawResult returns null. |
961 |
|
*/ |
962 |
|
public void testInvokeCC() { |
963 |
< |
ForkJoinTask a = new CheckedFJTask() { |
964 |
< |
public void realCompute() { |
963 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
964 |
> |
protected void realCompute() { |
965 |
|
CCF f = new LCCF(null, 8); |
966 |
|
assertNull(f.invoke()); |
967 |
|
assertEquals(21, f.number); |
976 |
|
* completed tasks |
977 |
|
*/ |
978 |
|
public void testQuietlyInvokeCC() { |
979 |
< |
ForkJoinTask a = new CheckedFJTask() { |
980 |
< |
public void realCompute() { |
979 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
980 |
> |
protected void realCompute() { |
981 |
|
CCF f = new LCCF(null, 8); |
982 |
|
f.quietlyInvoke(); |
983 |
|
assertEquals(21, f.number); |
990 |
|
* join of a forked task returns when task completes |
991 |
|
*/ |
992 |
|
public void testForkJoinCC() { |
993 |
< |
ForkJoinTask a = new CheckedFJTask() { |
994 |
< |
public void realCompute() { |
993 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
994 |
> |
protected void realCompute() { |
995 |
|
CCF f = new LCCF(null, 8); |
996 |
|
assertSame(f, f.fork()); |
997 |
|
assertNull(f.join()); |
1005 |
|
* get of a forked task returns when task completes |
1006 |
|
*/ |
1007 |
|
public void testForkGetCC() { |
1008 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1009 |
< |
public void realCompute() throws Exception { |
1008 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1009 |
> |
protected void realCompute() throws Exception { |
1010 |
|
CCF f = new LCCF(null, 8); |
1011 |
|
assertSame(f, f.fork()); |
1012 |
|
assertNull(f.get()); |
1020 |
|
* timed get of a forked task returns when task completes |
1021 |
|
*/ |
1022 |
|
public void testForkTimedGetCC() { |
1023 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1024 |
< |
public void realCompute() throws Exception { |
1023 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1024 |
> |
protected void realCompute() throws Exception { |
1025 |
|
CCF f = new LCCF(null, 8); |
1026 |
|
assertSame(f, f.fork()); |
1027 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1035 |
|
* timed get with null time unit throws NPE |
1036 |
|
*/ |
1037 |
|
public void testForkTimedGetNPECC() { |
1038 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1039 |
< |
public void realCompute() throws Exception { |
1038 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1039 |
> |
protected void realCompute() throws Exception { |
1040 |
|
CCF f = new LCCF(null, 8); |
1041 |
|
assertSame(f, f.fork()); |
1042 |
|
try { |
1051 |
|
* quietlyJoin of a forked task returns when task completes |
1052 |
|
*/ |
1053 |
|
public void testForkQuietlyJoinCC() { |
1054 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1055 |
< |
public void realCompute() { |
1054 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1055 |
> |
protected void realCompute() { |
1056 |
|
CCF f = new LCCF(null, 8); |
1057 |
|
assertSame(f, f.fork()); |
1058 |
|
f.quietlyJoin(); |
1063 |
|
} |
1064 |
|
|
1065 |
|
/** |
1103 |
– |
* helpQuiesce returns when tasks are complete. |
1104 |
– |
* getQueuedTaskCount returns 0 when quiescent |
1105 |
– |
*/ |
1106 |
– |
public void testForkHelpQuiesceCC() { |
1107 |
– |
ForkJoinTask a = new CheckedFJTask() { |
1108 |
– |
public void realCompute() { |
1109 |
– |
CCF f = new LCCF(null, 8); |
1110 |
– |
assertSame(f, f.fork()); |
1111 |
– |
helpQuiesce(); |
1112 |
– |
assertEquals(21, f.number); |
1113 |
– |
assertEquals(0, getQueuedTaskCount()); |
1114 |
– |
checkCompletedNormally(f); |
1115 |
– |
}}; |
1116 |
– |
checkInvoke(a); |
1117 |
– |
} |
1118 |
– |
|
1119 |
– |
/** |
1066 |
|
* invoke task throws exception when task completes abnormally |
1067 |
|
*/ |
1068 |
|
public void testAbnormalInvokeCC() { |
1069 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1070 |
< |
public void realCompute() { |
1069 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1070 |
> |
protected void realCompute() { |
1071 |
|
FailingCCF f = new LFCCF(null, 8); |
1072 |
|
try { |
1073 |
|
f.invoke(); |
1083 |
|
* quietlyInvoke task returns when task completes abnormally |
1084 |
|
*/ |
1085 |
|
public void testAbnormalQuietlyInvokeCC() { |
1086 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1087 |
< |
public void realCompute() { |
1086 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1087 |
> |
protected void realCompute() { |
1088 |
|
FailingCCF f = new LFCCF(null, 8); |
1089 |
|
f.quietlyInvoke(); |
1090 |
|
assertTrue(f.getException() instanceof FJException); |
1097 |
|
* join of a forked task throws exception when task completes abnormally |
1098 |
|
*/ |
1099 |
|
public void testAbnormalForkJoinCC() { |
1100 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1101 |
< |
public void realCompute() { |
1100 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1101 |
> |
protected void realCompute() { |
1102 |
|
FailingCCF f = new LFCCF(null, 8); |
1103 |
|
assertSame(f, f.fork()); |
1104 |
|
try { |
1115 |
|
* get of a forked task throws exception when task completes abnormally |
1116 |
|
*/ |
1117 |
|
public void testAbnormalForkGetCC() { |
1118 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1119 |
< |
public void realCompute() throws Exception { |
1118 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1119 |
> |
protected void realCompute() throws Exception { |
1120 |
|
FailingCCF f = new LFCCF(null, 8); |
1121 |
|
assertSame(f, f.fork()); |
1122 |
|
try { |
1135 |
|
* timed get of a forked task throws exception when task completes abnormally |
1136 |
|
*/ |
1137 |
|
public void testAbnormalForkTimedGetCC() { |
1138 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1139 |
< |
public void realCompute() throws Exception { |
1138 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1139 |
> |
protected void realCompute() throws Exception { |
1140 |
|
FailingCCF f = new LFCCF(null, 8); |
1141 |
|
assertSame(f, f.fork()); |
1142 |
|
try { |
1155 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
1156 |
|
*/ |
1157 |
|
public void testAbnormalForkQuietlyJoinCC() { |
1158 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1159 |
< |
public void realCompute() { |
1158 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1159 |
> |
protected void realCompute() { |
1160 |
|
FailingCCF f = new LFCCF(null, 8); |
1161 |
|
assertSame(f, f.fork()); |
1162 |
|
f.quietlyJoin(); |
1170 |
|
* invoke task throws exception when task cancelled |
1171 |
|
*/ |
1172 |
|
public void testCancelledInvokeCC() { |
1173 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1174 |
< |
public void realCompute() { |
1173 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1174 |
> |
protected void realCompute() { |
1175 |
|
CCF f = new LCCF(null, 8); |
1176 |
|
assertTrue(f.cancel(true)); |
1177 |
|
try { |
1188 |
|
* join of a forked task throws exception when task cancelled |
1189 |
|
*/ |
1190 |
|
public void testCancelledForkJoinCC() { |
1191 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1192 |
< |
public void realCompute() { |
1191 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1192 |
> |
protected void realCompute() { |
1193 |
|
CCF f = new LCCF(null, 8); |
1194 |
|
assertTrue(f.cancel(true)); |
1195 |
|
assertSame(f, f.fork()); |
1207 |
|
* get of a forked task throws exception when task cancelled |
1208 |
|
*/ |
1209 |
|
public void testCancelledForkGetCC() { |
1210 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1211 |
< |
public void realCompute() throws Exception { |
1210 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1211 |
> |
protected void realCompute() throws Exception { |
1212 |
|
CCF f = new LCCF(null, 8); |
1213 |
|
assertTrue(f.cancel(true)); |
1214 |
|
assertSame(f, f.fork()); |
1226 |
|
* timed get of a forked task throws exception when task cancelled |
1227 |
|
*/ |
1228 |
|
public void testCancelledForkTimedGetCC() throws Exception { |
1229 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1230 |
< |
public void realCompute() throws Exception { |
1229 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1230 |
> |
protected void realCompute() throws Exception { |
1231 |
|
CCF f = new LCCF(null, 8); |
1232 |
|
assertTrue(f.cancel(true)); |
1233 |
|
assertSame(f, f.fork()); |
1245 |
|
* quietlyJoin of a forked task returns when task cancelled |
1246 |
|
*/ |
1247 |
|
public void testCancelledForkQuietlyJoinCC() { |
1248 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1249 |
< |
public void realCompute() { |
1248 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1249 |
> |
protected void realCompute() { |
1250 |
|
CCF f = new LCCF(null, 8); |
1251 |
|
assertTrue(f.cancel(true)); |
1252 |
|
assertSame(f, f.fork()); |
1260 |
|
* getPool of non-FJ task returns null |
1261 |
|
*/ |
1262 |
|
public void testGetPool2CC() { |
1263 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1264 |
< |
public void realCompute() { |
1263 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1264 |
> |
protected void realCompute() { |
1265 |
|
assertNull(getPool()); |
1266 |
|
}}; |
1267 |
|
assertNull(a.invoke()); |
1271 |
|
* inForkJoinPool of non-FJ task returns false |
1272 |
|
*/ |
1273 |
|
public void testInForkJoinPool2CC() { |
1274 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1275 |
< |
public void realCompute() { |
1274 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1275 |
> |
protected void realCompute() { |
1276 |
|
assertFalse(inForkJoinPool()); |
1277 |
|
}}; |
1278 |
|
assertNull(a.invoke()); |
1282 |
|
* setRawResult(null) succeeds |
1283 |
|
*/ |
1284 |
|
public void testSetRawResultCC() { |
1285 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1286 |
< |
public void realCompute() { |
1285 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1286 |
> |
protected void realCompute() { |
1287 |
|
setRawResult(null); |
1288 |
|
assertNull(getRawResult()); |
1289 |
|
}}; |
1294 |
|
* invoke task throws exception after invoking completeExceptionally |
1295 |
|
*/ |
1296 |
|
public void testCompleteExceptionally2CC() { |
1297 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1298 |
< |
public void realCompute() { |
1297 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1298 |
> |
protected void realCompute() { |
1299 |
|
CCF f = new LCCF(null, 8); |
1300 |
|
f.completeExceptionally(new FJException()); |
1301 |
|
try { |
1312 |
|
* invokeAll(t1, t2) invokes all task arguments |
1313 |
|
*/ |
1314 |
|
public void testInvokeAll2CC() { |
1315 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1316 |
< |
public void realCompute() { |
1315 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1316 |
> |
protected void realCompute() { |
1317 |
|
CCF f = new LCCF(null, 8); |
1318 |
|
CCF g = new LCCF(null, 9); |
1319 |
|
invokeAll(f, g); |
1329 |
|
* invokeAll(tasks) with 1 argument invokes task |
1330 |
|
*/ |
1331 |
|
public void testInvokeAll1CC() { |
1332 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1333 |
< |
public void realCompute() { |
1332 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1333 |
> |
protected void realCompute() { |
1334 |
|
CCF f = new LCCF(null, 8); |
1335 |
|
invokeAll(f); |
1336 |
|
checkCompletedNormally(f); |
1343 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1344 |
|
*/ |
1345 |
|
public void testInvokeAll3CC() { |
1346 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1347 |
< |
public void realCompute() { |
1346 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1347 |
> |
protected void realCompute() { |
1348 |
|
CCF f = new LCCF(null, 8); |
1349 |
|
CCF g = new LCCF(null, 9); |
1350 |
|
CCF h = new LCCF(null, 7); |
1363 |
|
* invokeAll(collection) invokes all tasks in the collection |
1364 |
|
*/ |
1365 |
|
public void testInvokeAllCollectionCC() { |
1366 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1367 |
< |
public void realCompute() { |
1366 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1367 |
> |
protected void realCompute() { |
1368 |
|
CCF f = new LCCF(null, 8); |
1369 |
|
CCF g = new LCCF(null, 9); |
1370 |
|
CCF h = new LCCF(null, 7); |
1387 |
|
* invokeAll(tasks) with any null task throws NPE |
1388 |
|
*/ |
1389 |
|
public void testInvokeAllNPECC() { |
1390 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1391 |
< |
public void realCompute() { |
1390 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1391 |
> |
protected void realCompute() { |
1392 |
|
CCF f = new LCCF(null, 8); |
1393 |
|
CCF g = new LCCF(null, 9); |
1394 |
|
CCF h = null; |
1404 |
|
* invokeAll(t1, t2) throw exception if any task does |
1405 |
|
*/ |
1406 |
|
public void testAbnormalInvokeAll2CC() { |
1407 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1408 |
< |
public void realCompute() { |
1407 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1408 |
> |
protected void realCompute() { |
1409 |
|
CCF f = new LCCF(null, 8); |
1410 |
|
FailingCCF g = new LFCCF(null, 9); |
1411 |
|
try { |
1422 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1423 |
|
*/ |
1424 |
|
public void testAbnormalInvokeAll1CC() { |
1425 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1426 |
< |
public void realCompute() { |
1425 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1426 |
> |
protected void realCompute() { |
1427 |
|
FailingCCF g = new LFCCF(null, 9); |
1428 |
|
try { |
1429 |
|
invokeAll(g); |
1439 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1440 |
|
*/ |
1441 |
|
public void testAbnormalInvokeAll3CC() { |
1442 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1443 |
< |
public void realCompute() { |
1442 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1443 |
> |
protected void realCompute() { |
1444 |
|
CCF f = new LCCF(null, 8); |
1445 |
|
FailingCCF g = new LFCCF(null, 9); |
1446 |
|
CCF h = new LCCF(null, 7); |
1455 |
|
} |
1456 |
|
|
1457 |
|
/** |
1458 |
< |
* invokeAll(collection) throws exception if any task does |
1458 |
> |
* invokeAll(collection) throws exception if any task does |
1459 |
|
*/ |
1460 |
|
public void testAbnormalInvokeAllCollectionCC() { |
1461 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1462 |
< |
public void realCompute() { |
1461 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1462 |
> |
protected void realCompute() { |
1463 |
|
FailingCCF f = new LFCCF(null, 8); |
1464 |
|
CCF g = new LCCF(null, 9); |
1465 |
|
CCF h = new LCCF(null, 7); |
1477 |
|
checkInvoke(a); |
1478 |
|
} |
1479 |
|
|
1480 |
+ |
/** |
1481 |
+ |
* awaitQuiescence by a worker is equivalent in effect to |
1482 |
+ |
* ForkJoinTask.helpQuiesce() |
1483 |
+ |
*/ |
1484 |
+ |
public void testAwaitQuiescence1() throws Exception { |
1485 |
+ |
final ForkJoinPool p = new ForkJoinPool(); |
1486 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1487 |
+ |
final long startTime = System.nanoTime(); |
1488 |
+ |
assertTrue(p.isQuiescent()); |
1489 |
+ |
ForkJoinTask a = new CheckedRecursiveAction() { |
1490 |
+ |
protected void realCompute() { |
1491 |
+ |
FibAction f = new FibAction(8); |
1492 |
+ |
assertSame(f, f.fork()); |
1493 |
+ |
assertSame(p, ForkJoinTask.getPool()); |
1494 |
+ |
boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); |
1495 |
+ |
assertTrue(quiescent); |
1496 |
+ |
assertFalse(p.isQuiescent()); |
1497 |
+ |
while (!f.isDone()) { |
1498 |
+ |
assertFalse(p.getAsyncMode()); |
1499 |
+ |
assertFalse(p.isShutdown()); |
1500 |
+ |
assertFalse(p.isTerminating()); |
1501 |
+ |
assertFalse(p.isTerminated()); |
1502 |
+ |
Thread.yield(); |
1503 |
+ |
} |
1504 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1505 |
+ |
assertFalse(p.isQuiescent()); |
1506 |
+ |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1507 |
+ |
assertEquals(21, f.result); |
1508 |
+ |
}}; |
1509 |
+ |
p.execute(a); |
1510 |
+ |
while (!a.isDone() || !p.isQuiescent()) { |
1511 |
+ |
assertFalse(p.getAsyncMode()); |
1512 |
+ |
assertFalse(p.isShutdown()); |
1513 |
+ |
assertFalse(p.isTerminating()); |
1514 |
+ |
assertFalse(p.isTerminated()); |
1515 |
+ |
Thread.yield(); |
1516 |
+ |
} |
1517 |
+ |
assertEquals(0, p.getQueuedTaskCount()); |
1518 |
+ |
assertFalse(p.getAsyncMode()); |
1519 |
+ |
assertEquals(0, p.getQueuedSubmissionCount()); |
1520 |
+ |
assertFalse(p.hasQueuedSubmissions()); |
1521 |
+ |
while (p.getActiveThreadCount() != 0 |
1522 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1523 |
+ |
Thread.yield(); |
1524 |
+ |
assertFalse(p.isShutdown()); |
1525 |
+ |
assertFalse(p.isTerminating()); |
1526 |
+ |
assertFalse(p.isTerminated()); |
1527 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1528 |
+ |
} |
1529 |
+ |
} |
1530 |
+ |
|
1531 |
+ |
/** |
1532 |
+ |
* awaitQuiescence returns when pool isQuiescent() or the indicated |
1533 |
+ |
* timeout elapsed |
1534 |
+ |
*/ |
1535 |
+ |
public void testAwaitQuiescence2() throws Exception { |
1536 |
+ |
/** |
1537 |
+ |
* """It is possible to disable or limit the use of threads in the |
1538 |
+ |
* common pool by setting the parallelism property to zero. However |
1539 |
+ |
* doing so may cause unjoined tasks to never be executed.""" |
1540 |
+ |
*/ |
1541 |
+ |
if ("0".equals(System.getProperty( |
1542 |
+ |
"java.util.concurrent.ForkJoinPool.common.parallelism"))) |
1543 |
+ |
return; |
1544 |
+ |
final ForkJoinPool p = new ForkJoinPool(); |
1545 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1546 |
+ |
assertTrue(p.isQuiescent()); |
1547 |
+ |
final long startTime = System.nanoTime(); |
1548 |
+ |
ForkJoinTask a = new CheckedRecursiveAction() { |
1549 |
+ |
protected void realCompute() { |
1550 |
+ |
FibAction f = new FibAction(8); |
1551 |
+ |
assertSame(f, f.fork()); |
1552 |
+ |
while (!f.isDone() |
1553 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) { |
1554 |
+ |
assertFalse(p.getAsyncMode()); |
1555 |
+ |
assertFalse(p.isShutdown()); |
1556 |
+ |
assertFalse(p.isTerminating()); |
1557 |
+ |
assertFalse(p.isTerminated()); |
1558 |
+ |
Thread.yield(); |
1559 |
+ |
} |
1560 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1561 |
+ |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1562 |
+ |
assertEquals(21, f.result); |
1563 |
+ |
}}; |
1564 |
+ |
p.execute(a); |
1565 |
+ |
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); |
1566 |
+ |
assertTrue(p.isQuiescent()); |
1567 |
+ |
assertTrue(a.isDone()); |
1568 |
+ |
assertEquals(0, p.getQueuedTaskCount()); |
1569 |
+ |
assertFalse(p.getAsyncMode()); |
1570 |
+ |
assertEquals(0, p.getQueuedSubmissionCount()); |
1571 |
+ |
assertFalse(p.hasQueuedSubmissions()); |
1572 |
+ |
while (p.getActiveThreadCount() != 0 |
1573 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1574 |
+ |
Thread.yield(); |
1575 |
+ |
assertFalse(p.isShutdown()); |
1576 |
+ |
assertFalse(p.isTerminating()); |
1577 |
+ |
assertFalse(p.isTerminated()); |
1578 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1579 |
+ |
} |
1580 |
+ |
} |
1581 |
+ |
|
1582 |
|
} |