139 |
|
* {@code tryComplete}) the pending count is set to one: |
140 |
|
* |
141 |
|
* <pre> {@code |
142 |
< |
* class ForEach<E> ... |
142 |
> |
* class ForEach<E> ... { |
143 |
> |
* ... |
144 |
|
* public void compute() { // version 2 |
145 |
|
* if (hi - lo >= 2) { |
146 |
|
* int mid = (lo + hi) >>> 1; |
154 |
|
* tryComplete(); |
155 |
|
* } |
156 |
|
* } |
157 |
< |
* }</pre> |
157 |
> |
* }}</pre> |
158 |
|
* |
159 |
|
* As a further improvement, notice that the left task need not even exist. |
160 |
|
* Instead of creating a new one, we can iterate using the original task, |
163 |
|
* {@code tryComplete()} can be replaced with {@link #propagateCompletion}. |
164 |
|
* |
165 |
|
* <pre> {@code |
166 |
< |
* class ForEach<E> ... |
166 |
> |
* class ForEach<E> ... { |
167 |
> |
* ... |
168 |
|
* public void compute() { // version 3 |
169 |
< |
* int l = lo, h = hi; |
169 |
> |
* int l = lo, h = hi; |
170 |
|
* while (h - l >= 2) { |
171 |
|
* int mid = (l + h) >>> 1; |
172 |
|
* addToPendingCount(1); |
177 |
|
* op.apply(array[l]); |
178 |
|
* propagateCompletion(); |
179 |
|
* } |
180 |
< |
* }</pre> |
180 |
> |
* }}</pre> |
181 |
|
* |
182 |
|
* Additional improvements of such classes might entail precomputing |
183 |
|
* pending counts so that they can be established in constructors, |
375 |
|
* // sample use: |
376 |
|
* PacketSender p = new PacketSender(); |
377 |
|
* new HeaderBuilder(p, ...).fork(); |
378 |
< |
* new BodyBuilder(p, ...).fork(); |
377 |
< |
* }</pre> |
378 |
> |
* new BodyBuilder(p, ...).fork();}</pre> |
379 |
|
* |
380 |
|
* @since 1.8 |
381 |
|
* @author Doug Lea |