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.41 by dl, Tue Jan 20 17:02:26 2015 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 > import java.util.concurrent.TimeUnit;
21 >
22 > import junit.framework.Test;
23 > import junit.framework.TestSuite;
24  
25   public class RecursiveActionTest extends JSR166TestCase {
26  
# Line 173 | Line 176 | public class RecursiveActionTest extends
176          final int number;
177          int result;
178          FibAction(int n) { number = n; }
179 <        public void realCompute() {
179 >        protected void realCompute() {
180              int n = number;
181              if (n <= 1)
182                  result = n;
# Line 211 | Line 214 | public class RecursiveActionTest extends
214       */
215      public void testInvoke() {
216          RecursiveAction a = new CheckedRecursiveAction() {
217 <            public void realCompute() {
217 >            protected void realCompute() {
218                  FibAction f = new FibAction(8);
219                  assertNull(f.invoke());
220                  assertEquals(21, f.result);
# Line 227 | Line 230 | public class RecursiveActionTest extends
230       */
231      public void testQuietlyInvoke() {
232          RecursiveAction a = new CheckedRecursiveAction() {
233 <            public void realCompute() {
233 >            protected void realCompute() {
234                  FibAction f = new FibAction(8);
235                  f.quietlyInvoke();
236                  assertEquals(21, f.result);
# Line 241 | Line 244 | public class RecursiveActionTest extends
244       */
245      public void testForkJoin() {
246          RecursiveAction a = new CheckedRecursiveAction() {
247 <            public void realCompute() {
247 >            protected void realCompute() {
248                  FibAction f = new FibAction(8);
249                  assertSame(f, f.fork());
250                  assertNull(f.join());
# Line 256 | Line 259 | public class RecursiveActionTest extends
259       */
260      public void testJoinIgnoresInterrupts() {
261          RecursiveAction a = new CheckedRecursiveAction() {
262 <            public void realCompute() {
262 >            protected void realCompute() {
263                  FibAction f = new FibAction(8);
264                  final Thread myself = Thread.currentThread();
265  
# Line 336 | Line 339 | public class RecursiveActionTest extends
339          final SynchronousQueue<FibAction[]> sq =
340              new SynchronousQueue<FibAction[]>();
341          RecursiveAction a = new CheckedRecursiveAction() {
342 <            public void realCompute() throws InterruptedException {
342 >            protected void realCompute() throws InterruptedException {
343                  FibAction[] fibActions = new FibAction[6];
344                  for (int i = 0; i < fibActions.length; i++)
345                      fibActions[i] = new FibAction(8);
# Line 436 | Line 439 | public class RecursiveActionTest extends
439       */
440      public void testForkGet() {
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());
# Line 451 | Line 454 | public class RecursiveActionTest extends
454       */
455      public void testForkTimedGet() {
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                  assertNull(f.get(5L, SECONDS));
# Line 466 | Line 469 | public class RecursiveActionTest extends
469       */
470      public void testForkTimedGetNPE() {
471          RecursiveAction a = new CheckedRecursiveAction() {
472 <            public void realCompute() throws Exception {
472 >            protected void realCompute() throws Exception {
473                  FibAction f = new FibAction(8);
474                  assertSame(f, f.fork());
475                  try {
# Line 482 | Line 485 | public class RecursiveActionTest extends
485       */
486      public void testForkQuietlyJoin() {
487          RecursiveAction a = new CheckedRecursiveAction() {
488 <            public void realCompute() {
488 >            protected void realCompute() {
489                  FibAction f = new FibAction(8);
490                  assertSame(f, f.fork());
491                  f.quietlyJoin();
# Line 498 | Line 501 | public class RecursiveActionTest extends
501       */
502      public void testForkHelpQuiesce() {
503          RecursiveAction a = new CheckedRecursiveAction() {
504 <            public void realCompute() {
504 >            protected void realCompute() {
505                  FibAction f = new FibAction(8);
506                  assertSame(f, f.fork());
507                  helpQuiesce();
508 +                while (!f.isDone()) // wait out race
509 +                    ;
510                  assertEquals(21, f.result);
511                  assertEquals(0, getQueuedTaskCount());
512                  checkCompletedNormally(f);
# Line 514 | Line 519 | public class RecursiveActionTest extends
519       */
520      public void testAbnormalInvoke() {
521          RecursiveAction a = new CheckedRecursiveAction() {
522 <            public void realCompute() {
522 >            protected void realCompute() {
523                  FailingFibAction f = new FailingFibAction(8);
524                  try {
525                      f.invoke();
# Line 531 | Line 536 | public class RecursiveActionTest extends
536       */
537      public void testAbnormalQuietlyInvoke() {
538          RecursiveAction a = new CheckedRecursiveAction() {
539 <            public void realCompute() {
539 >            protected void realCompute() {
540                  FailingFibAction f = new FailingFibAction(8);
541                  f.quietlyInvoke();
542                  assertTrue(f.getException() instanceof FJException);
# Line 545 | Line 550 | public class RecursiveActionTest extends
550       */
551      public void testAbnormalForkJoin() {
552          RecursiveAction a = new CheckedRecursiveAction() {
553 <            public void realCompute() {
553 >            protected void realCompute() {
554                  FailingFibAction f = new FailingFibAction(8);
555                  assertSame(f, f.fork());
556                  try {
# Line 563 | Line 568 | public class RecursiveActionTest extends
568       */
569      public void testAbnormalForkGet() {
570          RecursiveAction a = new CheckedRecursiveAction() {
571 <            public void realCompute() throws Exception {
571 >            protected void realCompute() throws Exception {
572                  FailingFibAction f = new FailingFibAction(8);
573                  assertSame(f, f.fork());
574                  try {
# Line 583 | Line 588 | public class RecursiveActionTest extends
588       */
589      public void testAbnormalForkTimedGet() {
590          RecursiveAction a = new CheckedRecursiveAction() {
591 <            public void realCompute() throws Exception {
591 >            protected void realCompute() throws Exception {
592                  FailingFibAction f = new FailingFibAction(8);
593                  assertSame(f, f.fork());
594                  try {
# Line 603 | Line 608 | public class RecursiveActionTest extends
608       */
609      public void testAbnormalForkQuietlyJoin() {
610          RecursiveAction a = new CheckedRecursiveAction() {
611 <            public void realCompute() {
611 >            protected void realCompute() {
612                  FailingFibAction f = new FailingFibAction(8);
613                  assertSame(f, f.fork());
614                  f.quietlyJoin();
# Line 618 | Line 623 | public class RecursiveActionTest extends
623       */
624      public void testCancelledInvoke() {
625          RecursiveAction a = new CheckedRecursiveAction() {
626 <            public void realCompute() {
626 >            protected void realCompute() {
627                  FibAction f = new FibAction(8);
628                  assertTrue(f.cancel(true));
629                  try {
# Line 636 | Line 641 | public class RecursiveActionTest extends
641       */
642      public void testCancelledForkJoin() {
643          RecursiveAction a = new CheckedRecursiveAction() {
644 <            public void realCompute() {
644 >            protected void realCompute() {
645                  FibAction f = new FibAction(8);
646                  assertTrue(f.cancel(true));
647                  assertSame(f, f.fork());
# Line 655 | Line 660 | public class RecursiveActionTest extends
660       */
661      public void testCancelledForkGet() {
662          RecursiveAction a = new CheckedRecursiveAction() {
663 <            public void realCompute() throws Exception {
663 >            protected void realCompute() throws Exception {
664                  FibAction f = new FibAction(8);
665                  assertTrue(f.cancel(true));
666                  assertSame(f, f.fork());
# Line 674 | Line 679 | public class RecursiveActionTest extends
679       */
680      public void testCancelledForkTimedGet() {
681          RecursiveAction a = new CheckedRecursiveAction() {
682 <            public void realCompute() throws Exception {
682 >            protected void realCompute() throws Exception {
683                  FibAction f = new FibAction(8);
684                  assertTrue(f.cancel(true));
685                  assertSame(f, f.fork());
# Line 693 | Line 698 | public class RecursiveActionTest extends
698       */
699      public void testCancelledForkQuietlyJoin() {
700          RecursiveAction a = new CheckedRecursiveAction() {
701 <            public void realCompute() {
701 >            protected void realCompute() {
702                  FibAction f = new FibAction(8);
703                  assertTrue(f.cancel(true));
704                  assertSame(f, f.fork());
# Line 709 | Line 714 | public class RecursiveActionTest extends
714      public void testGetPool() {
715          final ForkJoinPool mainPool = mainPool();
716          RecursiveAction a = new CheckedRecursiveAction() {
717 <            public void realCompute() {
717 >            protected void realCompute() {
718                  assertSame(mainPool, getPool());
719              }};
720          testInvokeOnPool(mainPool, a);
# Line 720 | Line 725 | public class RecursiveActionTest extends
725       */
726      public void testGetPool2() {
727          RecursiveAction a = new CheckedRecursiveAction() {
728 <            public void realCompute() {
728 >            protected void realCompute() {
729                  assertNull(getPool());
730              }};
731          assertNull(a.invoke());
# Line 731 | Line 736 | public class RecursiveActionTest extends
736       */
737      public void testInForkJoinPool() {
738          RecursiveAction a = new CheckedRecursiveAction() {
739 <            public void realCompute() {
739 >            protected void realCompute() {
740                  assertTrue(inForkJoinPool());
741              }};
742          testInvokeOnPool(mainPool(), a);
# Line 742 | Line 747 | public class RecursiveActionTest extends
747       */
748      public void testInForkJoinPool2() {
749          RecursiveAction a = new CheckedRecursiveAction() {
750 <            public void realCompute() {
750 >            protected void realCompute() {
751                  assertFalse(inForkJoinPool());
752              }};
753          assertNull(a.invoke());
# Line 754 | Line 759 | public class RecursiveActionTest extends
759      public void testWorkerGetPool() {
760          final ForkJoinPool mainPool = mainPool();
761          RecursiveAction a = new CheckedRecursiveAction() {
762 <            public void realCompute() {
762 >            protected void realCompute() {
763                  ForkJoinWorkerThread w =
764                      (ForkJoinWorkerThread) Thread.currentThread();
765                  assertSame(mainPool, w.getPool());
# Line 768 | Line 773 | public class RecursiveActionTest extends
773      public void testWorkerGetPoolIndex() {
774          final ForkJoinPool mainPool = mainPool();
775          RecursiveAction a = new CheckedRecursiveAction() {
776 <            public void realCompute() {
776 >            protected void realCompute() {
777                  ForkJoinWorkerThread w =
778                      (ForkJoinWorkerThread) Thread.currentThread();
779                  assertTrue(w.getPoolIndex() >= 0);
# Line 783 | Line 788 | public class RecursiveActionTest extends
788       */
789      public void testSetRawResult() {
790          RecursiveAction a = new CheckedRecursiveAction() {
791 <            public void realCompute() {
791 >            protected void realCompute() {
792                  setRawResult(null);
793                  assertNull(getRawResult());
794              }};
# Line 795 | Line 800 | public class RecursiveActionTest extends
800       */
801      public void testReinitialize() {
802          RecursiveAction a = new CheckedRecursiveAction() {
803 <            public void realCompute() {
803 >            protected void realCompute() {
804                  FibAction f = new FibAction(8);
805                  checkNotDone(f);
806  
# Line 815 | Line 820 | public class RecursiveActionTest extends
820       */
821      public void testReinitializeAbnormal() {
822          RecursiveAction a = new CheckedRecursiveAction() {
823 <            public void realCompute() {
823 >            protected void realCompute() {
824                  FailingFibAction f = new FailingFibAction(8);
825                  checkNotDone(f);
826  
# Line 838 | Line 843 | public class RecursiveActionTest extends
843       */
844      public void testCompleteExceptionally() {
845          RecursiveAction a = new CheckedRecursiveAction() {
846 <            public void realCompute() {
846 >            protected void realCompute() {
847                  FibAction f = new FibAction(8);
848                  f.completeExceptionally(new FJException());
849                  try {
# Line 856 | Line 861 | public class RecursiveActionTest extends
861       */
862      public void testComplete() {
863          RecursiveAction a = new CheckedRecursiveAction() {
864 <            public void realCompute() {
864 >            protected void realCompute() {
865                  FibAction f = new FibAction(8);
866                  f.complete(null);
867                  assertNull(f.invoke());
# Line 871 | Line 876 | public class RecursiveActionTest extends
876       */
877      public void testInvokeAll2() {
878          RecursiveAction a = new CheckedRecursiveAction() {
879 <            public void realCompute() {
879 >            protected void realCompute() {
880                  FibAction f = new FibAction(8);
881                  FibAction g = new FibAction(9);
882                  invokeAll(f, g);
# Line 888 | Line 893 | public class RecursiveActionTest extends
893       */
894      public void testInvokeAll1() {
895          RecursiveAction a = new CheckedRecursiveAction() {
896 <            public void realCompute() {
896 >            protected void realCompute() {
897                  FibAction f = new FibAction(8);
898                  invokeAll(f);
899                  checkCompletedNormally(f);
# Line 902 | Line 907 | public class RecursiveActionTest extends
907       */
908      public void testInvokeAll3() {
909          RecursiveAction a = new CheckedRecursiveAction() {
910 <            public void realCompute() {
910 >            protected void realCompute() {
911                  FibAction f = new FibAction(8);
912                  FibAction g = new FibAction(9);
913                  FibAction h = new FibAction(7);
# Line 925 | Line 930 | public class RecursiveActionTest extends
930       */
931      public void testInvokeAllCollection() {
932          RecursiveAction a = new CheckedRecursiveAction() {
933 <            public void realCompute() {
933 >            protected void realCompute() {
934                  FibAction f = new FibAction(8);
935                  FibAction g = new FibAction(9);
936                  FibAction h = new FibAction(7);
# Line 952 | Line 957 | public class RecursiveActionTest extends
957       */
958      public void testInvokeAllNPE() {
959          RecursiveAction a = new CheckedRecursiveAction() {
960 <            public void realCompute() {
960 >            protected void realCompute() {
961                  FibAction f = new FibAction(8);
962                  FibAction g = new FibAction(9);
963                  FibAction h = null;
# Line 969 | Line 974 | public class RecursiveActionTest extends
974       */
975      public void testAbnormalInvokeAll2() {
976          RecursiveAction a = new CheckedRecursiveAction() {
977 <            public void realCompute() {
977 >            protected void realCompute() {
978                  FibAction f = new FibAction(8);
979                  FailingFibAction g = new FailingFibAction(9);
980                  try {
# Line 987 | Line 992 | public class RecursiveActionTest extends
992       */
993      public void testAbnormalInvokeAll1() {
994          RecursiveAction a = new CheckedRecursiveAction() {
995 <            public void realCompute() {
995 >            protected void realCompute() {
996                  FailingFibAction g = new FailingFibAction(9);
997                  try {
998                      invokeAll(g);
# Line 1004 | Line 1009 | public class RecursiveActionTest extends
1009       */
1010      public void testAbnormalInvokeAll3() {
1011          RecursiveAction a = new CheckedRecursiveAction() {
1012 <            public void realCompute() {
1012 >            protected void realCompute() {
1013                  FibAction f = new FibAction(8);
1014                  FailingFibAction g = new FailingFibAction(9);
1015                  FibAction h = new FibAction(7);
# Line 1023 | Line 1028 | public class RecursiveActionTest extends
1028       */
1029      public void testAbnormalInvokeAllCollection() {
1030          RecursiveAction a = new CheckedRecursiveAction() {
1031 <            public void realCompute() {
1031 >            protected void realCompute() {
1032                  FailingFibAction f = new FailingFibAction(8);
1033                  FibAction g = new FibAction(9);
1034                  FibAction h = new FibAction(7);
# Line 1047 | Line 1052 | public class RecursiveActionTest extends
1052       */
1053      public void testTryUnfork() {
1054          RecursiveAction a = new CheckedRecursiveAction() {
1055 <            public void realCompute() {
1055 >            protected void realCompute() {
1056                  FibAction g = new FibAction(9);
1057                  assertSame(g, g.fork());
1058                  FibAction f = new FibAction(8);
# Line 1066 | Line 1071 | public class RecursiveActionTest extends
1071       */
1072      public void testGetSurplusQueuedTaskCount() {
1073          RecursiveAction a = new CheckedRecursiveAction() {
1074 <            public void realCompute() {
1074 >            protected void realCompute() {
1075                  FibAction h = new FibAction(7);
1076                  assertSame(h, h.fork());
1077                  FibAction g = new FibAction(9);
# Line 1088 | Line 1093 | public class RecursiveActionTest extends
1093       */
1094      public void testPeekNextLocalTask() {
1095          RecursiveAction a = new CheckedRecursiveAction() {
1096 <            public void realCompute() {
1096 >            protected void realCompute() {
1097                  FibAction g = new FibAction(9);
1098                  assertSame(g, g.fork());
1099                  FibAction f = new FibAction(8);
# Line 1109 | Line 1114 | public class RecursiveActionTest extends
1114       */
1115      public void testPollNextLocalTask() {
1116          RecursiveAction a = new CheckedRecursiveAction() {
1117 <            public void realCompute() {
1117 >            protected void realCompute() {
1118                  FibAction g = new FibAction(9);
1119                  assertSame(g, g.fork());
1120                  FibAction f = new FibAction(8);
# Line 1127 | Line 1132 | public class RecursiveActionTest extends
1132       */
1133      public void testPollTask() {
1134          RecursiveAction a = new CheckedRecursiveAction() {
1135 <            public void realCompute() {
1135 >            protected void realCompute() {
1136                  FibAction g = new FibAction(9);
1137                  assertSame(g, g.fork());
1138                  FibAction f = new FibAction(8);
# Line 1145 | Line 1150 | public class RecursiveActionTest extends
1150       */
1151      public void testPeekNextLocalTaskAsync() {
1152          RecursiveAction a = new CheckedRecursiveAction() {
1153 <            public void realCompute() {
1153 >            protected void realCompute() {
1154                  FibAction g = new FibAction(9);
1155                  assertSame(g, g.fork());
1156                  FibAction f = new FibAction(8);
# Line 1165 | Line 1170 | public class RecursiveActionTest extends
1170       */
1171      public void testPollNextLocalTaskAsync() {
1172          RecursiveAction a = new CheckedRecursiveAction() {
1173 <            public void realCompute() {
1173 >            protected void realCompute() {
1174                  FibAction g = new FibAction(9);
1175                  assertSame(g, g.fork());
1176                  FibAction f = new FibAction(8);
# Line 1184 | Line 1189 | public class RecursiveActionTest extends
1189       */
1190      public void testPollTaskAsync() {
1191          RecursiveAction a = new CheckedRecursiveAction() {
1192 <            public void realCompute() {
1192 >            protected void realCompute() {
1193                  FibAction g = new FibAction(9);
1194                  assertSame(g, g.fork());
1195                  FibAction f = new FibAction(8);
# Line 1197 | Line 1202 | public class RecursiveActionTest extends
1202          testInvokeOnPool(asyncSingletonPool(), a);
1203      }
1204  
1205 +    /** Demo from RecursiveAction javadoc */
1206      static class SortTask extends RecursiveAction {
1207          final long[] array; final int lo, hi;
1208          SortTask(long[] array, int lo, int hi) {
1209              this.array = array; this.lo = lo; this.hi = hi;
1210          }
1211 <        final static int THRESHOLD = 100;
1211 >        SortTask(long[] array) { this(array, 0, array.length); }
1212          protected void compute() {
1213              if (hi - lo < THRESHOLD)
1214 <                sequentiallySort(array, lo, hi);
1214 >                sortSequentially(lo, hi);
1215              else {
1216                  int mid = (lo + hi) >>> 1;
1217                  invokeAll(new SortTask(array, lo, mid),
1218                            new SortTask(array, mid, hi));
1219 <                merge(array, lo, mid, hi);
1219 >                merge(lo, mid, hi);
1220              }
1221          }
1222 <        static void sequentiallySort(long[] array, int lo, int hi) {
1222 >        // implementation details follow:
1223 >        static final int THRESHOLD = 100;
1224 >        void sortSequentially(int lo, int hi) {
1225              Arrays.sort(array, lo, hi);
1226          }
1227 <        static void merge(long[] array, int lo, int mid, int hi) {
1228 <            int n = hi - lo;
1229 <            long[] buf = new long[n];
1230 <            int a = lo, b = mid;
1231 <            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);
1227 >        void merge(int lo, int mid, int hi) {
1228 >            long[] buf = Arrays.copyOfRange(array, lo, mid);
1229 >            for (int i = 0, j = lo, k = mid; i < buf.length; j++)
1230 >                array[j] = (k == hi || buf[i] < array[k]) ?
1231 >                    buf[i++] : array[k++];
1232          }
1233      }
1234  
# Line 1232 | Line 1237 | public class RecursiveActionTest extends
1237       */
1238      public void testSortTaskDemo() {
1239          ThreadLocalRandom rnd = ThreadLocalRandom.current();
1240 <        long[] array = new long[1000];
1240 >        long[] array = new long[1007];
1241          for (int i = 0; i < array.length; i++)
1242              array[i] = rnd.nextLong();
1243          long[] arrayClone = array.clone();
1244 <        testInvokeOnPool(mainPool(),
1240 <                         new SortTask(array, 0, array.length));
1244 >        testInvokeOnPool(mainPool(), new SortTask(array));
1245          Arrays.sort(arrayClone);
1246          assertTrue(Arrays.equals(array, arrayClone));
1247      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines