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.SynchronousQueue; |
18 |
|
import java.util.concurrent.ThreadLocalRandom; |
16 |
– |
import java.util.concurrent.TimeUnit; |
19 |
|
import java.util.concurrent.TimeoutException; |
20 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
< |
import java.util.Arrays; |
22 |
< |
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() { |
46 |
|
} |
47 |
|
|
48 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
49 |
< |
try { |
49 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
50 |
|
checkNotDone(a); |
51 |
|
|
52 |
|
assertNull(pool.invoke(a)); |
53 |
|
|
54 |
|
checkCompletedNormally(a); |
53 |
– |
} finally { |
54 |
– |
joinPool(pool); |
55 |
|
} |
56 |
|
} |
57 |
|
|
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(); |
261 |
> |
final Thread currentThread = Thread.currentThread(); |
262 |
|
|
263 |
|
// test join() |
264 |
|
assertSame(f, f.fork()); |
265 |
< |
myself.interrupt(); |
266 |
< |
assertTrue(myself.isInterrupted()); |
265 |
> |
currentThread.interrupt(); |
266 |
|
assertNull(f.join()); |
267 |
|
Thread.interrupted(); |
268 |
|
assertEquals(21, f.result); |
271 |
|
f = new FibAction(8); |
272 |
|
f.cancel(true); |
273 |
|
assertSame(f, f.fork()); |
274 |
< |
myself.interrupt(); |
276 |
< |
assertTrue(myself.isInterrupted()); |
274 |
> |
currentThread.interrupt(); |
275 |
|
try { |
276 |
|
f.join(); |
277 |
|
shouldThrow(); |
283 |
|
f = new FibAction(8); |
284 |
|
f.completeExceptionally(new FJException()); |
285 |
|
assertSame(f, f.fork()); |
286 |
< |
myself.interrupt(); |
289 |
< |
assertTrue(myself.isInterrupted()); |
286 |
> |
currentThread.interrupt(); |
287 |
|
try { |
288 |
|
f.join(); |
289 |
|
shouldThrow(); |
295 |
|
// test quietlyJoin() |
296 |
|
f = new FibAction(8); |
297 |
|
assertSame(f, f.fork()); |
298 |
< |
myself.interrupt(); |
302 |
< |
assertTrue(myself.isInterrupted()); |
298 |
> |
currentThread.interrupt(); |
299 |
|
f.quietlyJoin(); |
300 |
|
Thread.interrupted(); |
301 |
|
assertEquals(21, f.result); |
304 |
|
f = new FibAction(8); |
305 |
|
f.cancel(true); |
306 |
|
assertSame(f, f.fork()); |
307 |
< |
myself.interrupt(); |
312 |
< |
assertTrue(myself.isInterrupted()); |
307 |
> |
currentThread.interrupt(); |
308 |
|
f.quietlyJoin(); |
309 |
|
Thread.interrupted(); |
310 |
|
checkCancelled(f); |
312 |
|
f = new FibAction(8); |
313 |
|
f.completeExceptionally(new FJException()); |
314 |
|
assertSame(f, f.fork()); |
315 |
< |
myself.interrupt(); |
321 |
< |
assertTrue(myself.isInterrupted()); |
315 |
> |
currentThread.interrupt(); |
316 |
|
f.quietlyJoin(); |
317 |
|
Thread.interrupted(); |
318 |
|
checkCompletedAbnormally(f, f.getException()); |
330 |
|
final SynchronousQueue<FibAction[]> sq = |
331 |
|
new SynchronousQueue<FibAction[]>(); |
332 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
333 |
< |
public void realCompute() throws InterruptedException { |
333 |
> |
protected void realCompute() throws InterruptedException { |
334 |
|
FibAction[] fibActions = new FibAction[6]; |
335 |
|
for (int i = 0; i < fibActions.length; i++) |
336 |
|
fibActions[i] = new FibAction(8); |
352 |
|
public void realRun() throws InterruptedException { |
353 |
|
FibAction[] fibActions = sq.take(); |
354 |
|
FibAction f; |
355 |
< |
final Thread myself = Thread.currentThread(); |
355 |
> |
final Thread currentThread = Thread.currentThread(); |
356 |
|
|
357 |
|
// test join() ------------ |
358 |
|
|
359 |
|
f = fibActions[0]; |
360 |
|
assertFalse(ForkJoinTask.inForkJoinPool()); |
361 |
< |
myself.interrupt(); |
368 |
< |
assertTrue(myself.isInterrupted()); |
361 |
> |
currentThread.interrupt(); |
362 |
|
assertNull(f.join()); |
363 |
|
assertTrue(Thread.interrupted()); |
364 |
|
assertEquals(21, f.result); |
365 |
|
checkCompletedNormally(f); |
366 |
|
|
367 |
|
f = fibActions[1]; |
368 |
< |
myself.interrupt(); |
376 |
< |
assertTrue(myself.isInterrupted()); |
368 |
> |
currentThread.interrupt(); |
369 |
|
try { |
370 |
|
f.join(); |
371 |
|
shouldThrow(); |
375 |
|
} |
376 |
|
|
377 |
|
f = fibActions[2]; |
378 |
< |
myself.interrupt(); |
387 |
< |
assertTrue(myself.isInterrupted()); |
378 |
> |
currentThread.interrupt(); |
379 |
|
try { |
380 |
|
f.join(); |
381 |
|
shouldThrow(); |
387 |
|
// test quietlyJoin() --------- |
388 |
|
|
389 |
|
f = fibActions[3]; |
390 |
< |
myself.interrupt(); |
400 |
< |
assertTrue(myself.isInterrupted()); |
390 |
> |
currentThread.interrupt(); |
391 |
|
f.quietlyJoin(); |
392 |
|
assertTrue(Thread.interrupted()); |
393 |
|
assertEquals(21, f.result); |
394 |
|
checkCompletedNormally(f); |
395 |
|
|
396 |
|
f = fibActions[4]; |
397 |
< |
myself.interrupt(); |
408 |
< |
assertTrue(myself.isInterrupted()); |
397 |
> |
currentThread.interrupt(); |
398 |
|
f.quietlyJoin(); |
399 |
|
assertTrue(Thread.interrupted()); |
400 |
|
checkCancelled(f); |
401 |
|
|
402 |
|
f = fibActions[5]; |
403 |
< |
myself.interrupt(); |
415 |
< |
assertTrue(myself.isInterrupted()); |
403 |
> |
currentThread.interrupt(); |
404 |
|
f.quietlyJoin(); |
405 |
|
assertTrue(Thread.interrupted()); |
406 |
|
assertTrue(f.getException() instanceof FJException); |
411 |
|
|
412 |
|
t = newStartedThread(r); |
413 |
|
testInvokeOnPool(mainPool(), a); |
414 |
< |
awaitTermination(t, LONG_DELAY_MS); |
414 |
> |
awaitTermination(t); |
415 |
|
|
416 |
|
a.reinitialize(); |
417 |
|
t = newStartedThread(r); |
418 |
|
testInvokeOnPool(singletonPool(), a); |
419 |
< |
awaitTermination(t, LONG_DELAY_MS); |
419 |
> |
awaitTermination(t); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
424 |
|
*/ |
425 |
|
public void testForkGet() { |
426 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
427 |
< |
public void realCompute() throws Exception { |
427 |
> |
protected void realCompute() throws Exception { |
428 |
|
FibAction f = new FibAction(8); |
429 |
|
assertSame(f, f.fork()); |
430 |
|
assertNull(f.get()); |
439 |
|
*/ |
440 |
|
public void testForkTimedGet() { |
441 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
442 |
< |
public void realCompute() throws Exception { |
442 |
> |
protected void realCompute() throws Exception { |
443 |
|
FibAction f = new FibAction(8); |
444 |
|
assertSame(f, f.fork()); |
445 |
|
assertNull(f.get(5L, SECONDS)); |
454 |
|
*/ |
455 |
|
public void testForkTimedGetNPE() { |
456 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
457 |
< |
public void realCompute() throws Exception { |
457 |
> |
protected void realCompute() throws Exception { |
458 |
|
FibAction f = new FibAction(8); |
459 |
|
assertSame(f, f.fork()); |
460 |
|
try { |
470 |
|
*/ |
471 |
|
public void testForkQuietlyJoin() { |
472 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
473 |
< |
public void realCompute() { |
473 |
> |
protected void realCompute() { |
474 |
|
FibAction f = new FibAction(8); |
475 |
|
assertSame(f, f.fork()); |
476 |
|
f.quietlyJoin(); |
486 |
|
*/ |
487 |
|
public void testForkHelpQuiesce() { |
488 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
489 |
< |
public void realCompute() { |
489 |
> |
protected void realCompute() { |
490 |
|
FibAction f = new FibAction(8); |
491 |
|
assertSame(f, f.fork()); |
492 |
|
helpQuiesce(); |
493 |
+ |
while (!f.isDone()) // wait out race |
494 |
+ |
; |
495 |
|
assertEquals(21, f.result); |
496 |
|
assertEquals(0, getQueuedTaskCount()); |
497 |
|
checkCompletedNormally(f); |
504 |
|
*/ |
505 |
|
public void testAbnormalInvoke() { |
506 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
507 |
< |
public void realCompute() { |
507 |
> |
protected void realCompute() { |
508 |
|
FailingFibAction f = new FailingFibAction(8); |
509 |
|
try { |
510 |
|
f.invoke(); |
521 |
|
*/ |
522 |
|
public void testAbnormalQuietlyInvoke() { |
523 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
524 |
< |
public void realCompute() { |
524 |
> |
protected void realCompute() { |
525 |
|
FailingFibAction f = new FailingFibAction(8); |
526 |
|
f.quietlyInvoke(); |
527 |
|
assertTrue(f.getException() instanceof FJException); |
535 |
|
*/ |
536 |
|
public void testAbnormalForkJoin() { |
537 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
538 |
< |
public void realCompute() { |
538 |
> |
protected void realCompute() { |
539 |
|
FailingFibAction f = new FailingFibAction(8); |
540 |
|
assertSame(f, f.fork()); |
541 |
|
try { |
553 |
|
*/ |
554 |
|
public void testAbnormalForkGet() { |
555 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
556 |
< |
public void realCompute() throws Exception { |
556 |
> |
protected void realCompute() throws Exception { |
557 |
|
FailingFibAction f = new FailingFibAction(8); |
558 |
|
assertSame(f, f.fork()); |
559 |
|
try { |
573 |
|
*/ |
574 |
|
public void testAbnormalForkTimedGet() { |
575 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
576 |
< |
public void realCompute() throws Exception { |
576 |
> |
protected void realCompute() throws Exception { |
577 |
|
FailingFibAction f = new FailingFibAction(8); |
578 |
|
assertSame(f, f.fork()); |
579 |
|
try { |
580 |
< |
f.get(5L, TimeUnit.SECONDS); |
580 |
> |
f.get(5L, SECONDS); |
581 |
|
shouldThrow(); |
582 |
|
} catch (ExecutionException success) { |
583 |
|
Throwable cause = success.getCause(); |
593 |
|
*/ |
594 |
|
public void testAbnormalForkQuietlyJoin() { |
595 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
596 |
< |
public void realCompute() { |
596 |
> |
protected void realCompute() { |
597 |
|
FailingFibAction f = new FailingFibAction(8); |
598 |
|
assertSame(f, f.fork()); |
599 |
|
f.quietlyJoin(); |
608 |
|
*/ |
609 |
|
public void testCancelledInvoke() { |
610 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
611 |
< |
public void realCompute() { |
611 |
> |
protected void realCompute() { |
612 |
|
FibAction f = new FibAction(8); |
613 |
|
assertTrue(f.cancel(true)); |
614 |
|
try { |
626 |
|
*/ |
627 |
|
public void testCancelledForkJoin() { |
628 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
629 |
< |
public void realCompute() { |
629 |
> |
protected void realCompute() { |
630 |
|
FibAction f = new FibAction(8); |
631 |
|
assertTrue(f.cancel(true)); |
632 |
|
assertSame(f, f.fork()); |
645 |
|
*/ |
646 |
|
public void testCancelledForkGet() { |
647 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
648 |
< |
public void realCompute() throws Exception { |
648 |
> |
protected void realCompute() throws Exception { |
649 |
|
FibAction f = new FibAction(8); |
650 |
|
assertTrue(f.cancel(true)); |
651 |
|
assertSame(f, f.fork()); |
664 |
|
*/ |
665 |
|
public void testCancelledForkTimedGet() { |
666 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
667 |
< |
public void realCompute() throws Exception { |
667 |
> |
protected void realCompute() throws Exception { |
668 |
|
FibAction f = new FibAction(8); |
669 |
|
assertTrue(f.cancel(true)); |
670 |
|
assertSame(f, f.fork()); |
683 |
|
*/ |
684 |
|
public void testCancelledForkQuietlyJoin() { |
685 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
686 |
< |
public void realCompute() { |
686 |
> |
protected void realCompute() { |
687 |
|
FibAction f = new FibAction(8); |
688 |
|
assertTrue(f.cancel(true)); |
689 |
|
assertSame(f, f.fork()); |
699 |
|
public void testGetPool() { |
700 |
|
final ForkJoinPool mainPool = mainPool(); |
701 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
702 |
< |
public void realCompute() { |
702 |
> |
protected void realCompute() { |
703 |
|
assertSame(mainPool, getPool()); |
704 |
|
}}; |
705 |
|
testInvokeOnPool(mainPool, a); |
710 |
|
*/ |
711 |
|
public void testGetPool2() { |
712 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
713 |
< |
public void realCompute() { |
713 |
> |
protected void realCompute() { |
714 |
|
assertNull(getPool()); |
715 |
|
}}; |
716 |
|
assertNull(a.invoke()); |
721 |
|
*/ |
722 |
|
public void testInForkJoinPool() { |
723 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
724 |
< |
public void realCompute() { |
724 |
> |
protected void realCompute() { |
725 |
|
assertTrue(inForkJoinPool()); |
726 |
|
}}; |
727 |
|
testInvokeOnPool(mainPool(), a); |
732 |
|
*/ |
733 |
|
public void testInForkJoinPool2() { |
734 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
735 |
< |
public void realCompute() { |
735 |
> |
protected void realCompute() { |
736 |
|
assertFalse(inForkJoinPool()); |
737 |
|
}}; |
738 |
|
assertNull(a.invoke()); |
744 |
|
public void testWorkerGetPool() { |
745 |
|
final ForkJoinPool mainPool = mainPool(); |
746 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
747 |
< |
public void realCompute() { |
747 |
> |
protected void realCompute() { |
748 |
|
ForkJoinWorkerThread w = |
749 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
750 |
|
assertSame(mainPool, w.getPool()); |
758 |
|
public void testWorkerGetPoolIndex() { |
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 |
|
assertTrue(w.getPoolIndex() >= 0); |
773 |
|
*/ |
774 |
|
public void testSetRawResult() { |
775 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
776 |
< |
public void realCompute() { |
776 |
> |
protected void realCompute() { |
777 |
|
setRawResult(null); |
778 |
|
assertNull(getRawResult()); |
779 |
|
}}; |
785 |
|
*/ |
786 |
|
public void testReinitialize() { |
787 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
788 |
< |
public void realCompute() { |
788 |
> |
protected void realCompute() { |
789 |
|
FibAction f = new FibAction(8); |
790 |
|
checkNotDone(f); |
791 |
|
|
805 |
|
*/ |
806 |
|
public void testReinitializeAbnormal() { |
807 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
808 |
< |
public void realCompute() { |
808 |
> |
protected void realCompute() { |
809 |
|
FailingFibAction f = new FailingFibAction(8); |
810 |
|
checkNotDone(f); |
811 |
|
|
828 |
|
*/ |
829 |
|
public void testCompleteExceptionally() { |
830 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
831 |
< |
public void realCompute() { |
831 |
> |
protected void realCompute() { |
832 |
|
FibAction f = new FibAction(8); |
833 |
|
f.completeExceptionally(new FJException()); |
834 |
|
try { |
846 |
|
*/ |
847 |
|
public void testComplete() { |
848 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
849 |
< |
public void realCompute() { |
849 |
> |
protected void realCompute() { |
850 |
|
FibAction f = new FibAction(8); |
851 |
|
f.complete(null); |
852 |
|
assertNull(f.invoke()); |
861 |
|
*/ |
862 |
|
public void testInvokeAll2() { |
863 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
864 |
< |
public void realCompute() { |
864 |
> |
protected void realCompute() { |
865 |
|
FibAction f = new FibAction(8); |
866 |
|
FibAction g = new FibAction(9); |
867 |
|
invokeAll(f, g); |
878 |
|
*/ |
879 |
|
public void testInvokeAll1() { |
880 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
881 |
< |
public void realCompute() { |
881 |
> |
protected void realCompute() { |
882 |
|
FibAction f = new FibAction(8); |
883 |
|
invokeAll(f); |
884 |
|
checkCompletedNormally(f); |
892 |
|
*/ |
893 |
|
public void testInvokeAll3() { |
894 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
895 |
< |
public void realCompute() { |
895 |
> |
protected void realCompute() { |
896 |
|
FibAction f = new FibAction(8); |
897 |
|
FibAction g = new FibAction(9); |
898 |
|
FibAction h = new FibAction(7); |
915 |
|
*/ |
916 |
|
public void testInvokeAllCollection() { |
917 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
918 |
< |
public void realCompute() { |
918 |
> |
protected void realCompute() { |
919 |
|
FibAction f = new FibAction(8); |
920 |
|
FibAction g = new FibAction(9); |
921 |
|
FibAction h = new FibAction(7); |
942 |
|
*/ |
943 |
|
public void testInvokeAllNPE() { |
944 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
945 |
< |
public void realCompute() { |
945 |
> |
protected void realCompute() { |
946 |
|
FibAction f = new FibAction(8); |
947 |
|
FibAction g = new FibAction(9); |
948 |
|
FibAction h = null; |
959 |
|
*/ |
960 |
|
public void testAbnormalInvokeAll2() { |
961 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
962 |
< |
public void realCompute() { |
962 |
> |
protected void realCompute() { |
963 |
|
FibAction f = new FibAction(8); |
964 |
|
FailingFibAction g = new FailingFibAction(9); |
965 |
|
try { |
977 |
|
*/ |
978 |
|
public void testAbnormalInvokeAll1() { |
979 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
980 |
< |
public void realCompute() { |
980 |
> |
protected void realCompute() { |
981 |
|
FailingFibAction g = new FailingFibAction(9); |
982 |
|
try { |
983 |
|
invokeAll(g); |
994 |
|
*/ |
995 |
|
public void testAbnormalInvokeAll3() { |
996 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
997 |
< |
public void realCompute() { |
997 |
> |
protected void realCompute() { |
998 |
|
FibAction f = new FibAction(8); |
999 |
|
FailingFibAction g = new FailingFibAction(9); |
1000 |
|
FibAction h = new FibAction(7); |
1013 |
|
*/ |
1014 |
|
public void testAbnormalInvokeAllCollection() { |
1015 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1016 |
< |
public void realCompute() { |
1016 |
> |
protected void realCompute() { |
1017 |
|
FailingFibAction f = new FailingFibAction(8); |
1018 |
|
FibAction g = new FibAction(9); |
1019 |
|
FibAction h = new FibAction(7); |
1037 |
|
*/ |
1038 |
|
public void testTryUnfork() { |
1039 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1040 |
< |
public void realCompute() { |
1040 |
> |
protected void realCompute() { |
1041 |
|
FibAction g = new FibAction(9); |
1042 |
|
assertSame(g, g.fork()); |
1043 |
|
FibAction f = new FibAction(8); |
1056 |
|
*/ |
1057 |
|
public void testGetSurplusQueuedTaskCount() { |
1058 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1059 |
< |
public void realCompute() { |
1059 |
> |
protected void realCompute() { |
1060 |
|
FibAction h = new FibAction(7); |
1061 |
|
assertSame(h, h.fork()); |
1062 |
|
FibAction g = new FibAction(9); |
1078 |
|
*/ |
1079 |
|
public void testPeekNextLocalTask() { |
1080 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1081 |
< |
public void realCompute() { |
1081 |
> |
protected void realCompute() { |
1082 |
|
FibAction g = new FibAction(9); |
1083 |
|
assertSame(g, g.fork()); |
1084 |
|
FibAction f = new FibAction(8); |
1099 |
|
*/ |
1100 |
|
public void testPollNextLocalTask() { |
1101 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1102 |
< |
public void realCompute() { |
1102 |
> |
protected void realCompute() { |
1103 |
|
FibAction g = new FibAction(9); |
1104 |
|
assertSame(g, g.fork()); |
1105 |
|
FibAction f = new FibAction(8); |
1117 |
|
*/ |
1118 |
|
public void testPollTask() { |
1119 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1120 |
< |
public void realCompute() { |
1120 |
> |
protected void realCompute() { |
1121 |
|
FibAction g = new FibAction(9); |
1122 |
|
assertSame(g, g.fork()); |
1123 |
|
FibAction f = new FibAction(8); |
1135 |
|
*/ |
1136 |
|
public void testPeekNextLocalTaskAsync() { |
1137 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1138 |
< |
public void realCompute() { |
1138 |
> |
protected void realCompute() { |
1139 |
|
FibAction g = new FibAction(9); |
1140 |
|
assertSame(g, g.fork()); |
1141 |
|
FibAction f = new FibAction(8); |
1155 |
|
*/ |
1156 |
|
public void testPollNextLocalTaskAsync() { |
1157 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1158 |
< |
public void realCompute() { |
1158 |
> |
protected void realCompute() { |
1159 |
|
FibAction g = new FibAction(9); |
1160 |
|
assertSame(g, g.fork()); |
1161 |
|
FibAction f = new FibAction(8); |
1174 |
|
*/ |
1175 |
|
public void testPollTaskAsync() { |
1176 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1177 |
< |
public void realCompute() { |
1177 |
> |
protected void realCompute() { |
1178 |
|
FibAction g = new FibAction(9); |
1179 |
|
assertSame(g, g.fork()); |
1180 |
|
FibAction f = new FibAction(8); |
1187 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1188 |
|
} |
1189 |
|
|
1190 |
+ |
/** Demo from RecursiveAction javadoc */ |
1191 |
|
static class SortTask extends RecursiveAction { |
1192 |
|
final long[] array; final int lo, hi; |
1193 |
|
SortTask(long[] array, int lo, int hi) { |
1194 |
|
this.array = array; this.lo = lo; this.hi = hi; |
1195 |
|
} |
1196 |
< |
final static int THRESHOLD = 100; |
1196 |
> |
SortTask(long[] array) { this(array, 0, array.length); } |
1197 |
|
protected void compute() { |
1198 |
|
if (hi - lo < THRESHOLD) |
1199 |
< |
sequentiallySort(array, lo, hi); |
1199 |
> |
sortSequentially(lo, hi); |
1200 |
|
else { |
1201 |
|
int mid = (lo + hi) >>> 1; |
1202 |
|
invokeAll(new SortTask(array, lo, mid), |
1203 |
|
new SortTask(array, mid, hi)); |
1204 |
< |
merge(array, lo, mid, hi); |
1204 |
> |
merge(lo, mid, hi); |
1205 |
|
} |
1206 |
|
} |
1207 |
< |
static void sequentiallySort(long[] array, int lo, int hi) { |
1207 |
> |
// implementation details follow: |
1208 |
> |
static final int THRESHOLD = 100; |
1209 |
> |
void sortSequentially(int lo, int hi) { |
1210 |
|
Arrays.sort(array, lo, hi); |
1211 |
|
} |
1212 |
< |
static void merge(long[] array, int lo, int mid, int hi) { |
1212 |
> |
void merge(int lo, int mid, int hi) { |
1213 |
|
long[] buf = Arrays.copyOfRange(array, lo, mid); |
1214 |
|
for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
1215 |
|
array[j] = (k == hi || buf[i] < array[k]) ? |
1226 |
|
for (int i = 0; i < array.length; i++) |
1227 |
|
array[i] = rnd.nextLong(); |
1228 |
|
long[] arrayClone = array.clone(); |
1229 |
< |
testInvokeOnPool(mainPool(), |
1237 |
< |
new SortTask(array, 0, array.length)); |
1229 |
> |
testInvokeOnPool(mainPool(), new SortTask(array)); |
1230 |
|
Arrays.sort(arrayClone); |
1231 |
|
assertTrue(Arrays.equals(array, arrayClone)); |
1232 |
|
} |