ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
(Generate patch)

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.34 by jsr166, Sat Jun 25 05:20:30 2011 UTC vs.
Revision 1.47 by jsr166, Tue Aug 16 23:02:57 2016 UTC

# Line 4 | Line 4
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() {
# Line 44 | Line 46 | public class RecursiveActionTest extends
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  
# Line 173 | Line 173 | public class RecursiveActionTest extends
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;
# Line 211 | Line 211 | public class RecursiveActionTest extends
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);
# Line 227 | Line 227 | public class RecursiveActionTest extends
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);
# Line 241 | Line 241 | public class RecursiveActionTest extends
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());
# Line 256 | Line 256 | public class RecursiveActionTest extends
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 >                assertTrue(currentThread.isInterrupted());
267                  assertNull(f.join());
268                  Thread.interrupted();
269                  assertEquals(21, f.result);
# Line 272 | Line 272 | public class RecursiveActionTest extends
272                  f = new FibAction(8);
273                  f.cancel(true);
274                  assertSame(f, f.fork());
275 <                myself.interrupt();
276 <                assertTrue(myself.isInterrupted());
275 >                currentThread.interrupt();
276 >                assertTrue(currentThread.isInterrupted());
277                  try {
278                      f.join();
279                      shouldThrow();
# Line 285 | Line 285 | public class RecursiveActionTest extends
285                  f = new FibAction(8);
286                  f.completeExceptionally(new FJException());
287                  assertSame(f, f.fork());
288 <                myself.interrupt();
289 <                assertTrue(myself.isInterrupted());
288 >                currentThread.interrupt();
289 >                assertTrue(currentThread.isInterrupted());
290                  try {
291                      f.join();
292                      shouldThrow();
# Line 298 | Line 298 | public class RecursiveActionTest extends
298                  // test quietlyJoin()
299                  f = new FibAction(8);
300                  assertSame(f, f.fork());
301 <                myself.interrupt();
302 <                assertTrue(myself.isInterrupted());
301 >                currentThread.interrupt();
302 >                assertTrue(currentThread.isInterrupted());
303                  f.quietlyJoin();
304                  Thread.interrupted();
305                  assertEquals(21, f.result);
# Line 308 | Line 308 | public class RecursiveActionTest extends
308                  f = new FibAction(8);
309                  f.cancel(true);
310                  assertSame(f, f.fork());
311 <                myself.interrupt();
312 <                assertTrue(myself.isInterrupted());
311 >                currentThread.interrupt();
312 >                assertTrue(currentThread.isInterrupted());
313                  f.quietlyJoin();
314                  Thread.interrupted();
315                  checkCancelled(f);
# Line 317 | Line 317 | public class RecursiveActionTest extends
317                  f = new FibAction(8);
318                  f.completeExceptionally(new FJException());
319                  assertSame(f, f.fork());
320 <                myself.interrupt();
321 <                assertTrue(myself.isInterrupted());
320 >                currentThread.interrupt();
321 >                assertTrue(currentThread.isInterrupted());
322                  f.quietlyJoin();
323                  Thread.interrupted();
324                  checkCompletedAbnormally(f, f.getException());
# Line 336 | Line 336 | public class RecursiveActionTest extends
336          final SynchronousQueue<FibAction[]> sq =
337              new SynchronousQueue<FibAction[]>();
338          RecursiveAction a = new CheckedRecursiveAction() {
339 <            public void realCompute() throws InterruptedException {
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);
# Line 358 | Line 358 | public class RecursiveActionTest extends
358              public void realRun() throws InterruptedException {
359                  FibAction[] fibActions = sq.take();
360                  FibAction f;
361 <                final Thread myself = Thread.currentThread();
361 >                final Thread currentThread = Thread.currentThread();
362  
363                  // test join() ------------
364  
365                  f = fibActions[0];
366                  assertFalse(ForkJoinTask.inForkJoinPool());
367 <                myself.interrupt();
368 <                assertTrue(myself.isInterrupted());
367 >                currentThread.interrupt();
368 >                assertTrue(currentThread.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());
375 >                currentThread.interrupt();
376 >                assertTrue(currentThread.isInterrupted());
377                  try {
378                      f.join();
379                      shouldThrow();
# Line 383 | Line 383 | public class RecursiveActionTest extends
383                  }
384  
385                  f = fibActions[2];
386 <                myself.interrupt();
387 <                assertTrue(myself.isInterrupted());
386 >                currentThread.interrupt();
387 >                assertTrue(currentThread.isInterrupted());
388                  try {
389                      f.join();
390                      shouldThrow();
# Line 396 | Line 396 | public class RecursiveActionTest extends
396                  // test quietlyJoin() ---------
397  
398                  f = fibActions[3];
399 <                myself.interrupt();
400 <                assertTrue(myself.isInterrupted());
399 >                currentThread.interrupt();
400 >                assertTrue(currentThread.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());
407 >                currentThread.interrupt();
408 >                assertTrue(currentThread.isInterrupted());
409                  f.quietlyJoin();
410                  assertTrue(Thread.interrupted());
411                  checkCancelled(f);
412  
413                  f = fibActions[5];
414 <                myself.interrupt();
415 <                assertTrue(myself.isInterrupted());
414 >                currentThread.interrupt();
415 >                assertTrue(currentThread.isInterrupted());
416                  f.quietlyJoin();
417                  assertTrue(Thread.interrupted());
418                  assertTrue(f.getException() instanceof FJException);
# Line 423 | Line 423 | public class RecursiveActionTest extends
423  
424          t = newStartedThread(r);
425          testInvokeOnPool(mainPool(), a);
426 <        awaitTermination(t, LONG_DELAY_MS);
426 >        awaitTermination(t);
427  
428          a.reinitialize();
429          t = newStartedThread(r);
430          testInvokeOnPool(singletonPool(), a);
431 <        awaitTermination(t, LONG_DELAY_MS);
431 >        awaitTermination(t);
432      }
433  
434      /**
# Line 436 | Line 436 | public class RecursiveActionTest extends
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());
# Line 451 | Line 451 | public class RecursiveActionTest extends
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));
# Line 466 | Line 466 | public class RecursiveActionTest extends
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 {
# Line 482 | Line 482 | public class RecursiveActionTest extends
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();
# Line 498 | Line 498 | public class RecursiveActionTest extends
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                  helpQuiesce();
505 +                while (!f.isDone()) // wait out race
506 +                    ;
507                  assertEquals(21, f.result);
508                  assertEquals(0, getQueuedTaskCount());
509                  checkCompletedNormally(f);
# Line 514 | Line 516 | public class RecursiveActionTest extends
516       */
517      public void testAbnormalInvoke() {
518          RecursiveAction a = new CheckedRecursiveAction() {
519 <            public void realCompute() {
519 >            protected void realCompute() {
520                  FailingFibAction f = new FailingFibAction(8);
521                  try {
522                      f.invoke();
# Line 531 | Line 533 | public class RecursiveActionTest extends
533       */
534      public void testAbnormalQuietlyInvoke() {
535          RecursiveAction a = new CheckedRecursiveAction() {
536 <            public void realCompute() {
536 >            protected void realCompute() {
537                  FailingFibAction f = new FailingFibAction(8);
538                  f.quietlyInvoke();
539                  assertTrue(f.getException() instanceof FJException);
# Line 545 | Line 547 | public class RecursiveActionTest extends
547       */
548      public void testAbnormalForkJoin() {
549          RecursiveAction a = new CheckedRecursiveAction() {
550 <            public void realCompute() {
550 >            protected void realCompute() {
551                  FailingFibAction f = new FailingFibAction(8);
552                  assertSame(f, f.fork());
553                  try {
# Line 563 | Line 565 | public class RecursiveActionTest extends
565       */
566      public void testAbnormalForkGet() {
567          RecursiveAction a = new CheckedRecursiveAction() {
568 <            public void realCompute() throws Exception {
568 >            protected void realCompute() throws Exception {
569                  FailingFibAction f = new FailingFibAction(8);
570                  assertSame(f, f.fork());
571                  try {
# Line 583 | Line 585 | public class RecursiveActionTest extends
585       */
586      public void testAbnormalForkTimedGet() {
587          RecursiveAction a = new CheckedRecursiveAction() {
588 <            public void realCompute() throws Exception {
588 >            protected void realCompute() throws Exception {
589                  FailingFibAction f = new FailingFibAction(8);
590                  assertSame(f, f.fork());
591                  try {
592 <                    f.get(5L, TimeUnit.SECONDS);
592 >                    f.get(5L, SECONDS);
593                      shouldThrow();
594                  } catch (ExecutionException success) {
595                      Throwable cause = success.getCause();
# Line 603 | Line 605 | public class RecursiveActionTest extends
605       */
606      public void testAbnormalForkQuietlyJoin() {
607          RecursiveAction a = new CheckedRecursiveAction() {
608 <            public void realCompute() {
608 >            protected void realCompute() {
609                  FailingFibAction f = new FailingFibAction(8);
610                  assertSame(f, f.fork());
611                  f.quietlyJoin();
# Line 618 | Line 620 | public class RecursiveActionTest extends
620       */
621      public void testCancelledInvoke() {
622          RecursiveAction a = new CheckedRecursiveAction() {
623 <            public void realCompute() {
623 >            protected void realCompute() {
624                  FibAction f = new FibAction(8);
625                  assertTrue(f.cancel(true));
626                  try {
# Line 636 | Line 638 | public class RecursiveActionTest extends
638       */
639      public void testCancelledForkJoin() {
640          RecursiveAction a = new CheckedRecursiveAction() {
641 <            public void realCompute() {
641 >            protected void realCompute() {
642                  FibAction f = new FibAction(8);
643                  assertTrue(f.cancel(true));
644                  assertSame(f, f.fork());
# Line 655 | Line 657 | public class RecursiveActionTest extends
657       */
658      public void testCancelledForkGet() {
659          RecursiveAction a = new CheckedRecursiveAction() {
660 <            public void realCompute() throws Exception {
660 >            protected void realCompute() throws Exception {
661                  FibAction f = new FibAction(8);
662                  assertTrue(f.cancel(true));
663                  assertSame(f, f.fork());
# Line 674 | Line 676 | public class RecursiveActionTest extends
676       */
677      public void testCancelledForkTimedGet() {
678          RecursiveAction a = new CheckedRecursiveAction() {
679 <            public void realCompute() throws Exception {
679 >            protected void realCompute() throws Exception {
680                  FibAction f = new FibAction(8);
681                  assertTrue(f.cancel(true));
682                  assertSame(f, f.fork());
# Line 693 | Line 695 | public class RecursiveActionTest extends
695       */
696      public void testCancelledForkQuietlyJoin() {
697          RecursiveAction a = new CheckedRecursiveAction() {
698 <            public void realCompute() {
698 >            protected void realCompute() {
699                  FibAction f = new FibAction(8);
700                  assertTrue(f.cancel(true));
701                  assertSame(f, f.fork());
# Line 709 | Line 711 | public class RecursiveActionTest extends
711      public void testGetPool() {
712          final ForkJoinPool mainPool = mainPool();
713          RecursiveAction a = new CheckedRecursiveAction() {
714 <            public void realCompute() {
714 >            protected void realCompute() {
715                  assertSame(mainPool, getPool());
716              }};
717          testInvokeOnPool(mainPool, a);
# Line 720 | Line 722 | public class RecursiveActionTest extends
722       */
723      public void testGetPool2() {
724          RecursiveAction a = new CheckedRecursiveAction() {
725 <            public void realCompute() {
725 >            protected void realCompute() {
726                  assertNull(getPool());
727              }};
728          assertNull(a.invoke());
# Line 731 | Line 733 | public class RecursiveActionTest extends
733       */
734      public void testInForkJoinPool() {
735          RecursiveAction a = new CheckedRecursiveAction() {
736 <            public void realCompute() {
736 >            protected void realCompute() {
737                  assertTrue(inForkJoinPool());
738              }};
739          testInvokeOnPool(mainPool(), a);
# Line 742 | Line 744 | public class RecursiveActionTest extends
744       */
745      public void testInForkJoinPool2() {
746          RecursiveAction a = new CheckedRecursiveAction() {
747 <            public void realCompute() {
747 >            protected void realCompute() {
748                  assertFalse(inForkJoinPool());
749              }};
750          assertNull(a.invoke());
# Line 754 | Line 756 | public class RecursiveActionTest extends
756      public void testWorkerGetPool() {
757          final ForkJoinPool mainPool = mainPool();
758          RecursiveAction a = new CheckedRecursiveAction() {
759 <            public void realCompute() {
759 >            protected void realCompute() {
760                  ForkJoinWorkerThread w =
761                      (ForkJoinWorkerThread) Thread.currentThread();
762                  assertSame(mainPool, w.getPool());
# Line 768 | Line 770 | public class RecursiveActionTest extends
770      public void testWorkerGetPoolIndex() {
771          final ForkJoinPool mainPool = mainPool();
772          RecursiveAction a = new CheckedRecursiveAction() {
773 <            public void realCompute() {
773 >            protected void realCompute() {
774                  ForkJoinWorkerThread w =
775                      (ForkJoinWorkerThread) Thread.currentThread();
776                  assertTrue(w.getPoolIndex() >= 0);
# Line 783 | Line 785 | public class RecursiveActionTest extends
785       */
786      public void testSetRawResult() {
787          RecursiveAction a = new CheckedRecursiveAction() {
788 <            public void realCompute() {
788 >            protected void realCompute() {
789                  setRawResult(null);
790                  assertNull(getRawResult());
791              }};
# Line 795 | Line 797 | public class RecursiveActionTest extends
797       */
798      public void testReinitialize() {
799          RecursiveAction a = new CheckedRecursiveAction() {
800 <            public void realCompute() {
800 >            protected void realCompute() {
801                  FibAction f = new FibAction(8);
802                  checkNotDone(f);
803  
# Line 815 | Line 817 | public class RecursiveActionTest extends
817       */
818      public void testReinitializeAbnormal() {
819          RecursiveAction a = new CheckedRecursiveAction() {
820 <            public void realCompute() {
820 >            protected void realCompute() {
821                  FailingFibAction f = new FailingFibAction(8);
822                  checkNotDone(f);
823  
# Line 838 | Line 840 | public class RecursiveActionTest extends
840       */
841      public void testCompleteExceptionally() {
842          RecursiveAction a = new CheckedRecursiveAction() {
843 <            public void realCompute() {
843 >            protected void realCompute() {
844                  FibAction f = new FibAction(8);
845                  f.completeExceptionally(new FJException());
846                  try {
# Line 856 | Line 858 | public class RecursiveActionTest extends
858       */
859      public void testComplete() {
860          RecursiveAction a = new CheckedRecursiveAction() {
861 <            public void realCompute() {
861 >            protected void realCompute() {
862                  FibAction f = new FibAction(8);
863                  f.complete(null);
864                  assertNull(f.invoke());
# Line 871 | Line 873 | public class RecursiveActionTest extends
873       */
874      public void testInvokeAll2() {
875          RecursiveAction a = new CheckedRecursiveAction() {
876 <            public void realCompute() {
876 >            protected void realCompute() {
877                  FibAction f = new FibAction(8);
878                  FibAction g = new FibAction(9);
879                  invokeAll(f, g);
# Line 888 | Line 890 | public class RecursiveActionTest extends
890       */
891      public void testInvokeAll1() {
892          RecursiveAction a = new CheckedRecursiveAction() {
893 <            public void realCompute() {
893 >            protected void realCompute() {
894                  FibAction f = new FibAction(8);
895                  invokeAll(f);
896                  checkCompletedNormally(f);
# Line 902 | Line 904 | public class RecursiveActionTest extends
904       */
905      public void testInvokeAll3() {
906          RecursiveAction a = new CheckedRecursiveAction() {
907 <            public void realCompute() {
907 >            protected void realCompute() {
908                  FibAction f = new FibAction(8);
909                  FibAction g = new FibAction(9);
910                  FibAction h = new FibAction(7);
# Line 925 | Line 927 | public class RecursiveActionTest extends
927       */
928      public void testInvokeAllCollection() {
929          RecursiveAction a = new CheckedRecursiveAction() {
930 <            public void realCompute() {
930 >            protected void realCompute() {
931                  FibAction f = new FibAction(8);
932                  FibAction g = new FibAction(9);
933                  FibAction h = new FibAction(7);
# Line 952 | Line 954 | public class RecursiveActionTest extends
954       */
955      public void testInvokeAllNPE() {
956          RecursiveAction a = new CheckedRecursiveAction() {
957 <            public void realCompute() {
957 >            protected void realCompute() {
958                  FibAction f = new FibAction(8);
959                  FibAction g = new FibAction(9);
960                  FibAction h = null;
# Line 969 | Line 971 | public class RecursiveActionTest extends
971       */
972      public void testAbnormalInvokeAll2() {
973          RecursiveAction a = new CheckedRecursiveAction() {
974 <            public void realCompute() {
974 >            protected void realCompute() {
975                  FibAction f = new FibAction(8);
976                  FailingFibAction g = new FailingFibAction(9);
977                  try {
# Line 987 | Line 989 | public class RecursiveActionTest extends
989       */
990      public void testAbnormalInvokeAll1() {
991          RecursiveAction a = new CheckedRecursiveAction() {
992 <            public void realCompute() {
992 >            protected void realCompute() {
993                  FailingFibAction g = new FailingFibAction(9);
994                  try {
995                      invokeAll(g);
# Line 1004 | Line 1006 | public class RecursiveActionTest extends
1006       */
1007      public void testAbnormalInvokeAll3() {
1008          RecursiveAction a = new CheckedRecursiveAction() {
1009 <            public void realCompute() {
1009 >            protected void realCompute() {
1010                  FibAction f = new FibAction(8);
1011                  FailingFibAction g = new FailingFibAction(9);
1012                  FibAction h = new FibAction(7);
# Line 1023 | Line 1025 | public class RecursiveActionTest extends
1025       */
1026      public void testAbnormalInvokeAllCollection() {
1027          RecursiveAction a = new CheckedRecursiveAction() {
1028 <            public void realCompute() {
1028 >            protected void realCompute() {
1029                  FailingFibAction f = new FailingFibAction(8);
1030                  FibAction g = new FibAction(9);
1031                  FibAction h = new FibAction(7);
# Line 1047 | Line 1049 | public class RecursiveActionTest extends
1049       */
1050      public void testTryUnfork() {
1051          RecursiveAction a = new CheckedRecursiveAction() {
1052 <            public void realCompute() {
1052 >            protected void realCompute() {
1053                  FibAction g = new FibAction(9);
1054                  assertSame(g, g.fork());
1055                  FibAction f = new FibAction(8);
# Line 1066 | Line 1068 | public class RecursiveActionTest extends
1068       */
1069      public void testGetSurplusQueuedTaskCount() {
1070          RecursiveAction a = new CheckedRecursiveAction() {
1071 <            public void realCompute() {
1071 >            protected void realCompute() {
1072                  FibAction h = new FibAction(7);
1073                  assertSame(h, h.fork());
1074                  FibAction g = new FibAction(9);
# Line 1088 | Line 1090 | public class RecursiveActionTest extends
1090       */
1091      public void testPeekNextLocalTask() {
1092          RecursiveAction a = new CheckedRecursiveAction() {
1093 <            public void realCompute() {
1093 >            protected void realCompute() {
1094                  FibAction g = new FibAction(9);
1095                  assertSame(g, g.fork());
1096                  FibAction f = new FibAction(8);
# Line 1109 | Line 1111 | public class RecursiveActionTest extends
1111       */
1112      public void testPollNextLocalTask() {
1113          RecursiveAction a = new CheckedRecursiveAction() {
1114 <            public void realCompute() {
1114 >            protected void realCompute() {
1115                  FibAction g = new FibAction(9);
1116                  assertSame(g, g.fork());
1117                  FibAction f = new FibAction(8);
# Line 1127 | Line 1129 | public class RecursiveActionTest extends
1129       */
1130      public void testPollTask() {
1131          RecursiveAction a = new CheckedRecursiveAction() {
1132 <            public void realCompute() {
1132 >            protected void realCompute() {
1133                  FibAction g = new FibAction(9);
1134                  assertSame(g, g.fork());
1135                  FibAction f = new FibAction(8);
# Line 1145 | Line 1147 | public class RecursiveActionTest extends
1147       */
1148      public void testPeekNextLocalTaskAsync() {
1149          RecursiveAction a = new CheckedRecursiveAction() {
1150 <            public void realCompute() {
1150 >            protected void realCompute() {
1151                  FibAction g = new FibAction(9);
1152                  assertSame(g, g.fork());
1153                  FibAction f = new FibAction(8);
# Line 1165 | Line 1167 | public class RecursiveActionTest extends
1167       */
1168      public void testPollNextLocalTaskAsync() {
1169          RecursiveAction a = new CheckedRecursiveAction() {
1170 <            public void realCompute() {
1170 >            protected void realCompute() {
1171                  FibAction g = new FibAction(9);
1172                  assertSame(g, g.fork());
1173                  FibAction f = new FibAction(8);
# Line 1184 | Line 1186 | public class RecursiveActionTest extends
1186       */
1187      public void testPollTaskAsync() {
1188          RecursiveAction a = new CheckedRecursiveAction() {
1189 <            public void realCompute() {
1189 >            protected void realCompute() {
1190                  FibAction g = new FibAction(9);
1191                  assertSame(g, g.fork());
1192                  FibAction f = new FibAction(8);
# Line 1197 | Line 1199 | public class RecursiveActionTest extends
1199          testInvokeOnPool(asyncSingletonPool(), a);
1200      }
1201  
1202 +    /** Demo from RecursiveAction javadoc */
1203      static class SortTask extends RecursiveAction {
1204          final long[] array; final int lo, hi;
1205          SortTask(long[] array, int lo, int hi) {
1206              this.array = array; this.lo = lo; this.hi = hi;
1207          }
1208 <        final static int THRESHOLD = 100;
1208 >        SortTask(long[] array) { this(array, 0, array.length); }
1209          protected void compute() {
1210              if (hi - lo < THRESHOLD)
1211 <                sequentiallySort(array, lo, hi);
1211 >                sortSequentially(lo, hi);
1212              else {
1213                  int mid = (lo + hi) >>> 1;
1214                  invokeAll(new SortTask(array, lo, mid),
1215                            new SortTask(array, mid, hi));
1216 <                merge(array, lo, mid, hi);
1216 >                merge(lo, mid, hi);
1217              }
1218          }
1219 <        static void sequentiallySort(long[] array, int lo, int hi) {
1219 >        // implementation details follow:
1220 >        static final int THRESHOLD = 100;
1221 >        void sortSequentially(int lo, int hi) {
1222              Arrays.sort(array, lo, hi);
1223          }
1224 <        static void merge(long[] array, int lo, int mid, int hi) {
1225 <            int n = hi - lo;
1226 <            long[] buf = new long[n];
1227 <            int a = lo, b = mid;
1228 <            for (int i = 0; i < n; i++)
1224 <                buf[i] = (b == hi || (a < mid && array[a] < array[b])) ?
1225 <                    array[a++] : array[b++];
1226 <            System.arraycopy(buf, 0, array, lo, n);
1224 >        void merge(int lo, int mid, int hi) {
1225 >            long[] buf = Arrays.copyOfRange(array, lo, mid);
1226 >            for (int i = 0, j = lo, k = mid; i < buf.length; j++)
1227 >                array[j] = (k == hi || buf[i] < array[k]) ?
1228 >                    buf[i++] : array[k++];
1229          }
1230      }
1231  
# Line 1232 | Line 1234 | public class RecursiveActionTest extends
1234       */
1235      public void testSortTaskDemo() {
1236          ThreadLocalRandom rnd = ThreadLocalRandom.current();
1237 <        long[] array = new long[1000];
1237 >        long[] array = new long[1007];
1238          for (int i = 0; i < array.length; i++)
1239              array[i] = rnd.nextLong();
1240          long[] arrayClone = array.clone();
1241 <        testInvokeOnPool(mainPool(),
1240 <                         new SortTask(array, 0, array.length));
1241 >        testInvokeOnPool(mainPool(), new SortTask(array));
1242          Arrays.sort(arrayClone);
1243          assertTrue(Arrays.equals(array, arrayClone));
1244      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines