13 |
|
* only valid value of type {@code Void}, methods such as {@code join} |
14 |
|
* always return {@code null} upon completion. |
15 |
|
* |
16 |
< |
* <p><b>Sample Usages.</b> Here is a sketch of a ForkJoin sort that |
17 |
< |
* sorts a given {@code long[]} array: |
16 |
> |
* <p><b>Sample Usages.</b> Here is a simple but complete ForkJoin |
17 |
> |
* sort that sorts a given {@code long[]} array: |
18 |
|
* |
19 |
|
* <pre> {@code |
20 |
< |
* class SortTask extends RecursiveAction { |
20 |
> |
* static class SortTask extends RecursiveAction { |
21 |
|
* final long[] array; final int lo, hi; |
22 |
|
* SortTask(long[] array, int lo, int hi) { |
23 |
|
* this.array = array; this.lo = lo; this.hi = hi; |
24 |
|
* } |
25 |
+ |
* SortTask(long[] array) { this(array, 0, array.length); } |
26 |
|
* protected void compute() { |
27 |
|
* if (hi - lo < THRESHOLD) |
28 |
< |
* sequentiallySort(array, lo, hi); |
28 |
> |
* sortSequentially(lo, hi); |
29 |
|
* else { |
30 |
|
* int mid = (lo + hi) >>> 1; |
31 |
|
* invokeAll(new SortTask(array, lo, mid), |
32 |
|
* new SortTask(array, mid, hi)); |
33 |
< |
* merge(array, lo, mid, hi); |
33 |
> |
* merge(lo, mid, hi); |
34 |
|
* } |
35 |
|
* } |
36 |
+ |
* // implementation details follow: |
37 |
+ |
* final static int THRESHOLD = 1000; |
38 |
+ |
* void sortSequentially(int lo, int hi) { |
39 |
+ |
* Arrays.sort(array, lo, hi); |
40 |
+ |
* } |
41 |
+ |
* void merge(int lo, int mid, int hi) { |
42 |
+ |
* long[] buf = Arrays.copyOfRange(array, lo, mid); |
43 |
+ |
* for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
44 |
+ |
* array[j] = (k == hi || buf[i] < array[k]) ? |
45 |
+ |
* buf[i++] : array[k++]; |
46 |
+ |
* } |
47 |
|
* }}</pre> |
48 |
|
* |
49 |
|
* You could then sort {@code anArray} by creating {@code new |
50 |
< |
* SortTask(anArray, 0, anArray.length) } and invoking it in a |
51 |
< |
* ForkJoinPool. As a more concrete simple example, the following |
52 |
< |
* task increments each element of an array: |
50 |
> |
* SortTask(anArray)} and invoking it in a ForkJoinPool. As a more |
51 |
> |
* concrete simple example, the following task increments each element |
52 |
> |
* of an array: |
53 |
|
* <pre> {@code |
54 |
|
* class IncrementTask extends RecursiveAction { |
55 |
|
* final long[] array; final int lo, hi; |