author | dl |
Mon, 12 Sep 2016 13:14:49 -0700 | |
changeset 40817 | 4f5fb115676d |
parent 40815 | 196b358234d8 |
child 42319 | 0193886267c3 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
34 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
35 |
import static java.util.concurrent.TimeUnit.SECONDS; |
|
36 |
||
37 |
import java.util.HashSet; |
|
38 |
import java.util.concurrent.CancellationException; |
|
39 |
import java.util.concurrent.CountedCompleter; |
|
40 |
import java.util.concurrent.ExecutionException; |
|
41 |
import java.util.concurrent.ForkJoinPool; |
|
42 |
import java.util.concurrent.ForkJoinTask; |
|
40815
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
43 |
import java.util.concurrent.ThreadLocalRandom; |
35394 | 44 |
import java.util.concurrent.TimeoutException; |
45 |
import java.util.concurrent.atomic.AtomicInteger; |
|
46 |
import java.util.concurrent.atomic.AtomicReference; |
|
40815
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
47 |
import java.util.function.BiConsumer; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
48 |
import java.util.function.Consumer; |
35394 | 49 |
|
50 |
import junit.framework.Test; |
|
51 |
import junit.framework.TestSuite; |
|
52 |
||
53 |
public class CountedCompleterTest extends JSR166TestCase { |
|
54 |
||
55 |
public static void main(String[] args) { |
|
56 |
main(suite(), args); |
|
57 |
} |
|
58 |
||
59 |
public static Test suite() { |
|
60 |
return new TestSuite(CountedCompleterTest.class); |
|
61 |
} |
|
62 |
||
63 |
// Runs with "mainPool" use > 1 thread. singletonPool tests use 1 |
|
64 |
static final int mainPoolSize = |
|
65 |
Math.max(2, Runtime.getRuntime().availableProcessors()); |
|
66 |
||
67 |
private static ForkJoinPool mainPool() { |
|
68 |
return new ForkJoinPool(mainPoolSize); |
|
69 |
} |
|
70 |
||
71 |
private static ForkJoinPool singletonPool() { |
|
72 |
return new ForkJoinPool(1); |
|
73 |
} |
|
74 |
||
75 |
private static ForkJoinPool asyncSingletonPool() { |
|
76 |
return new ForkJoinPool(1, |
|
77 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
|
78 |
null, true); |
|
79 |
} |
|
80 |
||
81 |
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { |
|
82 |
try (PoolCleaner cleaner = cleaner(pool)) { |
|
83 |
assertFalse(a.isDone()); |
|
84 |
assertFalse(a.isCompletedNormally()); |
|
85 |
assertFalse(a.isCompletedAbnormally()); |
|
86 |
assertFalse(a.isCancelled()); |
|
87 |
assertNull(a.getException()); |
|
88 |
assertNull(a.getRawResult()); |
|
89 |
||
90 |
assertNull(pool.invoke(a)); |
|
91 |
||
92 |
assertTrue(a.isDone()); |
|
93 |
assertTrue(a.isCompletedNormally()); |
|
94 |
assertFalse(a.isCompletedAbnormally()); |
|
95 |
assertFalse(a.isCancelled()); |
|
96 |
assertNull(a.getException()); |
|
97 |
assertNull(a.getRawResult()); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
void checkNotDone(CountedCompleter a) { |
|
102 |
assertFalse(a.isDone()); |
|
103 |
assertFalse(a.isCompletedNormally()); |
|
104 |
assertFalse(a.isCompletedAbnormally()); |
|
105 |
assertFalse(a.isCancelled()); |
|
106 |
assertNull(a.getException()); |
|
107 |
assertNull(a.getRawResult()); |
|
108 |
||
109 |
try { |
|
110 |
a.get(0L, SECONDS); |
|
111 |
shouldThrow(); |
|
112 |
} catch (TimeoutException success) { |
|
113 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
114 |
} |
|
115 |
||
116 |
void checkCompletedNormally(CountedCompleter<?> a) { |
|
117 |
assertTrue(a.isDone()); |
|
118 |
assertFalse(a.isCancelled()); |
|
119 |
assertTrue(a.isCompletedNormally()); |
|
120 |
assertFalse(a.isCompletedAbnormally()); |
|
121 |
assertNull(a.getException()); |
|
122 |
assertNull(a.getRawResult()); |
|
123 |
||
124 |
{ |
|
125 |
Thread.currentThread().interrupt(); |
|
126 |
long startTime = System.nanoTime(); |
|
127 |
assertNull(a.join()); |
|
128 |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
|
129 |
Thread.interrupted(); |
|
130 |
} |
|
131 |
||
132 |
{ |
|
133 |
Thread.currentThread().interrupt(); |
|
134 |
long startTime = System.nanoTime(); |
|
135 |
a.quietlyJoin(); // should be no-op |
|
136 |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
|
137 |
Thread.interrupted(); |
|
138 |
} |
|
139 |
||
140 |
assertFalse(a.cancel(false)); |
|
141 |
assertFalse(a.cancel(true)); |
|
142 |
try { |
|
143 |
assertNull(a.get()); |
|
144 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
145 |
try { |
|
146 |
assertNull(a.get(5L, SECONDS)); |
|
147 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
148 |
} |
|
149 |
||
150 |
void checkCancelled(CountedCompleter a) { |
|
151 |
assertTrue(a.isDone()); |
|
152 |
assertTrue(a.isCancelled()); |
|
153 |
assertFalse(a.isCompletedNormally()); |
|
154 |
assertTrue(a.isCompletedAbnormally()); |
|
155 |
assertTrue(a.getException() instanceof CancellationException); |
|
156 |
assertNull(a.getRawResult()); |
|
157 |
assertTrue(a.cancel(false)); |
|
158 |
assertTrue(a.cancel(true)); |
|
159 |
||
160 |
try { |
|
161 |
Thread.currentThread().interrupt(); |
|
162 |
a.join(); |
|
163 |
shouldThrow(); |
|
164 |
} catch (CancellationException success) { |
|
165 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
166 |
Thread.interrupted(); |
|
167 |
||
168 |
{ |
|
169 |
long startTime = System.nanoTime(); |
|
170 |
a.quietlyJoin(); // should be no-op |
|
171 |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
|
172 |
} |
|
173 |
||
174 |
try { |
|
175 |
a.get(); |
|
176 |
shouldThrow(); |
|
177 |
} catch (CancellationException success) { |
|
178 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
179 |
||
180 |
try { |
|
181 |
a.get(5L, SECONDS); |
|
182 |
shouldThrow(); |
|
183 |
} catch (CancellationException success) { |
|
184 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
185 |
} |
|
186 |
||
187 |
void checkCompletedAbnormally(CountedCompleter a, Throwable t) { |
|
188 |
assertTrue(a.isDone()); |
|
189 |
assertFalse(a.isCancelled()); |
|
190 |
assertFalse(a.isCompletedNormally()); |
|
191 |
assertTrue(a.isCompletedAbnormally()); |
|
192 |
assertSame(t.getClass(), a.getException().getClass()); |
|
193 |
assertNull(a.getRawResult()); |
|
194 |
assertFalse(a.cancel(false)); |
|
195 |
assertFalse(a.cancel(true)); |
|
196 |
||
197 |
try { |
|
198 |
Thread.currentThread().interrupt(); |
|
199 |
a.join(); |
|
200 |
shouldThrow(); |
|
201 |
} catch (Throwable expected) { |
|
202 |
assertSame(t.getClass(), expected.getClass()); |
|
203 |
} |
|
204 |
Thread.interrupted(); |
|
205 |
||
206 |
{ |
|
207 |
long startTime = System.nanoTime(); |
|
208 |
a.quietlyJoin(); // should be no-op |
|
209 |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
|
210 |
} |
|
211 |
||
212 |
try { |
|
213 |
a.get(); |
|
214 |
shouldThrow(); |
|
215 |
} catch (ExecutionException success) { |
|
216 |
assertSame(t.getClass(), success.getCause().getClass()); |
|
217 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
218 |
||
219 |
try { |
|
220 |
a.get(5L, SECONDS); |
|
221 |
shouldThrow(); |
|
222 |
} catch (ExecutionException success) { |
|
223 |
assertSame(t.getClass(), success.getCause().getClass()); |
|
224 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
225 |
||
226 |
try { |
|
227 |
a.invoke(); |
|
228 |
shouldThrow(); |
|
229 |
} catch (Throwable success) { |
|
230 |
assertSame(t, success); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
public static final class FJException extends RuntimeException { |
|
235 |
FJException() { super(); } |
|
236 |
} |
|
237 |
||
238 |
abstract class CheckedCC extends CountedCompleter<Object> { |
|
239 |
final AtomicInteger computeN = new AtomicInteger(0); |
|
240 |
final AtomicInteger onCompletionN = new AtomicInteger(0); |
|
241 |
final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); |
|
242 |
final AtomicInteger setRawResultN = new AtomicInteger(0); |
|
243 |
final AtomicReference<Object> rawResult = new AtomicReference<Object>(null); |
|
244 |
int computeN() { return computeN.get(); } |
|
245 |
int onCompletionN() { return onCompletionN.get(); } |
|
246 |
int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } |
|
247 |
int setRawResultN() { return setRawResultN.get(); } |
|
248 |
||
249 |
CheckedCC() { super(); } |
|
250 |
CheckedCC(CountedCompleter p) { super(p); } |
|
251 |
CheckedCC(CountedCompleter p, int n) { super(p, n); } |
|
252 |
abstract void realCompute(); |
|
253 |
public final void compute() { |
|
254 |
computeN.incrementAndGet(); |
|
255 |
realCompute(); |
|
256 |
} |
|
257 |
public void onCompletion(CountedCompleter caller) { |
|
258 |
onCompletionN.incrementAndGet(); |
|
259 |
super.onCompletion(caller); |
|
260 |
} |
|
261 |
public boolean onExceptionalCompletion(Throwable ex, |
|
262 |
CountedCompleter caller) { |
|
263 |
onExceptionalCompletionN.incrementAndGet(); |
|
264 |
assertNotNull(ex); |
|
265 |
assertTrue(isCompletedAbnormally()); |
|
266 |
assertTrue(super.onExceptionalCompletion(ex, caller)); |
|
267 |
return true; |
|
268 |
} |
|
269 |
protected void setRawResult(Object t) { |
|
270 |
setRawResultN.incrementAndGet(); |
|
271 |
rawResult.set(t); |
|
272 |
super.setRawResult(t); |
|
273 |
} |
|
274 |
void checkIncomplete() { |
|
275 |
assertEquals(0, computeN()); |
|
276 |
assertEquals(0, onCompletionN()); |
|
277 |
assertEquals(0, onExceptionalCompletionN()); |
|
278 |
assertEquals(0, setRawResultN()); |
|
279 |
checkNotDone(this); |
|
280 |
} |
|
281 |
void checkCompletes(Object rawResult) { |
|
282 |
checkIncomplete(); |
|
283 |
int pendingCount = getPendingCount(); |
|
284 |
complete(rawResult); |
|
285 |
assertEquals(pendingCount, getPendingCount()); |
|
286 |
assertEquals(0, computeN()); |
|
287 |
assertEquals(1, onCompletionN()); |
|
288 |
assertEquals(0, onExceptionalCompletionN()); |
|
289 |
assertEquals(1, setRawResultN()); |
|
290 |
assertSame(rawResult, this.rawResult.get()); |
|
291 |
checkCompletedNormally(this); |
|
292 |
} |
|
293 |
void checkCompletesExceptionally(Throwable ex) { |
|
294 |
checkIncomplete(); |
|
295 |
completeExceptionally(ex); |
|
296 |
checkCompletedExceptionally(ex); |
|
297 |
} |
|
298 |
void checkCompletedExceptionally(Throwable ex) { |
|
299 |
assertEquals(0, computeN()); |
|
300 |
assertEquals(0, onCompletionN()); |
|
301 |
assertEquals(1, onExceptionalCompletionN()); |
|
302 |
assertEquals(0, setRawResultN()); |
|
303 |
assertNull(this.rawResult.get()); |
|
304 |
checkCompletedAbnormally(this, ex); |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
final class NoopCC extends CheckedCC { |
|
309 |
NoopCC() { super(); } |
|
310 |
NoopCC(CountedCompleter p) { super(p); } |
|
311 |
NoopCC(CountedCompleter p, int initialPendingCount) { |
|
312 |
super(p, initialPendingCount); |
|
313 |
} |
|
314 |
protected void realCompute() {} |
|
315 |
} |
|
316 |
||
317 |
/** |
|
318 |
* A newly constructed CountedCompleter is not completed; |
|
319 |
* complete() causes completion. pendingCount is ignored. |
|
320 |
*/ |
|
321 |
public void testComplete() { |
|
322 |
for (Object x : new Object[] { Boolean.TRUE, null }) { |
|
323 |
for (int pendingCount : new int[] { 0, 42 }) { |
|
324 |
testComplete(new NoopCC(), x, pendingCount); |
|
325 |
testComplete(new NoopCC(new NoopCC()), x, pendingCount); |
|
326 |
} |
|
327 |
} |
|
328 |
} |
|
329 |
void testComplete(NoopCC cc, Object x, int pendingCount) { |
|
330 |
cc.setPendingCount(pendingCount); |
|
331 |
cc.checkCompletes(x); |
|
332 |
assertEquals(pendingCount, cc.getPendingCount()); |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* completeExceptionally completes exceptionally |
|
337 |
*/ |
|
338 |
public void testCompleteExceptionally() { |
|
339 |
new NoopCC() |
|
340 |
.checkCompletesExceptionally(new FJException()); |
|
341 |
new NoopCC(new NoopCC()) |
|
342 |
.checkCompletesExceptionally(new FJException()); |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* completeExceptionally(null) surprisingly has the same effect as |
|
347 |
* completeExceptionally(new RuntimeException()) |
|
348 |
*/ |
|
349 |
public void testCompleteExceptionally_null() { |
|
350 |
NoopCC a = new NoopCC(); |
|
351 |
a.completeExceptionally(null); |
|
352 |
try { |
|
353 |
a.invoke(); |
|
354 |
shouldThrow(); |
|
355 |
} catch (RuntimeException success) { |
|
356 |
assertSame(success.getClass(), RuntimeException.class); |
|
357 |
assertNull(success.getCause()); |
|
358 |
a.checkCompletedExceptionally(success); |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
/** |
|
363 |
* setPendingCount sets the reported pending count |
|
364 |
*/ |
|
365 |
public void testSetPendingCount() { |
|
366 |
NoopCC a = new NoopCC(); |
|
367 |
assertEquals(0, a.getPendingCount()); |
|
368 |
int[] vals = { |
|
369 |
-1, 0, 1, |
|
370 |
Integer.MIN_VALUE, |
|
371 |
Integer.MAX_VALUE, |
|
372 |
}; |
|
373 |
for (int val : vals) { |
|
374 |
a.setPendingCount(val); |
|
375 |
assertEquals(val, a.getPendingCount()); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* addToPendingCount adds to the reported pending count |
|
381 |
*/ |
|
382 |
public void testAddToPendingCount() { |
|
383 |
NoopCC a = new NoopCC(); |
|
384 |
assertEquals(0, a.getPendingCount()); |
|
385 |
a.addToPendingCount(1); |
|
386 |
assertEquals(1, a.getPendingCount()); |
|
387 |
a.addToPendingCount(27); |
|
388 |
assertEquals(28, a.getPendingCount()); |
|
389 |
a.addToPendingCount(-28); |
|
390 |
assertEquals(0, a.getPendingCount()); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* decrementPendingCountUnlessZero decrements reported pending |
|
395 |
* count unless zero |
|
396 |
*/ |
|
397 |
public void testDecrementPendingCountUnlessZero() { |
|
398 |
NoopCC a = new NoopCC(null, 2); |
|
399 |
assertEquals(2, a.getPendingCount()); |
|
400 |
assertEquals(2, a.decrementPendingCountUnlessZero()); |
|
401 |
assertEquals(1, a.getPendingCount()); |
|
402 |
assertEquals(1, a.decrementPendingCountUnlessZero()); |
|
403 |
assertEquals(0, a.getPendingCount()); |
|
404 |
assertEquals(0, a.decrementPendingCountUnlessZero()); |
|
405 |
assertEquals(0, a.getPendingCount()); |
|
406 |
a.setPendingCount(-1); |
|
407 |
assertEquals(-1, a.decrementPendingCountUnlessZero()); |
|
408 |
assertEquals(-2, a.getPendingCount()); |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* compareAndSetPendingCount compares and sets the reported |
|
413 |
* pending count |
|
414 |
*/ |
|
415 |
public void testCompareAndSetPendingCount() { |
|
416 |
NoopCC a = new NoopCC(); |
|
417 |
assertEquals(0, a.getPendingCount()); |
|
418 |
assertTrue(a.compareAndSetPendingCount(0, 1)); |
|
419 |
assertEquals(1, a.getPendingCount()); |
|
420 |
assertTrue(a.compareAndSetPendingCount(1, 2)); |
|
421 |
assertEquals(2, a.getPendingCount()); |
|
422 |
assertFalse(a.compareAndSetPendingCount(1, 3)); |
|
423 |
assertEquals(2, a.getPendingCount()); |
|
424 |
} |
|
425 |
||
426 |
/** |
|
427 |
* getCompleter returns parent or null if at root |
|
428 |
*/ |
|
429 |
public void testGetCompleter() { |
|
430 |
NoopCC a = new NoopCC(); |
|
431 |
assertNull(a.getCompleter()); |
|
432 |
CountedCompleter b = new NoopCC(a); |
|
433 |
assertSame(a, b.getCompleter()); |
|
434 |
CountedCompleter c = new NoopCC(b); |
|
435 |
assertSame(b, c.getCompleter()); |
|
436 |
} |
|
437 |
||
438 |
/** |
|
439 |
* getRoot returns self if no parent, else parent's root |
|
440 |
*/ |
|
441 |
public void testGetRoot() { |
|
442 |
NoopCC a = new NoopCC(); |
|
443 |
NoopCC b = new NoopCC(a); |
|
444 |
NoopCC c = new NoopCC(b); |
|
445 |
assertSame(a, a.getRoot()); |
|
446 |
assertSame(a, b.getRoot()); |
|
447 |
assertSame(a, c.getRoot()); |
|
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* tryComplete decrements pending count unless zero, in which case |
|
452 |
* causes completion |
|
453 |
*/ |
|
454 |
public void testTryComplete() { |
|
455 |
NoopCC a = new NoopCC(); |
|
456 |
assertEquals(0, a.getPendingCount()); |
|
457 |
int n = 3; |
|
458 |
a.setPendingCount(n); |
|
459 |
for (; n > 0; n--) { |
|
460 |
assertEquals(n, a.getPendingCount()); |
|
461 |
a.tryComplete(); |
|
462 |
a.checkIncomplete(); |
|
463 |
assertEquals(n - 1, a.getPendingCount()); |
|
464 |
} |
|
465 |
a.tryComplete(); |
|
466 |
assertEquals(0, a.computeN()); |
|
467 |
assertEquals(1, a.onCompletionN()); |
|
468 |
assertEquals(0, a.onExceptionalCompletionN()); |
|
469 |
assertEquals(0, a.setRawResultN()); |
|
470 |
checkCompletedNormally(a); |
|
471 |
} |
|
472 |
||
473 |
/** |
|
474 |
* propagateCompletion decrements pending count unless zero, in |
|
475 |
* which case causes completion, without invoking onCompletion |
|
476 |
*/ |
|
477 |
public void testPropagateCompletion() { |
|
478 |
NoopCC a = new NoopCC(); |
|
479 |
assertEquals(0, a.getPendingCount()); |
|
480 |
int n = 3; |
|
481 |
a.setPendingCount(n); |
|
482 |
for (; n > 0; n--) { |
|
483 |
assertEquals(n, a.getPendingCount()); |
|
484 |
a.propagateCompletion(); |
|
485 |
a.checkIncomplete(); |
|
486 |
assertEquals(n - 1, a.getPendingCount()); |
|
487 |
} |
|
488 |
a.propagateCompletion(); |
|
489 |
assertEquals(0, a.computeN()); |
|
490 |
assertEquals(0, a.onCompletionN()); |
|
491 |
assertEquals(0, a.onExceptionalCompletionN()); |
|
492 |
assertEquals(0, a.setRawResultN()); |
|
493 |
checkCompletedNormally(a); |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
* firstComplete returns this if pending count is zero else null |
|
498 |
*/ |
|
499 |
public void testFirstComplete() { |
|
500 |
NoopCC a = new NoopCC(); |
|
501 |
a.setPendingCount(1); |
|
502 |
assertNull(a.firstComplete()); |
|
503 |
a.checkIncomplete(); |
|
504 |
assertSame(a, a.firstComplete()); |
|
505 |
a.checkIncomplete(); |
|
506 |
} |
|
507 |
||
508 |
/** |
|
509 |
* firstComplete.nextComplete returns parent if pending count is |
|
510 |
* zero else null |
|
511 |
*/ |
|
512 |
public void testNextComplete() { |
|
513 |
NoopCC a = new NoopCC(); |
|
514 |
NoopCC b = new NoopCC(a); |
|
515 |
a.setPendingCount(1); |
|
516 |
b.setPendingCount(1); |
|
517 |
assertNull(b.firstComplete()); |
|
518 |
assertSame(b, b.firstComplete()); |
|
519 |
assertNull(b.nextComplete()); |
|
520 |
a.checkIncomplete(); |
|
521 |
b.checkIncomplete(); |
|
522 |
assertSame(a, b.nextComplete()); |
|
523 |
assertSame(a, b.nextComplete()); |
|
524 |
a.checkIncomplete(); |
|
525 |
b.checkIncomplete(); |
|
526 |
assertNull(a.nextComplete()); |
|
527 |
b.checkIncomplete(); |
|
528 |
checkCompletedNormally(a); |
|
529 |
} |
|
530 |
||
531 |
/** |
|
532 |
* quietlyCompleteRoot completes root task and only root task |
|
533 |
*/ |
|
534 |
public void testQuietlyCompleteRoot() { |
|
535 |
NoopCC a = new NoopCC(); |
|
536 |
NoopCC b = new NoopCC(a); |
|
537 |
NoopCC c = new NoopCC(b); |
|
538 |
a.setPendingCount(1); |
|
539 |
b.setPendingCount(1); |
|
540 |
c.setPendingCount(1); |
|
541 |
c.quietlyCompleteRoot(); |
|
542 |
assertTrue(a.isDone()); |
|
543 |
assertFalse(b.isDone()); |
|
544 |
assertFalse(c.isDone()); |
|
545 |
} |
|
546 |
||
547 |
// Invocation tests use some interdependent task classes |
|
548 |
// to better test propagation etc |
|
549 |
||
550 |
/** |
|
551 |
* Version of Fibonacci with different classes for left vs right forks |
|
552 |
*/ |
|
553 |
abstract class CCF extends CheckedCC { |
|
554 |
int number; |
|
555 |
int rnumber; |
|
556 |
||
557 |
public CCF(CountedCompleter parent, int n) { |
|
558 |
super(parent, 1); |
|
559 |
this.number = n; |
|
560 |
} |
|
561 |
||
562 |
protected final void realCompute() { |
|
563 |
CCF f = this; |
|
564 |
int n = number; |
|
565 |
while (n >= 2) { |
|
566 |
new RCCF(f, n - 2).fork(); |
|
567 |
f = new LCCF(f, --n); |
|
568 |
} |
|
569 |
f.complete(null); |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
final class LCCF extends CCF { |
|
574 |
public LCCF(int n) { this(null, n); } |
|
575 |
public LCCF(CountedCompleter parent, int n) { |
|
576 |
super(parent, n); |
|
577 |
} |
|
578 |
public final void onCompletion(CountedCompleter caller) { |
|
579 |
super.onCompletion(caller); |
|
580 |
CCF p = (CCF)getCompleter(); |
|
581 |
int n = number + rnumber; |
|
582 |
if (p != null) |
|
583 |
p.number = n; |
|
584 |
else |
|
585 |
number = n; |
|
586 |
} |
|
587 |
} |
|
588 |
final class RCCF extends CCF { |
|
589 |
public RCCF(CountedCompleter parent, int n) { |
|
590 |
super(parent, n); |
|
591 |
} |
|
592 |
public final void onCompletion(CountedCompleter caller) { |
|
593 |
super.onCompletion(caller); |
|
594 |
CCF p = (CCF)getCompleter(); |
|
595 |
int n = number + rnumber; |
|
596 |
if (p != null) |
|
597 |
p.rnumber = n; |
|
598 |
else |
|
599 |
number = n; |
|
600 |
} |
|
601 |
} |
|
602 |
||
603 |
// Version of CCF with forced failure in left completions |
|
604 |
abstract class FailingCCF extends CheckedCC { |
|
605 |
int number; |
|
606 |
int rnumber; |
|
607 |
||
608 |
public FailingCCF(CountedCompleter parent, int n) { |
|
609 |
super(parent, 1); |
|
610 |
this.number = n; |
|
611 |
} |
|
612 |
||
613 |
protected final void realCompute() { |
|
614 |
FailingCCF f = this; |
|
615 |
int n = number; |
|
616 |
while (n >= 2) { |
|
617 |
new RFCCF(f, n - 2).fork(); |
|
618 |
f = new LFCCF(f, --n); |
|
619 |
} |
|
620 |
f.complete(null); |
|
621 |
} |
|
622 |
} |
|
623 |
||
624 |
final class LFCCF extends FailingCCF { |
|
625 |
public LFCCF(int n) { this(null, n); } |
|
626 |
public LFCCF(CountedCompleter parent, int n) { |
|
627 |
super(parent, n); |
|
628 |
} |
|
629 |
public final void onCompletion(CountedCompleter caller) { |
|
630 |
super.onCompletion(caller); |
|
631 |
FailingCCF p = (FailingCCF)getCompleter(); |
|
632 |
int n = number + rnumber; |
|
633 |
if (p != null) |
|
634 |
p.number = n; |
|
635 |
else |
|
636 |
number = n; |
|
637 |
} |
|
638 |
} |
|
639 |
final class RFCCF extends FailingCCF { |
|
640 |
public RFCCF(CountedCompleter parent, int n) { |
|
641 |
super(parent, n); |
|
642 |
} |
|
643 |
public final void onCompletion(CountedCompleter caller) { |
|
644 |
super.onCompletion(caller); |
|
645 |
completeExceptionally(new FJException()); |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
/** |
|
650 |
* invoke returns when task completes normally. |
|
651 |
* isCompletedAbnormally and isCancelled return false for normally |
|
652 |
* completed tasks; getRawResult returns null. |
|
653 |
*/ |
|
654 |
public void testInvoke() { |
|
655 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
656 |
protected void realCompute() { |
|
657 |
CCF f = new LCCF(8); |
|
658 |
assertNull(f.invoke()); |
|
659 |
assertEquals(21, f.number); |
|
660 |
checkCompletedNormally(f); |
|
661 |
}}; |
|
662 |
testInvokeOnPool(mainPool(), a); |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
* quietlyInvoke task returns when task completes normally. |
|
667 |
* isCompletedAbnormally and isCancelled return false for normally |
|
668 |
* completed tasks |
|
669 |
*/ |
|
670 |
public void testQuietlyInvoke() { |
|
671 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
672 |
protected void realCompute() { |
|
673 |
CCF f = new LCCF(8); |
|
674 |
f.quietlyInvoke(); |
|
675 |
assertEquals(21, f.number); |
|
676 |
checkCompletedNormally(f); |
|
677 |
}}; |
|
678 |
testInvokeOnPool(mainPool(), a); |
|
679 |
} |
|
680 |
||
681 |
/** |
|
682 |
* join of a forked task returns when task completes |
|
683 |
*/ |
|
684 |
public void testForkJoin() { |
|
685 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
686 |
protected void realCompute() { |
|
687 |
CCF f = new LCCF(8); |
|
688 |
assertSame(f, f.fork()); |
|
689 |
assertNull(f.join()); |
|
690 |
assertEquals(21, f.number); |
|
691 |
checkCompletedNormally(f); |
|
692 |
}}; |
|
693 |
testInvokeOnPool(mainPool(), a); |
|
694 |
} |
|
695 |
||
696 |
/** |
|
697 |
* get of a forked task returns when task completes |
|
698 |
*/ |
|
699 |
public void testForkGet() { |
|
700 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
701 |
protected void realCompute() throws Exception { |
|
702 |
CCF f = new LCCF(8); |
|
703 |
assertSame(f, f.fork()); |
|
704 |
assertNull(f.get()); |
|
705 |
assertEquals(21, f.number); |
|
706 |
checkCompletedNormally(f); |
|
707 |
}}; |
|
708 |
testInvokeOnPool(mainPool(), a); |
|
709 |
} |
|
710 |
||
711 |
/** |
|
712 |
* timed get of a forked task returns when task completes |
|
713 |
*/ |
|
714 |
public void testForkTimedGet() { |
|
715 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
716 |
protected void realCompute() throws Exception { |
|
717 |
CCF f = new LCCF(8); |
|
718 |
assertSame(f, f.fork()); |
|
719 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
|
720 |
assertEquals(21, f.number); |
|
721 |
checkCompletedNormally(f); |
|
722 |
}}; |
|
723 |
testInvokeOnPool(mainPool(), a); |
|
724 |
} |
|
725 |
||
726 |
/** |
|
727 |
* timed get with null time unit throws NPE |
|
728 |
*/ |
|
729 |
public void testForkTimedGetNPE() { |
|
730 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
731 |
protected void realCompute() throws Exception { |
|
732 |
CCF f = new LCCF(8); |
|
733 |
assertSame(f, f.fork()); |
|
734 |
try { |
|
735 |
f.get(5L, null); |
|
736 |
shouldThrow(); |
|
737 |
} catch (NullPointerException success) {} |
|
738 |
}}; |
|
739 |
testInvokeOnPool(mainPool(), a); |
|
740 |
} |
|
741 |
||
742 |
/** |
|
743 |
* quietlyJoin of a forked task returns when task completes |
|
744 |
*/ |
|
745 |
public void testForkQuietlyJoin() { |
|
746 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
747 |
protected void realCompute() { |
|
748 |
CCF f = new LCCF(8); |
|
749 |
assertSame(f, f.fork()); |
|
750 |
f.quietlyJoin(); |
|
751 |
assertEquals(21, f.number); |
|
752 |
checkCompletedNormally(f); |
|
753 |
}}; |
|
754 |
testInvokeOnPool(mainPool(), a); |
|
755 |
} |
|
756 |
||
757 |
/** |
|
758 |
* helpQuiesce returns when tasks are complete. |
|
759 |
* getQueuedTaskCount returns 0 when quiescent |
|
760 |
*/ |
|
761 |
public void testForkHelpQuiesce() { |
|
762 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
763 |
protected void realCompute() { |
|
764 |
CCF f = new LCCF(8); |
|
765 |
assertSame(f, f.fork()); |
|
766 |
helpQuiesce(); |
|
767 |
assertEquals(21, f.number); |
|
768 |
assertEquals(0, getQueuedTaskCount()); |
|
769 |
checkCompletedNormally(f); |
|
770 |
}}; |
|
771 |
testInvokeOnPool(mainPool(), a); |
|
772 |
} |
|
773 |
||
774 |
/** |
|
775 |
* invoke task throws exception when task completes abnormally |
|
776 |
*/ |
|
777 |
public void testAbnormalInvoke() { |
|
778 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
779 |
protected void realCompute() { |
|
780 |
FailingCCF f = new LFCCF(8); |
|
781 |
try { |
|
782 |
f.invoke(); |
|
783 |
shouldThrow(); |
|
784 |
} catch (FJException success) { |
|
785 |
checkCompletedAbnormally(f, success); |
|
786 |
} |
|
787 |
}}; |
|
788 |
testInvokeOnPool(mainPool(), a); |
|
789 |
} |
|
790 |
||
791 |
/** |
|
792 |
* quietlyInvoke task returns when task completes abnormally |
|
793 |
*/ |
|
794 |
public void testAbnormalQuietlyInvoke() { |
|
795 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
796 |
protected void realCompute() { |
|
797 |
FailingCCF f = new LFCCF(8); |
|
798 |
f.quietlyInvoke(); |
|
799 |
assertTrue(f.getException() instanceof FJException); |
|
800 |
checkCompletedAbnormally(f, f.getException()); |
|
801 |
}}; |
|
802 |
testInvokeOnPool(mainPool(), a); |
|
803 |
} |
|
804 |
||
805 |
/** |
|
806 |
* join of a forked task throws exception when task completes abnormally |
|
807 |
*/ |
|
808 |
public void testAbnormalForkJoin() { |
|
809 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
810 |
protected void realCompute() { |
|
811 |
FailingCCF f = new LFCCF(8); |
|
812 |
assertSame(f, f.fork()); |
|
813 |
try { |
|
814 |
f.join(); |
|
815 |
shouldThrow(); |
|
816 |
} catch (FJException success) { |
|
817 |
checkCompletedAbnormally(f, success); |
|
818 |
} |
|
819 |
}}; |
|
820 |
testInvokeOnPool(mainPool(), a); |
|
821 |
} |
|
822 |
||
823 |
/** |
|
824 |
* get of a forked task throws exception when task completes abnormally |
|
825 |
*/ |
|
826 |
public void testAbnormalForkGet() { |
|
827 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
828 |
protected void realCompute() throws Exception { |
|
829 |
FailingCCF f = new LFCCF(8); |
|
830 |
assertSame(f, f.fork()); |
|
831 |
try { |
|
832 |
f.get(); |
|
833 |
shouldThrow(); |
|
834 |
} catch (ExecutionException success) { |
|
835 |
Throwable cause = success.getCause(); |
|
836 |
assertTrue(cause instanceof FJException); |
|
837 |
checkCompletedAbnormally(f, cause); |
|
838 |
} |
|
839 |
}}; |
|
840 |
testInvokeOnPool(mainPool(), a); |
|
841 |
} |
|
842 |
||
843 |
/** |
|
844 |
* timed get of a forked task throws exception when task completes abnormally |
|
845 |
*/ |
|
846 |
public void testAbnormalForkTimedGet() { |
|
847 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
848 |
protected void realCompute() throws Exception { |
|
849 |
FailingCCF f = new LFCCF(8); |
|
850 |
assertSame(f, f.fork()); |
|
851 |
try { |
|
852 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
|
853 |
shouldThrow(); |
|
854 |
} catch (ExecutionException success) { |
|
855 |
Throwable cause = success.getCause(); |
|
856 |
assertTrue(cause instanceof FJException); |
|
857 |
checkCompletedAbnormally(f, cause); |
|
858 |
} |
|
859 |
}}; |
|
860 |
testInvokeOnPool(mainPool(), a); |
|
861 |
} |
|
862 |
||
863 |
/** |
|
864 |
* quietlyJoin of a forked task returns when task completes abnormally |
|
865 |
*/ |
|
866 |
public void testAbnormalForkQuietlyJoin() { |
|
867 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
868 |
protected void realCompute() { |
|
869 |
FailingCCF f = new LFCCF(8); |
|
870 |
assertSame(f, f.fork()); |
|
871 |
f.quietlyJoin(); |
|
872 |
assertTrue(f.getException() instanceof FJException); |
|
873 |
checkCompletedAbnormally(f, f.getException()); |
|
874 |
}}; |
|
875 |
testInvokeOnPool(mainPool(), a); |
|
876 |
} |
|
877 |
||
878 |
/** |
|
879 |
* invoke task throws exception when task cancelled |
|
880 |
*/ |
|
881 |
public void testCancelledInvoke() { |
|
882 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
883 |
protected void realCompute() { |
|
884 |
CCF f = new LCCF(8); |
|
885 |
assertTrue(f.cancel(true)); |
|
886 |
try { |
|
887 |
f.invoke(); |
|
888 |
shouldThrow(); |
|
889 |
} catch (CancellationException success) { |
|
890 |
checkCancelled(f); |
|
891 |
} |
|
892 |
}}; |
|
893 |
testInvokeOnPool(mainPool(), a); |
|
894 |
} |
|
895 |
||
896 |
/** |
|
897 |
* join of a forked task throws exception when task cancelled |
|
898 |
*/ |
|
899 |
public void testCancelledForkJoin() { |
|
900 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
901 |
protected void realCompute() { |
|
902 |
CCF f = new LCCF(8); |
|
903 |
assertTrue(f.cancel(true)); |
|
904 |
assertSame(f, f.fork()); |
|
905 |
try { |
|
906 |
f.join(); |
|
907 |
shouldThrow(); |
|
908 |
} catch (CancellationException success) { |
|
909 |
checkCancelled(f); |
|
910 |
} |
|
911 |
}}; |
|
912 |
testInvokeOnPool(mainPool(), a); |
|
913 |
} |
|
914 |
||
915 |
/** |
|
916 |
* get of a forked task throws exception when task cancelled |
|
917 |
*/ |
|
918 |
public void testCancelledForkGet() { |
|
919 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
920 |
protected void realCompute() throws Exception { |
|
921 |
CCF f = new LCCF(8); |
|
922 |
assertTrue(f.cancel(true)); |
|
923 |
assertSame(f, f.fork()); |
|
924 |
try { |
|
925 |
f.get(); |
|
926 |
shouldThrow(); |
|
927 |
} catch (CancellationException success) { |
|
928 |
checkCancelled(f); |
|
929 |
} |
|
930 |
}}; |
|
931 |
testInvokeOnPool(mainPool(), a); |
|
932 |
} |
|
933 |
||
934 |
/** |
|
935 |
* timed get of a forked task throws exception when task cancelled |
|
936 |
*/ |
|
937 |
public void testCancelledForkTimedGet() throws Exception { |
|
938 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
939 |
protected void realCompute() throws Exception { |
|
940 |
CCF f = new LCCF(8); |
|
941 |
assertTrue(f.cancel(true)); |
|
942 |
assertSame(f, f.fork()); |
|
943 |
try { |
|
944 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
|
945 |
shouldThrow(); |
|
946 |
} catch (CancellationException success) { |
|
947 |
checkCancelled(f); |
|
948 |
} |
|
949 |
}}; |
|
950 |
testInvokeOnPool(mainPool(), a); |
|
951 |
} |
|
952 |
||
953 |
/** |
|
954 |
* quietlyJoin of a forked task returns when task cancelled |
|
955 |
*/ |
|
956 |
public void testCancelledForkQuietlyJoin() { |
|
957 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
958 |
protected void realCompute() { |
|
959 |
CCF f = new LCCF(8); |
|
960 |
assertTrue(f.cancel(true)); |
|
961 |
assertSame(f, f.fork()); |
|
962 |
f.quietlyJoin(); |
|
963 |
checkCancelled(f); |
|
964 |
}}; |
|
965 |
testInvokeOnPool(mainPool(), a); |
|
966 |
} |
|
967 |
||
968 |
/** |
|
969 |
* getPool of executing task returns its pool |
|
970 |
*/ |
|
971 |
public void testGetPool() { |
|
972 |
final ForkJoinPool mainPool = mainPool(); |
|
973 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
974 |
protected void realCompute() { |
|
975 |
assertSame(mainPool, getPool()); |
|
976 |
}}; |
|
977 |
testInvokeOnPool(mainPool, a); |
|
978 |
} |
|
979 |
||
980 |
/** |
|
981 |
* getPool of non-FJ task returns null |
|
982 |
*/ |
|
983 |
public void testGetPool2() { |
|
984 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
985 |
protected void realCompute() { |
|
986 |
assertNull(getPool()); |
|
987 |
}}; |
|
988 |
assertNull(a.invoke()); |
|
989 |
} |
|
990 |
||
991 |
/** |
|
992 |
* inForkJoinPool of executing task returns true |
|
993 |
*/ |
|
994 |
public void testInForkJoinPool() { |
|
995 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
996 |
protected void realCompute() { |
|
997 |
assertTrue(inForkJoinPool()); |
|
998 |
}}; |
|
999 |
testInvokeOnPool(mainPool(), a); |
|
1000 |
} |
|
1001 |
||
1002 |
/** |
|
1003 |
* inForkJoinPool of non-FJ task returns false |
|
1004 |
*/ |
|
1005 |
public void testInForkJoinPool2() { |
|
1006 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1007 |
protected void realCompute() { |
|
1008 |
assertFalse(inForkJoinPool()); |
|
1009 |
}}; |
|
1010 |
assertNull(a.invoke()); |
|
1011 |
} |
|
1012 |
||
1013 |
/** |
|
1014 |
* setRawResult(null) succeeds |
|
1015 |
*/ |
|
1016 |
public void testSetRawResult() { |
|
1017 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1018 |
protected void realCompute() { |
|
1019 |
setRawResult(null); |
|
1020 |
assertNull(getRawResult()); |
|
1021 |
}}; |
|
1022 |
assertNull(a.invoke()); |
|
1023 |
} |
|
1024 |
||
1025 |
/** |
|
1026 |
* invoke task throws exception after invoking completeExceptionally |
|
1027 |
*/ |
|
1028 |
public void testCompleteExceptionally2() { |
|
1029 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1030 |
protected void realCompute() { |
|
1031 |
CCF n = new LCCF(8); |
|
1032 |
CCF f = new LCCF(n, 8); |
|
1033 |
FJException ex = new FJException(); |
|
1034 |
f.completeExceptionally(ex); |
|
1035 |
f.checkCompletedExceptionally(ex); |
|
1036 |
n.checkCompletedExceptionally(ex); |
|
1037 |
}}; |
|
1038 |
testInvokeOnPool(mainPool(), a); |
|
1039 |
} |
|
1040 |
||
1041 |
/** |
|
1042 |
* invokeAll(t1, t2) invokes all task arguments |
|
1043 |
*/ |
|
1044 |
public void testInvokeAll2() { |
|
1045 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1046 |
protected void realCompute() { |
|
1047 |
CCF f = new LCCF(8); |
|
1048 |
CCF g = new LCCF(9); |
|
1049 |
invokeAll(f, g); |
|
1050 |
assertEquals(21, f.number); |
|
1051 |
assertEquals(34, g.number); |
|
1052 |
checkCompletedNormally(f); |
|
1053 |
checkCompletedNormally(g); |
|
1054 |
}}; |
|
1055 |
testInvokeOnPool(mainPool(), a); |
|
1056 |
} |
|
1057 |
||
1058 |
/** |
|
1059 |
* invokeAll(tasks) with 1 argument invokes task |
|
1060 |
*/ |
|
1061 |
public void testInvokeAll1() { |
|
1062 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1063 |
protected void realCompute() { |
|
1064 |
CCF f = new LCCF(8); |
|
1065 |
invokeAll(f); |
|
1066 |
checkCompletedNormally(f); |
|
1067 |
assertEquals(21, f.number); |
|
1068 |
}}; |
|
1069 |
testInvokeOnPool(mainPool(), a); |
|
1070 |
} |
|
1071 |
||
1072 |
/** |
|
1073 |
* invokeAll(tasks) with > 2 argument invokes tasks |
|
1074 |
*/ |
|
1075 |
public void testInvokeAll3() { |
|
1076 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1077 |
protected void realCompute() { |
|
1078 |
CCF f = new LCCF(8); |
|
1079 |
CCF g = new LCCF(9); |
|
1080 |
CCF h = new LCCF(7); |
|
1081 |
invokeAll(f, g, h); |
|
1082 |
assertEquals(21, f.number); |
|
1083 |
assertEquals(34, g.number); |
|
1084 |
assertEquals(13, h.number); |
|
1085 |
checkCompletedNormally(f); |
|
1086 |
checkCompletedNormally(g); |
|
1087 |
checkCompletedNormally(h); |
|
1088 |
}}; |
|
1089 |
testInvokeOnPool(mainPool(), a); |
|
1090 |
} |
|
1091 |
||
1092 |
/** |
|
1093 |
* invokeAll(collection) invokes all tasks in the collection |
|
1094 |
*/ |
|
1095 |
public void testInvokeAllCollection() { |
|
1096 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1097 |
protected void realCompute() { |
|
1098 |
CCF f = new LCCF(8); |
|
1099 |
CCF g = new LCCF(9); |
|
1100 |
CCF h = new LCCF(7); |
|
1101 |
HashSet set = new HashSet(); |
|
1102 |
set.add(f); |
|
1103 |
set.add(g); |
|
1104 |
set.add(h); |
|
1105 |
invokeAll(set); |
|
1106 |
assertEquals(21, f.number); |
|
1107 |
assertEquals(34, g.number); |
|
1108 |
assertEquals(13, h.number); |
|
1109 |
checkCompletedNormally(f); |
|
1110 |
checkCompletedNormally(g); |
|
1111 |
checkCompletedNormally(h); |
|
1112 |
}}; |
|
1113 |
testInvokeOnPool(mainPool(), a); |
|
1114 |
} |
|
1115 |
||
1116 |
/** |
|
1117 |
* invokeAll(tasks) with any null task throws NPE |
|
1118 |
*/ |
|
1119 |
public void testInvokeAllNPE() { |
|
1120 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1121 |
protected void realCompute() { |
|
1122 |
CCF f = new LCCF(8); |
|
1123 |
CCF g = new LCCF(9); |
|
1124 |
CCF h = null; |
|
1125 |
try { |
|
1126 |
invokeAll(f, g, h); |
|
1127 |
shouldThrow(); |
|
1128 |
} catch (NullPointerException success) {} |
|
1129 |
}}; |
|
1130 |
testInvokeOnPool(mainPool(), a); |
|
1131 |
} |
|
1132 |
||
1133 |
/** |
|
1134 |
* invokeAll(t1, t2) throw exception if any task does |
|
1135 |
*/ |
|
1136 |
public void testAbnormalInvokeAll2() { |
|
1137 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1138 |
protected void realCompute() { |
|
1139 |
CCF f = new LCCF(8); |
|
1140 |
FailingCCF g = new LFCCF(9); |
|
1141 |
try { |
|
1142 |
invokeAll(f, g); |
|
1143 |
shouldThrow(); |
|
1144 |
} catch (FJException success) { |
|
1145 |
checkCompletedAbnormally(g, success); |
|
1146 |
} |
|
1147 |
}}; |
|
1148 |
testInvokeOnPool(mainPool(), a); |
|
1149 |
} |
|
1150 |
||
1151 |
/** |
|
1152 |
* invokeAll(tasks) with 1 argument throws exception if task does |
|
1153 |
*/ |
|
1154 |
public void testAbnormalInvokeAll1() { |
|
1155 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1156 |
protected void realCompute() { |
|
1157 |
FailingCCF g = new LFCCF(9); |
|
1158 |
try { |
|
1159 |
invokeAll(g); |
|
1160 |
shouldThrow(); |
|
1161 |
} catch (FJException success) { |
|
1162 |
checkCompletedAbnormally(g, success); |
|
1163 |
} |
|
1164 |
}}; |
|
1165 |
testInvokeOnPool(mainPool(), a); |
|
1166 |
} |
|
1167 |
||
1168 |
/** |
|
1169 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
|
1170 |
*/ |
|
1171 |
public void testAbnormalInvokeAll3() { |
|
1172 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1173 |
protected void realCompute() { |
|
1174 |
CCF f = new LCCF(8); |
|
1175 |
FailingCCF g = new LFCCF(9); |
|
1176 |
CCF h = new LCCF(7); |
|
1177 |
try { |
|
1178 |
invokeAll(f, g, h); |
|
1179 |
shouldThrow(); |
|
1180 |
} catch (FJException success) { |
|
1181 |
checkCompletedAbnormally(g, success); |
|
1182 |
} |
|
1183 |
}}; |
|
1184 |
testInvokeOnPool(mainPool(), a); |
|
1185 |
} |
|
1186 |
||
1187 |
/** |
|
1188 |
* invokeAll(collection) throws exception if any task does |
|
1189 |
*/ |
|
1190 |
public void testAbnormalInvokeAllCollection() { |
|
1191 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1192 |
protected void realCompute() { |
|
1193 |
FailingCCF f = new LFCCF(8); |
|
1194 |
CCF g = new LCCF(9); |
|
1195 |
CCF h = new LCCF(7); |
|
1196 |
HashSet set = new HashSet(); |
|
1197 |
set.add(f); |
|
1198 |
set.add(g); |
|
1199 |
set.add(h); |
|
1200 |
try { |
|
1201 |
invokeAll(set); |
|
1202 |
shouldThrow(); |
|
1203 |
} catch (FJException success) { |
|
1204 |
checkCompletedAbnormally(f, success); |
|
1205 |
} |
|
1206 |
}}; |
|
1207 |
testInvokeOnPool(mainPool(), a); |
|
1208 |
} |
|
1209 |
||
1210 |
/** |
|
1211 |
* tryUnfork returns true for most recent unexecuted task, |
|
1212 |
* and suppresses execution |
|
1213 |
*/ |
|
1214 |
public void testTryUnfork() { |
|
1215 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1216 |
protected void realCompute() { |
|
1217 |
CCF g = new LCCF(9); |
|
1218 |
assertSame(g, g.fork()); |
|
1219 |
CCF f = new LCCF(8); |
|
1220 |
assertSame(f, f.fork()); |
|
1221 |
assertTrue(f.tryUnfork()); |
|
1222 |
helpQuiesce(); |
|
1223 |
checkNotDone(f); |
|
1224 |
checkCompletedNormally(g); |
|
1225 |
}}; |
|
1226 |
testInvokeOnPool(singletonPool(), a); |
|
1227 |
} |
|
1228 |
||
1229 |
/** |
|
1230 |
* getSurplusQueuedTaskCount returns > 0 when |
|
1231 |
* there are more tasks than threads |
|
1232 |
*/ |
|
1233 |
public void testGetSurplusQueuedTaskCount() { |
|
1234 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1235 |
protected void realCompute() { |
|
1236 |
CCF h = new LCCF(7); |
|
1237 |
assertSame(h, h.fork()); |
|
1238 |
CCF g = new LCCF(9); |
|
1239 |
assertSame(g, g.fork()); |
|
1240 |
CCF f = new LCCF(8); |
|
1241 |
assertSame(f, f.fork()); |
|
1242 |
assertTrue(getSurplusQueuedTaskCount() > 0); |
|
1243 |
helpQuiesce(); |
|
1244 |
assertEquals(0, getSurplusQueuedTaskCount()); |
|
1245 |
checkCompletedNormally(f); |
|
1246 |
checkCompletedNormally(g); |
|
1247 |
checkCompletedNormally(h); |
|
1248 |
}}; |
|
1249 |
testInvokeOnPool(singletonPool(), a); |
|
1250 |
} |
|
1251 |
||
1252 |
/** |
|
1253 |
* peekNextLocalTask returns most recent unexecuted task. |
|
1254 |
*/ |
|
1255 |
public void testPeekNextLocalTask() { |
|
1256 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1257 |
protected void realCompute() { |
|
1258 |
CCF g = new LCCF(9); |
|
1259 |
assertSame(g, g.fork()); |
|
1260 |
CCF f = new LCCF(8); |
|
1261 |
assertSame(f, f.fork()); |
|
1262 |
assertSame(f, peekNextLocalTask()); |
|
1263 |
assertNull(f.join()); |
|
1264 |
checkCompletedNormally(f); |
|
1265 |
helpQuiesce(); |
|
1266 |
checkCompletedNormally(g); |
|
1267 |
}}; |
|
1268 |
testInvokeOnPool(singletonPool(), a); |
|
1269 |
} |
|
1270 |
||
1271 |
/** |
|
1272 |
* pollNextLocalTask returns most recent unexecuted task without |
|
1273 |
* executing it |
|
1274 |
*/ |
|
1275 |
public void testPollNextLocalTask() { |
|
1276 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1277 |
protected void realCompute() { |
|
1278 |
CCF g = new LCCF(9); |
|
1279 |
assertSame(g, g.fork()); |
|
1280 |
CCF f = new LCCF(8); |
|
1281 |
assertSame(f, f.fork()); |
|
1282 |
assertSame(f, pollNextLocalTask()); |
|
1283 |
helpQuiesce(); |
|
1284 |
checkNotDone(f); |
|
1285 |
assertEquals(34, g.number); |
|
1286 |
checkCompletedNormally(g); |
|
1287 |
}}; |
|
1288 |
testInvokeOnPool(singletonPool(), a); |
|
1289 |
} |
|
1290 |
||
1291 |
/** |
|
1292 |
* pollTask returns an unexecuted task without executing it |
|
1293 |
*/ |
|
1294 |
public void testPollTask() { |
|
1295 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1296 |
protected void realCompute() { |
|
1297 |
CCF g = new LCCF(9); |
|
1298 |
assertSame(g, g.fork()); |
|
1299 |
CCF f = new LCCF(8); |
|
1300 |
assertSame(f, f.fork()); |
|
1301 |
assertSame(f, pollTask()); |
|
1302 |
helpQuiesce(); |
|
1303 |
checkNotDone(f); |
|
1304 |
checkCompletedNormally(g); |
|
1305 |
}}; |
|
1306 |
testInvokeOnPool(singletonPool(), a); |
|
1307 |
} |
|
1308 |
||
1309 |
/** |
|
1310 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
|
1311 |
*/ |
|
1312 |
public void testPeekNextLocalTaskAsync() { |
|
1313 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1314 |
protected void realCompute() { |
|
1315 |
CCF g = new LCCF(9); |
|
1316 |
assertSame(g, g.fork()); |
|
1317 |
CCF f = new LCCF(8); |
|
1318 |
assertSame(f, f.fork()); |
|
1319 |
assertSame(g, peekNextLocalTask()); |
|
1320 |
assertNull(f.join()); |
|
1321 |
helpQuiesce(); |
|
1322 |
checkCompletedNormally(f); |
|
1323 |
assertEquals(34, g.number); |
|
1324 |
checkCompletedNormally(g); |
|
1325 |
}}; |
|
1326 |
testInvokeOnPool(asyncSingletonPool(), a); |
|
1327 |
} |
|
1328 |
||
1329 |
/** |
|
1330 |
* pollNextLocalTask returns least recent unexecuted task without |
|
1331 |
* executing it, in async mode |
|
1332 |
*/ |
|
1333 |
public void testPollNextLocalTaskAsync() { |
|
1334 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1335 |
protected void realCompute() { |
|
1336 |
CCF g = new LCCF(9); |
|
1337 |
assertSame(g, g.fork()); |
|
1338 |
CCF f = new LCCF(8); |
|
1339 |
assertSame(f, f.fork()); |
|
1340 |
assertSame(g, pollNextLocalTask()); |
|
1341 |
helpQuiesce(); |
|
1342 |
assertEquals(21, f.number); |
|
1343 |
checkCompletedNormally(f); |
|
1344 |
checkNotDone(g); |
|
1345 |
}}; |
|
1346 |
testInvokeOnPool(asyncSingletonPool(), a); |
|
1347 |
} |
|
1348 |
||
1349 |
/** |
|
1350 |
* pollTask returns an unexecuted task without executing it, in |
|
1351 |
* async mode |
|
1352 |
*/ |
|
1353 |
public void testPollTaskAsync() { |
|
1354 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1355 |
protected void realCompute() { |
|
1356 |
CCF g = new LCCF(9); |
|
1357 |
assertSame(g, g.fork()); |
|
1358 |
CCF f = new LCCF(8); |
|
1359 |
assertSame(f, f.fork()); |
|
1360 |
assertSame(g, pollTask()); |
|
1361 |
helpQuiesce(); |
|
1362 |
assertEquals(21, f.number); |
|
1363 |
checkCompletedNormally(f); |
|
1364 |
checkNotDone(g); |
|
1365 |
}}; |
|
1366 |
testInvokeOnPool(asyncSingletonPool(), a); |
|
1367 |
} |
|
1368 |
||
1369 |
// versions for singleton pools |
|
1370 |
||
1371 |
/** |
|
1372 |
* invoke returns when task completes normally. |
|
1373 |
* isCompletedAbnormally and isCancelled return false for normally |
|
1374 |
* completed tasks; getRawResult returns null. |
|
1375 |
*/ |
|
1376 |
public void testInvokeSingleton() { |
|
1377 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1378 |
protected void realCompute() { |
|
1379 |
CCF f = new LCCF(8); |
|
1380 |
assertNull(f.invoke()); |
|
1381 |
assertEquals(21, f.number); |
|
1382 |
checkCompletedNormally(f); |
|
1383 |
}}; |
|
1384 |
testInvokeOnPool(singletonPool(), a); |
|
1385 |
} |
|
1386 |
||
1387 |
/** |
|
1388 |
* quietlyInvoke task returns when task completes normally. |
|
1389 |
* isCompletedAbnormally and isCancelled return false for normally |
|
1390 |
* completed tasks |
|
1391 |
*/ |
|
1392 |
public void testQuietlyInvokeSingleton() { |
|
1393 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1394 |
protected void realCompute() { |
|
1395 |
CCF f = new LCCF(8); |
|
1396 |
f.quietlyInvoke(); |
|
1397 |
assertEquals(21, f.number); |
|
1398 |
checkCompletedNormally(f); |
|
1399 |
}}; |
|
1400 |
testInvokeOnPool(singletonPool(), a); |
|
1401 |
} |
|
1402 |
||
1403 |
/** |
|
1404 |
* join of a forked task returns when task completes |
|
1405 |
*/ |
|
1406 |
public void testForkJoinSingleton() { |
|
1407 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1408 |
protected void realCompute() { |
|
1409 |
CCF f = new LCCF(8); |
|
1410 |
assertSame(f, f.fork()); |
|
1411 |
assertNull(f.join()); |
|
1412 |
assertEquals(21, f.number); |
|
1413 |
checkCompletedNormally(f); |
|
1414 |
}}; |
|
1415 |
testInvokeOnPool(singletonPool(), a); |
|
1416 |
} |
|
1417 |
||
1418 |
/** |
|
1419 |
* get of a forked task returns when task completes |
|
1420 |
*/ |
|
1421 |
public void testForkGetSingleton() { |
|
1422 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1423 |
protected void realCompute() throws Exception { |
|
1424 |
CCF f = new LCCF(8); |
|
1425 |
assertSame(f, f.fork()); |
|
1426 |
assertNull(f.get()); |
|
1427 |
assertEquals(21, f.number); |
|
1428 |
checkCompletedNormally(f); |
|
1429 |
}}; |
|
1430 |
testInvokeOnPool(singletonPool(), a); |
|
1431 |
} |
|
1432 |
||
1433 |
/** |
|
1434 |
* timed get of a forked task returns when task completes |
|
1435 |
*/ |
|
1436 |
public void testForkTimedGetSingleton() { |
|
1437 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1438 |
protected void realCompute() throws Exception { |
|
1439 |
CCF f = new LCCF(8); |
|
1440 |
assertSame(f, f.fork()); |
|
1441 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
|
1442 |
assertEquals(21, f.number); |
|
1443 |
checkCompletedNormally(f); |
|
1444 |
}}; |
|
1445 |
testInvokeOnPool(singletonPool(), a); |
|
1446 |
} |
|
1447 |
||
1448 |
/** |
|
1449 |
* timed get with null time unit throws NPE |
|
1450 |
*/ |
|
1451 |
public void testForkTimedGetNPESingleton() { |
|
1452 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1453 |
protected void realCompute() throws Exception { |
|
1454 |
CCF f = new LCCF(8); |
|
1455 |
assertSame(f, f.fork()); |
|
1456 |
try { |
|
1457 |
f.get(5L, null); |
|
1458 |
shouldThrow(); |
|
1459 |
} catch (NullPointerException success) {} |
|
1460 |
}}; |
|
1461 |
testInvokeOnPool(singletonPool(), a); |
|
1462 |
} |
|
1463 |
||
1464 |
/** |
|
1465 |
* quietlyJoin of a forked task returns when task completes |
|
1466 |
*/ |
|
1467 |
public void testForkQuietlyJoinSingleton() { |
|
1468 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1469 |
protected void realCompute() { |
|
1470 |
CCF f = new LCCF(8); |
|
1471 |
assertSame(f, f.fork()); |
|
1472 |
f.quietlyJoin(); |
|
1473 |
assertEquals(21, f.number); |
|
1474 |
checkCompletedNormally(f); |
|
1475 |
}}; |
|
1476 |
testInvokeOnPool(singletonPool(), a); |
|
1477 |
} |
|
1478 |
||
1479 |
/** |
|
1480 |
* helpQuiesce returns when tasks are complete. |
|
1481 |
* getQueuedTaskCount returns 0 when quiescent |
|
1482 |
*/ |
|
1483 |
public void testForkHelpQuiesceSingleton() { |
|
1484 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1485 |
protected void realCompute() { |
|
1486 |
CCF f = new LCCF(8); |
|
1487 |
assertSame(f, f.fork()); |
|
1488 |
helpQuiesce(); |
|
1489 |
assertEquals(0, getQueuedTaskCount()); |
|
1490 |
assertEquals(21, f.number); |
|
1491 |
checkCompletedNormally(f); |
|
1492 |
}}; |
|
1493 |
testInvokeOnPool(singletonPool(), a); |
|
1494 |
} |
|
1495 |
||
1496 |
/** |
|
1497 |
* invoke task throws exception when task completes abnormally |
|
1498 |
*/ |
|
1499 |
public void testAbnormalInvokeSingleton() { |
|
1500 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1501 |
protected void realCompute() { |
|
1502 |
FailingCCF f = new LFCCF(8); |
|
1503 |
try { |
|
1504 |
f.invoke(); |
|
1505 |
shouldThrow(); |
|
1506 |
} catch (FJException success) { |
|
1507 |
checkCompletedAbnormally(f, success); |
|
1508 |
} |
|
1509 |
}}; |
|
1510 |
testInvokeOnPool(singletonPool(), a); |
|
1511 |
} |
|
1512 |
||
1513 |
/** |
|
1514 |
* quietlyInvoke task returns when task completes abnormally |
|
1515 |
*/ |
|
1516 |
public void testAbnormalQuietlyInvokeSingleton() { |
|
1517 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1518 |
protected void realCompute() { |
|
1519 |
FailingCCF f = new LFCCF(8); |
|
1520 |
f.quietlyInvoke(); |
|
1521 |
assertTrue(f.getException() instanceof FJException); |
|
1522 |
checkCompletedAbnormally(f, f.getException()); |
|
1523 |
}}; |
|
1524 |
testInvokeOnPool(singletonPool(), a); |
|
1525 |
} |
|
1526 |
||
1527 |
/** |
|
1528 |
* join of a forked task throws exception when task completes abnormally |
|
1529 |
*/ |
|
1530 |
public void testAbnormalForkJoinSingleton() { |
|
1531 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1532 |
protected void realCompute() { |
|
1533 |
FailingCCF f = new LFCCF(8); |
|
1534 |
assertSame(f, f.fork()); |
|
1535 |
try { |
|
1536 |
f.join(); |
|
1537 |
shouldThrow(); |
|
1538 |
} catch (FJException success) { |
|
1539 |
checkCompletedAbnormally(f, success); |
|
1540 |
} |
|
1541 |
}}; |
|
1542 |
testInvokeOnPool(singletonPool(), a); |
|
1543 |
} |
|
1544 |
||
1545 |
/** |
|
1546 |
* get of a forked task throws exception when task completes abnormally |
|
1547 |
*/ |
|
1548 |
public void testAbnormalForkGetSingleton() { |
|
1549 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1550 |
protected void realCompute() throws Exception { |
|
1551 |
FailingCCF f = new LFCCF(8); |
|
1552 |
assertSame(f, f.fork()); |
|
1553 |
try { |
|
1554 |
f.get(); |
|
1555 |
shouldThrow(); |
|
1556 |
} catch (ExecutionException success) { |
|
1557 |
Throwable cause = success.getCause(); |
|
1558 |
assertTrue(cause instanceof FJException); |
|
1559 |
checkCompletedAbnormally(f, cause); |
|
1560 |
} |
|
1561 |
}}; |
|
1562 |
testInvokeOnPool(singletonPool(), a); |
|
1563 |
} |
|
1564 |
||
1565 |
/** |
|
1566 |
* timed get of a forked task throws exception when task completes abnormally |
|
1567 |
*/ |
|
1568 |
public void testAbnormalForkTimedGetSingleton() { |
|
1569 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1570 |
protected void realCompute() throws Exception { |
|
1571 |
FailingCCF f = new LFCCF(8); |
|
1572 |
assertSame(f, f.fork()); |
|
1573 |
try { |
|
1574 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
|
1575 |
shouldThrow(); |
|
1576 |
} catch (ExecutionException success) { |
|
1577 |
Throwable cause = success.getCause(); |
|
1578 |
assertTrue(cause instanceof FJException); |
|
1579 |
checkCompletedAbnormally(f, cause); |
|
1580 |
} |
|
1581 |
}}; |
|
1582 |
testInvokeOnPool(singletonPool(), a); |
|
1583 |
} |
|
1584 |
||
1585 |
/** |
|
1586 |
* quietlyJoin of a forked task returns when task completes abnormally |
|
1587 |
*/ |
|
1588 |
public void testAbnormalForkQuietlyJoinSingleton() { |
|
1589 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1590 |
protected void realCompute() { |
|
1591 |
FailingCCF f = new LFCCF(8); |
|
1592 |
assertSame(f, f.fork()); |
|
1593 |
f.quietlyJoin(); |
|
1594 |
assertTrue(f.getException() instanceof FJException); |
|
1595 |
checkCompletedAbnormally(f, f.getException()); |
|
1596 |
}}; |
|
1597 |
testInvokeOnPool(singletonPool(), a); |
|
1598 |
} |
|
1599 |
||
1600 |
/** |
|
1601 |
* invoke task throws exception when task cancelled |
|
1602 |
*/ |
|
1603 |
public void testCancelledInvokeSingleton() { |
|
1604 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1605 |
protected void realCompute() { |
|
1606 |
CCF f = new LCCF(8); |
|
1607 |
assertTrue(f.cancel(true)); |
|
1608 |
try { |
|
1609 |
f.invoke(); |
|
1610 |
shouldThrow(); |
|
1611 |
} catch (CancellationException success) { |
|
1612 |
checkCancelled(f); |
|
1613 |
} |
|
1614 |
}}; |
|
1615 |
testInvokeOnPool(singletonPool(), a); |
|
1616 |
} |
|
1617 |
||
1618 |
/** |
|
1619 |
* join of a forked task throws exception when task cancelled |
|
1620 |
*/ |
|
1621 |
public void testCancelledForkJoinSingleton() { |
|
1622 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1623 |
protected void realCompute() { |
|
1624 |
CCF f = new LCCF(8); |
|
1625 |
assertTrue(f.cancel(true)); |
|
1626 |
assertSame(f, f.fork()); |
|
1627 |
try { |
|
1628 |
f.join(); |
|
1629 |
shouldThrow(); |
|
1630 |
} catch (CancellationException success) { |
|
1631 |
checkCancelled(f); |
|
1632 |
} |
|
1633 |
}}; |
|
1634 |
testInvokeOnPool(singletonPool(), a); |
|
1635 |
} |
|
1636 |
||
1637 |
/** |
|
1638 |
* get of a forked task throws exception when task cancelled |
|
1639 |
*/ |
|
1640 |
public void testCancelledForkGetSingleton() { |
|
1641 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1642 |
protected void realCompute() throws Exception { |
|
1643 |
CCF f = new LCCF(8); |
|
1644 |
assertTrue(f.cancel(true)); |
|
1645 |
assertSame(f, f.fork()); |
|
1646 |
try { |
|
1647 |
f.get(); |
|
1648 |
shouldThrow(); |
|
1649 |
} catch (CancellationException success) { |
|
1650 |
checkCancelled(f); |
|
1651 |
} |
|
1652 |
}}; |
|
1653 |
testInvokeOnPool(singletonPool(), a); |
|
1654 |
} |
|
1655 |
||
1656 |
/** |
|
1657 |
* timed get of a forked task throws exception when task cancelled |
|
1658 |
*/ |
|
1659 |
public void testCancelledForkTimedGetSingleton() throws Exception { |
|
1660 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1661 |
protected void realCompute() throws Exception { |
|
1662 |
CCF f = new LCCF(8); |
|
1663 |
assertTrue(f.cancel(true)); |
|
1664 |
assertSame(f, f.fork()); |
|
1665 |
try { |
|
1666 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
|
1667 |
shouldThrow(); |
|
1668 |
} catch (CancellationException success) { |
|
1669 |
checkCancelled(f); |
|
1670 |
} |
|
1671 |
}}; |
|
1672 |
testInvokeOnPool(singletonPool(), a); |
|
1673 |
} |
|
1674 |
||
1675 |
/** |
|
1676 |
* quietlyJoin of a forked task returns when task cancelled |
|
1677 |
*/ |
|
1678 |
public void testCancelledForkQuietlyJoinSingleton() { |
|
1679 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1680 |
protected void realCompute() { |
|
1681 |
CCF f = new LCCF(8); |
|
1682 |
assertTrue(f.cancel(true)); |
|
1683 |
assertSame(f, f.fork()); |
|
1684 |
f.quietlyJoin(); |
|
1685 |
checkCancelled(f); |
|
1686 |
}}; |
|
1687 |
testInvokeOnPool(singletonPool(), a); |
|
1688 |
} |
|
1689 |
||
1690 |
/** |
|
1691 |
* invoke task throws exception after invoking completeExceptionally |
|
1692 |
*/ |
|
1693 |
public void testCompleteExceptionallySingleton() { |
|
1694 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1695 |
protected void realCompute() { |
|
1696 |
CCF n = new LCCF(8); |
|
1697 |
CCF f = new LCCF(n, 8); |
|
1698 |
FJException ex = new FJException(); |
|
1699 |
f.completeExceptionally(ex); |
|
1700 |
f.checkCompletedExceptionally(ex); |
|
1701 |
n.checkCompletedExceptionally(ex); |
|
1702 |
}}; |
|
1703 |
testInvokeOnPool(singletonPool(), a); |
|
1704 |
} |
|
1705 |
||
1706 |
/** |
|
1707 |
* invokeAll(t1, t2) invokes all task arguments |
|
1708 |
*/ |
|
1709 |
public void testInvokeAll2Singleton() { |
|
1710 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1711 |
protected void realCompute() { |
|
1712 |
CCF f = new LCCF(8); |
|
1713 |
CCF g = new LCCF(9); |
|
1714 |
invokeAll(f, g); |
|
1715 |
assertEquals(21, f.number); |
|
1716 |
assertEquals(34, g.number); |
|
1717 |
checkCompletedNormally(f); |
|
1718 |
checkCompletedNormally(g); |
|
1719 |
}}; |
|
1720 |
testInvokeOnPool(singletonPool(), a); |
|
1721 |
} |
|
1722 |
||
1723 |
/** |
|
1724 |
* invokeAll(tasks) with 1 argument invokes task |
|
1725 |
*/ |
|
1726 |
public void testInvokeAll1Singleton() { |
|
1727 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1728 |
protected void realCompute() { |
|
1729 |
CCF f = new LCCF(8); |
|
1730 |
invokeAll(f); |
|
1731 |
checkCompletedNormally(f); |
|
1732 |
assertEquals(21, f.number); |
|
1733 |
}}; |
|
1734 |
testInvokeOnPool(singletonPool(), a); |
|
1735 |
} |
|
1736 |
||
1737 |
/** |
|
1738 |
* invokeAll(tasks) with > 2 argument invokes tasks |
|
1739 |
*/ |
|
1740 |
public void testInvokeAll3Singleton() { |
|
1741 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1742 |
protected void realCompute() { |
|
1743 |
CCF f = new LCCF(8); |
|
1744 |
CCF g = new LCCF(9); |
|
1745 |
CCF h = new LCCF(7); |
|
1746 |
invokeAll(f, g, h); |
|
1747 |
assertEquals(21, f.number); |
|
1748 |
assertEquals(34, g.number); |
|
1749 |
assertEquals(13, h.number); |
|
1750 |
checkCompletedNormally(f); |
|
1751 |
checkCompletedNormally(g); |
|
1752 |
checkCompletedNormally(h); |
|
1753 |
}}; |
|
1754 |
testInvokeOnPool(singletonPool(), a); |
|
1755 |
} |
|
1756 |
||
1757 |
/** |
|
1758 |
* invokeAll(collection) invokes all tasks in the collection |
|
1759 |
*/ |
|
1760 |
public void testInvokeAllCollectionSingleton() { |
|
1761 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1762 |
protected void realCompute() { |
|
1763 |
CCF f = new LCCF(8); |
|
1764 |
CCF g = new LCCF(9); |
|
1765 |
CCF h = new LCCF(7); |
|
1766 |
HashSet set = new HashSet(); |
|
1767 |
set.add(f); |
|
1768 |
set.add(g); |
|
1769 |
set.add(h); |
|
1770 |
invokeAll(set); |
|
1771 |
assertEquals(21, f.number); |
|
1772 |
assertEquals(34, g.number); |
|
1773 |
assertEquals(13, h.number); |
|
1774 |
checkCompletedNormally(f); |
|
1775 |
checkCompletedNormally(g); |
|
1776 |
checkCompletedNormally(h); |
|
1777 |
}}; |
|
1778 |
testInvokeOnPool(singletonPool(), a); |
|
1779 |
} |
|
1780 |
||
1781 |
/** |
|
1782 |
* invokeAll(tasks) with any null task throws NPE |
|
1783 |
*/ |
|
1784 |
public void testInvokeAllNPESingleton() { |
|
1785 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1786 |
protected void realCompute() { |
|
1787 |
CCF f = new LCCF(8); |
|
1788 |
CCF g = new LCCF(9); |
|
1789 |
CCF h = null; |
|
1790 |
try { |
|
1791 |
invokeAll(f, g, h); |
|
1792 |
shouldThrow(); |
|
1793 |
} catch (NullPointerException success) {} |
|
1794 |
}}; |
|
1795 |
testInvokeOnPool(singletonPool(), a); |
|
1796 |
} |
|
1797 |
||
1798 |
/** |
|
1799 |
* invokeAll(t1, t2) throw exception if any task does |
|
1800 |
*/ |
|
1801 |
public void testAbnormalInvokeAll2Singleton() { |
|
1802 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1803 |
protected void realCompute() { |
|
1804 |
CCF f = new LCCF(8); |
|
1805 |
FailingCCF g = new LFCCF(9); |
|
1806 |
try { |
|
1807 |
invokeAll(f, g); |
|
1808 |
shouldThrow(); |
|
1809 |
} catch (FJException success) { |
|
1810 |
checkCompletedAbnormally(g, success); |
|
1811 |
} |
|
1812 |
}}; |
|
1813 |
testInvokeOnPool(singletonPool(), a); |
|
1814 |
} |
|
1815 |
||
1816 |
/** |
|
1817 |
* invokeAll(tasks) with 1 argument throws exception if task does |
|
1818 |
*/ |
|
1819 |
public void testAbnormalInvokeAll1Singleton() { |
|
1820 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1821 |
protected void realCompute() { |
|
1822 |
FailingCCF g = new LFCCF(9); |
|
1823 |
try { |
|
1824 |
invokeAll(g); |
|
1825 |
shouldThrow(); |
|
1826 |
} catch (FJException success) { |
|
1827 |
checkCompletedAbnormally(g, success); |
|
1828 |
} |
|
1829 |
}}; |
|
1830 |
testInvokeOnPool(singletonPool(), a); |
|
1831 |
} |
|
1832 |
||
1833 |
/** |
|
1834 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
|
1835 |
*/ |
|
1836 |
public void testAbnormalInvokeAll3Singleton() { |
|
1837 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1838 |
protected void realCompute() { |
|
1839 |
CCF f = new LCCF(8); |
|
1840 |
FailingCCF g = new LFCCF(9); |
|
1841 |
CCF h = new LCCF(7); |
|
1842 |
try { |
|
1843 |
invokeAll(f, g, h); |
|
1844 |
shouldThrow(); |
|
1845 |
} catch (FJException success) { |
|
1846 |
checkCompletedAbnormally(g, success); |
|
1847 |
} |
|
1848 |
}}; |
|
1849 |
testInvokeOnPool(singletonPool(), a); |
|
1850 |
} |
|
1851 |
||
1852 |
/** |
|
1853 |
* invokeAll(collection) throws exception if any task does |
|
1854 |
*/ |
|
1855 |
public void testAbnormalInvokeAllCollectionSingleton() { |
|
1856 |
ForkJoinTask a = new CheckedRecursiveAction() { |
|
1857 |
protected void realCompute() { |
|
1858 |
FailingCCF f = new LFCCF(8); |
|
1859 |
CCF g = new LCCF(9); |
|
1860 |
CCF h = new LCCF(7); |
|
1861 |
HashSet set = new HashSet(); |
|
1862 |
set.add(f); |
|
1863 |
set.add(g); |
|
1864 |
set.add(h); |
|
1865 |
try { |
|
1866 |
invokeAll(set); |
|
1867 |
shouldThrow(); |
|
1868 |
} catch (FJException success) { |
|
1869 |
checkCompletedAbnormally(f, success); |
|
1870 |
} |
|
1871 |
}}; |
|
1872 |
testInvokeOnPool(singletonPool(), a); |
|
1873 |
} |
|
1874 |
||
40815
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1875 |
/** CountedCompleter class javadoc code sample, version 1. */ |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1876 |
public static <E> void forEach1(E[] array, Consumer<E> action) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1877 |
class Task extends CountedCompleter<Void> { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1878 |
final int lo, hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1879 |
Task(Task parent, int lo, int hi) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1880 |
super(parent); this.lo = lo; this.hi = hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1881 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1882 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1883 |
public void compute() { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1884 |
if (hi - lo >= 2) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1885 |
int mid = (lo + hi) >>> 1; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1886 |
// must set pending count before fork |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1887 |
setPendingCount(2); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1888 |
new Task(this, mid, hi).fork(); // right child |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1889 |
new Task(this, lo, mid).fork(); // left child |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1890 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1891 |
else if (hi > lo) |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1892 |
action.accept(array[lo]); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1893 |
tryComplete(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1894 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1895 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1896 |
new Task(null, 0, array.length).invoke(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1897 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1898 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1899 |
/** CountedCompleter class javadoc code sample, version 2. */ |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1900 |
public static <E> void forEach2(E[] array, Consumer<E> action) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1901 |
class Task extends CountedCompleter<Void> { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1902 |
final int lo, hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1903 |
Task(Task parent, int lo, int hi) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1904 |
super(parent); this.lo = lo; this.hi = hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1905 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1906 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1907 |
public void compute() { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1908 |
if (hi - lo >= 2) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1909 |
int mid = (lo + hi) >>> 1; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1910 |
setPendingCount(1); // looks off by one, but correct! |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1911 |
new Task(this, mid, hi).fork(); // right child |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1912 |
new Task(this, lo, mid).compute(); // direct invoke |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1913 |
} else { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1914 |
if (hi > lo) |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1915 |
action.accept(array[lo]); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1916 |
tryComplete(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1917 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1918 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1919 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1920 |
new Task(null, 0, array.length).invoke(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1921 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1922 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1923 |
/** CountedCompleter class javadoc code sample, version 3. */ |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1924 |
public static <E> void forEach3(E[] array, Consumer<E> action) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1925 |
class Task extends CountedCompleter<Void> { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1926 |
final int lo, hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1927 |
Task(Task parent, int lo, int hi) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1928 |
super(parent); this.lo = lo; this.hi = hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1929 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1930 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1931 |
public void compute() { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1932 |
int n = hi - lo; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1933 |
for (; n >= 2; n /= 2) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1934 |
addToPendingCount(1); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1935 |
new Task(this, lo + n/2, lo + n).fork(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1936 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1937 |
if (n > 0) |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1938 |
action.accept(array[lo]); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1939 |
propagateCompletion(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1940 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1941 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1942 |
new Task(null, 0, array.length).invoke(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1943 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1944 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1945 |
/** CountedCompleter class javadoc code sample, version 4. */ |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1946 |
public static <E> void forEach4(E[] array, Consumer<E> action) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1947 |
class Task extends CountedCompleter<Void> { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1948 |
final int lo, hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1949 |
Task(Task parent, int lo, int hi) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1950 |
super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo)); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1951 |
this.lo = lo; this.hi = hi; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1952 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1953 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1954 |
public void compute() { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1955 |
for (int n = hi - lo; n >= 2; n /= 2) |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1956 |
new Task(this, lo + n/2, lo + n).fork(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1957 |
action.accept(array[lo]); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1958 |
propagateCompletion(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1959 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1960 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1961 |
if (array.length > 0) |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1962 |
new Task(null, 0, array.length).invoke(); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1963 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1964 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1965 |
void testRecursiveDecomposition( |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1966 |
BiConsumer<Integer[], Consumer<Integer>> action) { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1967 |
int n = ThreadLocalRandom.current().nextInt(8); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1968 |
Integer[] a = new Integer[n]; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1969 |
for (int i = 0; i < n; i++) a[i] = i + 1; |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1970 |
AtomicInteger ai = new AtomicInteger(0); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1971 |
action.accept(a, (x) -> ai.addAndGet(x)); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1972 |
assertEquals(n * (n + 1) / 2, ai.get()); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1973 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1974 |
|
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1975 |
/** |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1976 |
* Variants of divide-by-two recursive decomposition into leaf tasks, |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1977 |
* as described in the CountedCompleter class javadoc code samples |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1978 |
*/ |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1979 |
public void testRecursiveDecomposition() { |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1980 |
testRecursiveDecomposition(CountedCompleterTest::forEach1); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1981 |
testRecursiveDecomposition(CountedCompleterTest::forEach2); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1982 |
testRecursiveDecomposition(CountedCompleterTest::forEach3); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1983 |
testRecursiveDecomposition(CountedCompleterTest::forEach4); |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1984 |
} |
196b358234d8
8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents:
35394
diff
changeset
|
1985 |
|
35394 | 1986 |
} |