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.38 by jsr166, Fri Jan 18 04:23:28 2013 UTC

# Line 1197 | Line 1197 | public class RecursiveActionTest extends
1197          testInvokeOnPool(asyncSingletonPool(), a);
1198      }
1199  
1200 +    /** Demo from RecursiveAction javadoc */
1201      static class SortTask extends RecursiveAction {
1202          final long[] array; final int lo, hi;
1203          SortTask(long[] array, int lo, int hi) {
1204              this.array = array; this.lo = lo; this.hi = hi;
1205          }
1206 <        final static int THRESHOLD = 100;
1206 >        SortTask(long[] array) { this(array, 0, array.length); }
1207          protected void compute() {
1208              if (hi - lo < THRESHOLD)
1209 <                sequentiallySort(array, lo, hi);
1209 >                sortSequentially(lo, hi);
1210              else {
1211                  int mid = (lo + hi) >>> 1;
1212                  invokeAll(new SortTask(array, lo, mid),
1213                            new SortTask(array, mid, hi));
1214 <                merge(array, lo, mid, hi);
1214 >                merge(lo, mid, hi);
1215              }
1216          }
1217 <        static void sequentiallySort(long[] array, int lo, int hi) {
1217 >        // implementation details follow:
1218 >        static final int THRESHOLD = 100;
1219 >        void sortSequentially(int lo, int hi) {
1220              Arrays.sort(array, lo, hi);
1221          }
1222 <        static void merge(long[] array, int lo, int mid, int hi) {
1223 <            int n = hi - lo;
1224 <            long[] buf = new long[n];
1225 <            int a = lo, b = mid;
1226 <            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);
1222 >        void merge(int lo, int mid, int hi) {
1223 >            long[] buf = Arrays.copyOfRange(array, lo, mid);
1224 >            for (int i = 0, j = lo, k = mid; i < buf.length; j++)
1225 >                array[j] = (k == hi || buf[i] < array[k]) ?
1226 >                    buf[i++] : array[k++];
1227          }
1228      }
1229  
# Line 1232 | Line 1232 | public class RecursiveActionTest extends
1232       */
1233      public void testSortTaskDemo() {
1234          ThreadLocalRandom rnd = ThreadLocalRandom.current();
1235 <        long[] array = new long[1000];
1235 >        long[] array = new long[1007];
1236          for (int i = 0; i < array.length; i++)
1237              array[i] = rnd.nextLong();
1238          long[] arrayClone = array.clone();
1239 <        testInvokeOnPool(mainPool(),
1240 <                         new SortTask(array, 0, array.length));
1239 >        testInvokeOnPool(mainPool(), new SortTask(array));
1240          Arrays.sort(arrayClone);
1241          assertTrue(Arrays.equals(array, arrayClone));
1242      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines