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.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
8 |
> |
|
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
9 |
– |
import java.util.concurrent.SynchronousQueue; |
12 |
|
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
import java.util.concurrent.ForkJoinTask; |
15 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
16 |
|
import java.util.concurrent.RecursiveAction; |
17 |
< |
import java.util.concurrent.TimeUnit; |
17 |
> |
import java.util.concurrent.SynchronousQueue; |
18 |
> |
import java.util.concurrent.ThreadLocalRandom; |
19 |
|
import java.util.concurrent.TimeoutException; |
20 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
< |
import java.util.HashSet; |
20 |
> |
|
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class RecursiveActionTest extends JSR166TestCase { |
25 |
|
|
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
|
30 |
|
public static Test suite() { |
138 |
|
assertFalse(a.isCancelled()); |
139 |
|
assertFalse(a.isCompletedNormally()); |
140 |
|
assertTrue(a.isCompletedAbnormally()); |
141 |
< |
assertSame(t, a.getException()); |
141 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
142 |
|
assertNull(a.getRawResult()); |
143 |
|
assertFalse(a.cancel(false)); |
144 |
|
assertFalse(a.cancel(true)); |
147 |
|
a.join(); |
148 |
|
shouldThrow(); |
149 |
|
} catch (Throwable expected) { |
150 |
< |
assertSame(t, expected); |
150 |
> |
assertSame(expected.getClass(), t.getClass()); |
151 |
|
} |
152 |
|
|
153 |
|
try { |
154 |
|
a.get(); |
155 |
|
shouldThrow(); |
156 |
|
} catch (ExecutionException success) { |
157 |
< |
assertSame(t, success.getCause()); |
157 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
158 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
|
|
160 |
|
try { |
161 |
|
a.get(5L, SECONDS); |
162 |
|
shouldThrow(); |
163 |
|
} catch (ExecutionException success) { |
164 |
< |
assertSame(t, success.getCause()); |
164 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
165 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
166 |
|
} |
167 |
|
|
168 |
< |
static final class FJException extends RuntimeException { |
169 |
< |
FJException() { super(); } |
168 |
> |
public static final class FJException extends RuntimeException { |
169 |
> |
public FJException() { super(); } |
170 |
> |
public FJException(Throwable cause) { super(cause); } |
171 |
|
} |
172 |
|
|
173 |
|
// A simple recursive action for testing |
175 |
|
final int number; |
176 |
|
int result; |
177 |
|
FibAction(int n) { number = n; } |
178 |
< |
public void realCompute() { |
178 |
> |
protected void realCompute() { |
179 |
|
int n = number; |
180 |
|
if (n <= 1) |
181 |
|
result = n; |
213 |
|
*/ |
214 |
|
public void testInvoke() { |
215 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
216 |
< |
public void realCompute() { |
216 |
> |
protected void realCompute() { |
217 |
|
FibAction f = new FibAction(8); |
218 |
|
assertNull(f.invoke()); |
219 |
|
assertEquals(21, f.result); |
229 |
|
*/ |
230 |
|
public void testQuietlyInvoke() { |
231 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
232 |
< |
public void realCompute() { |
232 |
> |
protected void realCompute() { |
233 |
|
FibAction f = new FibAction(8); |
234 |
|
f.quietlyInvoke(); |
235 |
|
assertEquals(21, f.result); |
243 |
|
*/ |
244 |
|
public void testForkJoin() { |
245 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
246 |
< |
public void realCompute() { |
246 |
> |
protected void realCompute() { |
247 |
|
FibAction f = new FibAction(8); |
248 |
|
assertSame(f, f.fork()); |
249 |
|
assertNull(f.join()); |
258 |
|
*/ |
259 |
|
public void testJoinIgnoresInterrupts() { |
260 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
261 |
< |
public void realCompute() { |
261 |
> |
protected void realCompute() { |
262 |
|
FibAction f = new FibAction(8); |
263 |
|
final Thread myself = Thread.currentThread(); |
264 |
|
|
271 |
|
assertEquals(21, f.result); |
272 |
|
checkCompletedNormally(f); |
273 |
|
|
274 |
< |
f.reinitialize(); |
274 |
> |
f = new FibAction(8); |
275 |
|
f.cancel(true); |
276 |
|
assertSame(f, f.fork()); |
277 |
|
myself.interrupt(); |
284 |
|
checkCancelled(f); |
285 |
|
} |
286 |
|
|
287 |
< |
f.reinitialize(); |
287 |
> |
f = new FibAction(8); |
288 |
|
f.completeExceptionally(new FJException()); |
289 |
|
assertSame(f, f.fork()); |
290 |
|
myself.interrupt(); |
298 |
|
} |
299 |
|
|
300 |
|
// test quietlyJoin() |
301 |
< |
f.reinitialize(); |
301 |
> |
f = new FibAction(8); |
302 |
|
assertSame(f, f.fork()); |
303 |
|
myself.interrupt(); |
304 |
|
assertTrue(myself.isInterrupted()); |
307 |
|
assertEquals(21, f.result); |
308 |
|
checkCompletedNormally(f); |
309 |
|
|
310 |
< |
f.reinitialize(); |
310 |
> |
f = new FibAction(8); |
311 |
|
f.cancel(true); |
312 |
|
assertSame(f, f.fork()); |
313 |
|
myself.interrupt(); |
316 |
|
Thread.interrupted(); |
317 |
|
checkCancelled(f); |
318 |
|
|
319 |
< |
f.reinitialize(); |
319 |
> |
f = new FibAction(8); |
320 |
|
f.completeExceptionally(new FJException()); |
321 |
|
assertSame(f, f.fork()); |
322 |
|
myself.interrupt(); |
338 |
|
final SynchronousQueue<FibAction[]> sq = |
339 |
|
new SynchronousQueue<FibAction[]>(); |
340 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
341 |
< |
public void realCompute() throws InterruptedException { |
341 |
> |
protected void realCompute() throws InterruptedException { |
342 |
|
FibAction[] fibActions = new FibAction[6]; |
343 |
|
for (int i = 0; i < fibActions.length; i++) |
344 |
|
fibActions[i] = new FibAction(8); |
438 |
|
*/ |
439 |
|
public void testForkGet() { |
440 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
441 |
< |
public void realCompute() throws Exception { |
441 |
> |
protected void realCompute() throws Exception { |
442 |
|
FibAction f = new FibAction(8); |
443 |
|
assertSame(f, f.fork()); |
444 |
|
assertNull(f.get()); |
453 |
|
*/ |
454 |
|
public void testForkTimedGet() { |
455 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
456 |
< |
public void realCompute() throws Exception { |
456 |
> |
protected void realCompute() throws Exception { |
457 |
|
FibAction f = new FibAction(8); |
458 |
|
assertSame(f, f.fork()); |
459 |
|
assertNull(f.get(5L, SECONDS)); |
468 |
|
*/ |
469 |
|
public void testForkTimedGetNPE() { |
470 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
471 |
< |
public void realCompute() throws Exception { |
471 |
> |
protected void realCompute() throws Exception { |
472 |
|
FibAction f = new FibAction(8); |
473 |
|
assertSame(f, f.fork()); |
474 |
|
try { |
484 |
|
*/ |
485 |
|
public void testForkQuietlyJoin() { |
486 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
487 |
< |
public void realCompute() { |
487 |
> |
protected void realCompute() { |
488 |
|
FibAction f = new FibAction(8); |
489 |
|
assertSame(f, f.fork()); |
490 |
|
f.quietlyJoin(); |
494 |
|
testInvokeOnPool(mainPool(), a); |
495 |
|
} |
496 |
|
|
492 |
– |
|
497 |
|
/** |
498 |
|
* helpQuiesce returns when tasks are complete. |
499 |
|
* getQueuedTaskCount returns 0 when quiescent |
500 |
|
*/ |
501 |
|
public void testForkHelpQuiesce() { |
502 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
503 |
< |
public void realCompute() { |
503 |
> |
protected void realCompute() { |
504 |
|
FibAction f = new FibAction(8); |
505 |
|
assertSame(f, f.fork()); |
506 |
< |
f.helpQuiesce(); |
506 |
> |
helpQuiesce(); |
507 |
> |
while (!f.isDone()) // wait out race |
508 |
> |
; |
509 |
|
assertEquals(21, f.result); |
510 |
|
assertEquals(0, getQueuedTaskCount()); |
511 |
|
checkCompletedNormally(f); |
513 |
|
testInvokeOnPool(mainPool(), a); |
514 |
|
} |
515 |
|
|
510 |
– |
|
516 |
|
/** |
517 |
|
* invoke task throws exception when task completes abnormally |
518 |
|
*/ |
519 |
|
public void testAbnormalInvoke() { |
520 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
521 |
< |
public void realCompute() { |
521 |
> |
protected void realCompute() { |
522 |
|
FailingFibAction f = new FailingFibAction(8); |
523 |
|
try { |
524 |
|
f.invoke(); |
535 |
|
*/ |
536 |
|
public void testAbnormalQuietlyInvoke() { |
537 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
538 |
< |
public void realCompute() { |
538 |
> |
protected void realCompute() { |
539 |
|
FailingFibAction f = new FailingFibAction(8); |
540 |
|
f.quietlyInvoke(); |
541 |
|
assertTrue(f.getException() instanceof FJException); |
549 |
|
*/ |
550 |
|
public void testAbnormalForkJoin() { |
551 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
552 |
< |
public void realCompute() { |
552 |
> |
protected void realCompute() { |
553 |
|
FailingFibAction f = new FailingFibAction(8); |
554 |
|
assertSame(f, f.fork()); |
555 |
|
try { |
567 |
|
*/ |
568 |
|
public void testAbnormalForkGet() { |
569 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
570 |
< |
public void realCompute() throws Exception { |
570 |
> |
protected void realCompute() throws Exception { |
571 |
|
FailingFibAction f = new FailingFibAction(8); |
572 |
|
assertSame(f, f.fork()); |
573 |
|
try { |
587 |
|
*/ |
588 |
|
public void testAbnormalForkTimedGet() { |
589 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
590 |
< |
public void realCompute() throws Exception { |
590 |
> |
protected void realCompute() throws Exception { |
591 |
|
FailingFibAction f = new FailingFibAction(8); |
592 |
|
assertSame(f, f.fork()); |
593 |
|
try { |
594 |
< |
f.get(5L, TimeUnit.SECONDS); |
594 |
> |
f.get(5L, SECONDS); |
595 |
|
shouldThrow(); |
596 |
|
} catch (ExecutionException success) { |
597 |
|
Throwable cause = success.getCause(); |
607 |
|
*/ |
608 |
|
public void testAbnormalForkQuietlyJoin() { |
609 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
610 |
< |
public void realCompute() { |
610 |
> |
protected void realCompute() { |
611 |
|
FailingFibAction f = new FailingFibAction(8); |
612 |
|
assertSame(f, f.fork()); |
613 |
|
f.quietlyJoin(); |
622 |
|
*/ |
623 |
|
public void testCancelledInvoke() { |
624 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
625 |
< |
public void realCompute() { |
625 |
> |
protected void realCompute() { |
626 |
|
FibAction f = new FibAction(8); |
627 |
|
assertTrue(f.cancel(true)); |
628 |
|
try { |
640 |
|
*/ |
641 |
|
public void testCancelledForkJoin() { |
642 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
643 |
< |
public void realCompute() { |
643 |
> |
protected void realCompute() { |
644 |
|
FibAction f = new FibAction(8); |
645 |
|
assertTrue(f.cancel(true)); |
646 |
|
assertSame(f, f.fork()); |
659 |
|
*/ |
660 |
|
public void testCancelledForkGet() { |
661 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
662 |
< |
public void realCompute() throws Exception { |
662 |
> |
protected void realCompute() throws Exception { |
663 |
|
FibAction f = new FibAction(8); |
664 |
|
assertTrue(f.cancel(true)); |
665 |
|
assertSame(f, f.fork()); |
678 |
|
*/ |
679 |
|
public void testCancelledForkTimedGet() { |
680 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
681 |
< |
public void realCompute() throws Exception { |
681 |
> |
protected void realCompute() throws Exception { |
682 |
|
FibAction f = new FibAction(8); |
683 |
|
assertTrue(f.cancel(true)); |
684 |
|
assertSame(f, f.fork()); |
697 |
|
*/ |
698 |
|
public void testCancelledForkQuietlyJoin() { |
699 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
700 |
< |
public void realCompute() { |
700 |
> |
protected void realCompute() { |
701 |
|
FibAction f = new FibAction(8); |
702 |
|
assertTrue(f.cancel(true)); |
703 |
|
assertSame(f, f.fork()); |
713 |
|
public void testGetPool() { |
714 |
|
final ForkJoinPool mainPool = mainPool(); |
715 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
716 |
< |
public void realCompute() { |
716 |
> |
protected void realCompute() { |
717 |
|
assertSame(mainPool, getPool()); |
718 |
|
}}; |
719 |
|
testInvokeOnPool(mainPool, a); |
724 |
|
*/ |
725 |
|
public void testGetPool2() { |
726 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
727 |
< |
public void realCompute() { |
727 |
> |
protected void realCompute() { |
728 |
|
assertNull(getPool()); |
729 |
|
}}; |
730 |
|
assertNull(a.invoke()); |
735 |
|
*/ |
736 |
|
public void testInForkJoinPool() { |
737 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
738 |
< |
public void realCompute() { |
738 |
> |
protected void realCompute() { |
739 |
|
assertTrue(inForkJoinPool()); |
740 |
|
}}; |
741 |
|
testInvokeOnPool(mainPool(), a); |
746 |
|
*/ |
747 |
|
public void testInForkJoinPool2() { |
748 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
749 |
< |
public void realCompute() { |
749 |
> |
protected void realCompute() { |
750 |
|
assertFalse(inForkJoinPool()); |
751 |
|
}}; |
752 |
|
assertNull(a.invoke()); |
758 |
|
public void testWorkerGetPool() { |
759 |
|
final ForkJoinPool mainPool = mainPool(); |
760 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
761 |
< |
public void realCompute() { |
761 |
> |
protected void realCompute() { |
762 |
|
ForkJoinWorkerThread w = |
763 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
764 |
|
assertSame(mainPool, w.getPool()); |
772 |
|
public void testWorkerGetPoolIndex() { |
773 |
|
final ForkJoinPool mainPool = mainPool(); |
774 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
775 |
< |
public void realCompute() { |
775 |
> |
protected void realCompute() { |
776 |
|
ForkJoinWorkerThread w = |
777 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
778 |
|
assertTrue(w.getPoolIndex() >= 0); |
779 |
< |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
779 |
> |
// pool size can shrink after assigning index, so cannot check |
780 |
> |
// assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
781 |
|
}}; |
782 |
|
testInvokeOnPool(mainPool, a); |
783 |
|
} |
784 |
|
|
779 |
– |
|
785 |
|
/** |
786 |
|
* setRawResult(null) succeeds |
787 |
|
*/ |
788 |
|
public void testSetRawResult() { |
789 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
790 |
< |
public void realCompute() { |
790 |
> |
protected void realCompute() { |
791 |
|
setRawResult(null); |
792 |
|
assertNull(getRawResult()); |
793 |
|
}}; |
795 |
|
} |
796 |
|
|
797 |
|
/** |
798 |
< |
* A reinitialized task may be re-invoked |
798 |
> |
* A reinitialized normally completed task may be re-invoked |
799 |
|
*/ |
800 |
|
public void testReinitialize() { |
801 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
802 |
< |
public void realCompute() { |
802 |
> |
protected void realCompute() { |
803 |
|
FibAction f = new FibAction(8); |
804 |
|
checkNotDone(f); |
805 |
|
|
815 |
|
} |
816 |
|
|
817 |
|
/** |
818 |
+ |
* A reinitialized abnormally completed task may be re-invoked |
819 |
+ |
*/ |
820 |
+ |
public void testReinitializeAbnormal() { |
821 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
822 |
+ |
protected void realCompute() { |
823 |
+ |
FailingFibAction f = new FailingFibAction(8); |
824 |
+ |
checkNotDone(f); |
825 |
+ |
|
826 |
+ |
for (int i = 0; i < 3; i++) { |
827 |
+ |
try { |
828 |
+ |
f.invoke(); |
829 |
+ |
shouldThrow(); |
830 |
+ |
} catch (FJException success) { |
831 |
+ |
checkCompletedAbnormally(f, success); |
832 |
+ |
} |
833 |
+ |
f.reinitialize(); |
834 |
+ |
checkNotDone(f); |
835 |
+ |
} |
836 |
+ |
}}; |
837 |
+ |
testInvokeOnPool(mainPool(), a); |
838 |
+ |
} |
839 |
+ |
|
840 |
+ |
/** |
841 |
|
* invoke task throws exception after invoking completeExceptionally |
842 |
|
*/ |
843 |
|
public void testCompleteExceptionally() { |
844 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
845 |
< |
public void realCompute() { |
845 |
> |
protected void realCompute() { |
846 |
|
FibAction f = new FibAction(8); |
847 |
|
f.completeExceptionally(new FJException()); |
848 |
|
try { |
860 |
|
*/ |
861 |
|
public void testComplete() { |
862 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
863 |
< |
public void realCompute() { |
863 |
> |
protected void realCompute() { |
864 |
|
FibAction f = new FibAction(8); |
865 |
|
f.complete(null); |
866 |
|
assertNull(f.invoke()); |
875 |
|
*/ |
876 |
|
public void testInvokeAll2() { |
877 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
878 |
< |
public void realCompute() { |
878 |
> |
protected void realCompute() { |
879 |
|
FibAction f = new FibAction(8); |
880 |
|
FibAction g = new FibAction(9); |
881 |
|
invokeAll(f, g); |
892 |
|
*/ |
893 |
|
public void testInvokeAll1() { |
894 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
895 |
< |
public void realCompute() { |
895 |
> |
protected void realCompute() { |
896 |
|
FibAction f = new FibAction(8); |
897 |
|
invokeAll(f); |
898 |
|
checkCompletedNormally(f); |
906 |
|
*/ |
907 |
|
public void testInvokeAll3() { |
908 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
909 |
< |
public void realCompute() { |
909 |
> |
protected void realCompute() { |
910 |
|
FibAction f = new FibAction(8); |
911 |
|
FibAction g = new FibAction(9); |
912 |
|
FibAction h = new FibAction(7); |
929 |
|
*/ |
930 |
|
public void testInvokeAllCollection() { |
931 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
932 |
< |
public void realCompute() { |
932 |
> |
protected void realCompute() { |
933 |
|
FibAction f = new FibAction(8); |
934 |
|
FibAction g = new FibAction(9); |
935 |
|
FibAction h = new FibAction(7); |
951 |
|
testInvokeOnPool(mainPool(), a); |
952 |
|
} |
953 |
|
|
926 |
– |
|
954 |
|
/** |
955 |
|
* invokeAll(tasks) with any null task throws NPE |
956 |
|
*/ |
957 |
|
public void testInvokeAllNPE() { |
958 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
959 |
< |
public void realCompute() { |
959 |
> |
protected void realCompute() { |
960 |
|
FibAction f = new FibAction(8); |
961 |
|
FibAction g = new FibAction(9); |
962 |
|
FibAction h = null; |
973 |
|
*/ |
974 |
|
public void testAbnormalInvokeAll2() { |
975 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
976 |
< |
public void realCompute() { |
976 |
> |
protected void realCompute() { |
977 |
|
FibAction f = new FibAction(8); |
978 |
|
FailingFibAction g = new FailingFibAction(9); |
979 |
|
try { |
991 |
|
*/ |
992 |
|
public void testAbnormalInvokeAll1() { |
993 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
994 |
< |
public void realCompute() { |
994 |
> |
protected void realCompute() { |
995 |
|
FailingFibAction g = new FailingFibAction(9); |
996 |
|
try { |
997 |
|
invokeAll(g); |
1008 |
|
*/ |
1009 |
|
public void testAbnormalInvokeAll3() { |
1010 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1011 |
< |
public void realCompute() { |
1011 |
> |
protected void realCompute() { |
1012 |
|
FibAction f = new FibAction(8); |
1013 |
|
FailingFibAction g = new FailingFibAction(9); |
1014 |
|
FibAction h = new FibAction(7); |
1027 |
|
*/ |
1028 |
|
public void testAbnormalInvokeAllCollection() { |
1029 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1030 |
< |
public void realCompute() { |
1030 |
> |
protected void realCompute() { |
1031 |
|
FailingFibAction f = new FailingFibAction(8); |
1032 |
|
FibAction g = new FibAction(9); |
1033 |
|
FibAction h = new FibAction(7); |
1051 |
|
*/ |
1052 |
|
public void testTryUnfork() { |
1053 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1054 |
< |
public void realCompute() { |
1054 |
> |
protected void realCompute() { |
1055 |
|
FibAction g = new FibAction(9); |
1056 |
|
assertSame(g, g.fork()); |
1057 |
|
FibAction f = new FibAction(8); |
1070 |
|
*/ |
1071 |
|
public void testGetSurplusQueuedTaskCount() { |
1072 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1073 |
< |
public void realCompute() { |
1073 |
> |
protected void realCompute() { |
1074 |
|
FibAction h = new FibAction(7); |
1075 |
|
assertSame(h, h.fork()); |
1076 |
|
FibAction g = new FibAction(9); |
1092 |
|
*/ |
1093 |
|
public void testPeekNextLocalTask() { |
1094 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1095 |
< |
public void realCompute() { |
1095 |
> |
protected void realCompute() { |
1096 |
|
FibAction g = new FibAction(9); |
1097 |
|
assertSame(g, g.fork()); |
1098 |
|
FibAction f = new FibAction(8); |
1113 |
|
*/ |
1114 |
|
public void testPollNextLocalTask() { |
1115 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1116 |
< |
public void realCompute() { |
1116 |
> |
protected void realCompute() { |
1117 |
|
FibAction g = new FibAction(9); |
1118 |
|
assertSame(g, g.fork()); |
1119 |
|
FibAction f = new FibAction(8); |
1131 |
|
*/ |
1132 |
|
public void testPollTask() { |
1133 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1134 |
< |
public void realCompute() { |
1134 |
> |
protected void realCompute() { |
1135 |
|
FibAction g = new FibAction(9); |
1136 |
|
assertSame(g, g.fork()); |
1137 |
|
FibAction f = new FibAction(8); |
1149 |
|
*/ |
1150 |
|
public void testPeekNextLocalTaskAsync() { |
1151 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1152 |
< |
public void realCompute() { |
1152 |
> |
protected void realCompute() { |
1153 |
|
FibAction g = new FibAction(9); |
1154 |
|
assertSame(g, g.fork()); |
1155 |
|
FibAction f = new FibAction(8); |
1169 |
|
*/ |
1170 |
|
public void testPollNextLocalTaskAsync() { |
1171 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1172 |
< |
public void realCompute() { |
1172 |
> |
protected void realCompute() { |
1173 |
|
FibAction g = new FibAction(9); |
1174 |
|
assertSame(g, g.fork()); |
1175 |
|
FibAction f = new FibAction(8); |
1188 |
|
*/ |
1189 |
|
public void testPollTaskAsync() { |
1190 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1191 |
< |
public void realCompute() { |
1191 |
> |
protected void realCompute() { |
1192 |
|
FibAction g = new FibAction(9); |
1193 |
|
assertSame(g, g.fork()); |
1194 |
|
FibAction f = new FibAction(8); |
1201 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1202 |
|
} |
1203 |
|
|
1204 |
+ |
/** Demo from RecursiveAction javadoc */ |
1205 |
+ |
static class SortTask extends RecursiveAction { |
1206 |
+ |
final long[] array; final int lo, hi; |
1207 |
+ |
SortTask(long[] array, int lo, int hi) { |
1208 |
+ |
this.array = array; this.lo = lo; this.hi = hi; |
1209 |
+ |
} |
1210 |
+ |
SortTask(long[] array) { this(array, 0, array.length); } |
1211 |
+ |
protected void compute() { |
1212 |
+ |
if (hi - lo < THRESHOLD) |
1213 |
+ |
sortSequentially(lo, hi); |
1214 |
+ |
else { |
1215 |
+ |
int mid = (lo + hi) >>> 1; |
1216 |
+ |
invokeAll(new SortTask(array, lo, mid), |
1217 |
+ |
new SortTask(array, mid, hi)); |
1218 |
+ |
merge(lo, mid, hi); |
1219 |
+ |
} |
1220 |
+ |
} |
1221 |
+ |
// implementation details follow: |
1222 |
+ |
static final int THRESHOLD = 100; |
1223 |
+ |
void sortSequentially(int lo, int hi) { |
1224 |
+ |
Arrays.sort(array, lo, hi); |
1225 |
+ |
} |
1226 |
+ |
void merge(int lo, int mid, int hi) { |
1227 |
+ |
long[] buf = Arrays.copyOfRange(array, lo, mid); |
1228 |
+ |
for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
1229 |
+ |
array[j] = (k == hi || buf[i] < array[k]) ? |
1230 |
+ |
buf[i++] : array[k++]; |
1231 |
+ |
} |
1232 |
+ |
} |
1233 |
+ |
|
1234 |
+ |
/** |
1235 |
+ |
* SortTask demo works as advertised |
1236 |
+ |
*/ |
1237 |
+ |
public void testSortTaskDemo() { |
1238 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1239 |
+ |
long[] array = new long[1007]; |
1240 |
+ |
for (int i = 0; i < array.length; i++) |
1241 |
+ |
array[i] = rnd.nextLong(); |
1242 |
+ |
long[] arrayClone = array.clone(); |
1243 |
+ |
testInvokeOnPool(mainPool(), new SortTask(array)); |
1244 |
+ |
Arrays.sort(arrayClone); |
1245 |
+ |
assertTrue(Arrays.equals(array, arrayClone)); |
1246 |
+ |
} |
1247 |
|
} |