16 |
|
* <p><b>Sample Usages.</b> Here is a sketch of a ForkJoin sort that |
17 |
|
* sorts a given {@code long[]} array: |
18 |
|
* |
19 |
< |
* <pre> |
19 |
> |
* <pre> {@code |
20 |
|
* class SortTask extends RecursiveAction { |
21 |
|
* final long[] array; final int lo; final int hi; |
22 |
|
* SortTask(long[] array, int lo, int hi) { |
23 |
|
* this.array = array; this.lo = lo; this.hi = hi; |
24 |
|
* } |
25 |
|
* protected void compute() { |
26 |
< |
* if (hi - lo < THRESHOLD) |
26 |
> |
* if (hi - lo < THRESHOLD) |
27 |
|
* sequentiallySort(array, lo, hi); |
28 |
|
* else { |
29 |
< |
* int mid = (lo + hi) >>> 1; |
29 |
> |
* int mid = (lo + hi) >>> 1; |
30 |
|
* invokeAll(new SortTask(array, lo, mid), |
31 |
|
* new SortTask(array, mid, hi)); |
32 |
|
* merge(array, lo, hi); |
33 |
|
* } |
34 |
|
* } |
35 |
< |
* } |
36 |
< |
* </pre> |
35 |
> |
* }}</pre> |
36 |
|
* |
37 |
|
* You could then sort anArray by creating {@code new SortTask(anArray, 0, |
38 |
|
* anArray.length-1) } and invoking it in a ForkJoinPool. |
39 |
|
* As a more concrete simple example, the following task increments |
40 |
|
* each element of an array: |
41 |
< |
* <pre> |
41 |
> |
* <pre> {@code |
42 |
|
* class IncrementTask extends RecursiveAction { |
43 |
|
* final long[] array; final int lo; final int hi; |
44 |
|
* IncrementTask(long[] array, int lo, int hi) { |
45 |
|
* this.array = array; this.lo = lo; this.hi = hi; |
46 |
|
* } |
47 |
|
* protected void compute() { |
48 |
< |
* if (hi - lo < THRESHOLD) { |
49 |
< |
* for (int i = lo; i < hi; ++i) |
48 |
> |
* if (hi - lo < THRESHOLD) { |
49 |
> |
* for (int i = lo; i < hi; ++i) |
50 |
|
* array[i]++; |
51 |
|
* } |
52 |
|
* else { |
53 |
< |
* int mid = (lo + hi) >>> 1; |
53 |
> |
* int mid = (lo + hi) >>> 1; |
54 |
|
* invokeAll(new IncrementTask(array, lo, mid), |
55 |
|
* new IncrementTask(array, mid, hi)); |
56 |
|
* } |
57 |
|
* } |
58 |
< |
* } |
60 |
< |
* </pre> |
61 |
< |
* |
58 |
> |
* }}</pre> |
59 |
|
* |
60 |
|
* <p>The following example illustrates some refinements and idioms |
61 |
|
* that may lead to better performance: RecursiveActions need not be |
68 |
|
* potential excess partitioning by directly performing leaf actions |
69 |
|
* on unstolen tasks rather than further subdividing. |
70 |
|
* |
71 |
< |
* <pre> |
71 |
> |
* <pre> {@code |
72 |
|
* double sumOfSquares(ForkJoinPool pool, double[] array) { |
73 |
|
* int n = array.length; |
74 |
|
* int seqSize = 1 + n / (8 * pool.getParallelism()); |
89 |
|
* |
90 |
|
* double atLeaf(int l, int r) { |
91 |
|
* double sum = 0; |
92 |
< |
* for (int i = l; i < h; ++i) // perform leftmost base step |
92 |
> |
* for (int i = l; i < h; ++i) // perform leftmost base step |
93 |
|
* sum += array[i] * array[i]; |
94 |
|
* return sum; |
95 |
|
* } |
98 |
|
* int l = lo; |
99 |
|
* int h = hi; |
100 |
|
* Applyer right = null; |
101 |
< |
* while (h - l > 1 && |
102 |
< |
* ForkJoinWorkerThread.getEstimatedSurplusTaskCount() <= 3) { |
103 |
< |
* int mid = (l + h) >>> 1; |
101 |
> |
* while (h - l > 1 && |
102 |
> |
* ForkJoinWorkerThread.getEstimatedSurplusTaskCount() <= 3) { |
103 |
> |
* int mid = (l + h) >>> 1; |
104 |
|
* right = new Applyer(array, mid, h, seqSize, right); |
105 |
|
* right.fork(); |
106 |
|
* h = mid; |
117 |
|
* } |
118 |
|
* result = sum; |
119 |
|
* } |
120 |
< |
* } |
124 |
< |
* </pre> |
120 |
> |
* }}</pre> |
121 |
|
*/ |
122 |
|
public abstract class RecursiveAction extends ForkJoinTask<Void> { |
123 |
|
|