3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
< |
import java.util.concurrent.ExecutionException; |
6 |
> |
|
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
> |
|
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
+ |
import java.util.concurrent.CountedCompleter; |
13 |
+ |
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
16 |
< |
import java.util.concurrent.CountedCompleter; |
11 |
< |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
< |
import java.util.concurrent.RecursiveAction; |
13 |
< |
import java.util.concurrent.TimeUnit; |
16 |
> |
import java.util.concurrent.ThreadLocalRandom; |
17 |
|
import java.util.concurrent.TimeoutException; |
18 |
< |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
19 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
< |
import java.util.HashSet; |
22 |
< |
import junit.framework.*; |
18 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
19 |
> |
import java.util.concurrent.atomic.AtomicReference; |
20 |
> |
import java.util.function.BiConsumer; |
21 |
> |
import java.util.function.Consumer; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
> |
import junit.framework.TestSuite; |
25 |
|
|
26 |
|
public class CountedCompleterTest extends JSR166TestCase { |
27 |
|
|
28 |
|
public static void main(String[] args) { |
29 |
< |
junit.textui.TestRunner.run(suite()); |
29 |
> |
main(suite(), args); |
30 |
|
} |
31 |
|
|
32 |
|
public static Test suite() { |
52 |
|
} |
53 |
|
|
54 |
|
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { |
55 |
< |
try { |
55 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
56 |
|
assertFalse(a.isDone()); |
57 |
|
assertFalse(a.isCompletedNormally()); |
58 |
|
assertFalse(a.isCompletedAbnormally()); |
68 |
|
assertFalse(a.isCancelled()); |
69 |
|
assertNull(a.getException()); |
70 |
|
assertNull(a.getRawResult()); |
66 |
– |
} finally { |
67 |
– |
joinPool(pool); |
71 |
|
} |
72 |
|
} |
73 |
|
|
86 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
87 |
|
} |
88 |
|
|
89 |
< |
<T> void checkCompletedNormally(CountedCompleter<T> a) { |
87 |
< |
checkCompletedNormally(a, null); |
88 |
< |
} |
89 |
< |
|
90 |
< |
<T> void checkCompletedNormally(CountedCompleter<T> a, T expected) { |
89 |
> |
void checkCompletedNormally(CountedCompleter<?> a) { |
90 |
|
assertTrue(a.isDone()); |
91 |
|
assertFalse(a.isCancelled()); |
92 |
|
assertTrue(a.isCompletedNormally()); |
93 |
|
assertFalse(a.isCompletedAbnormally()); |
94 |
|
assertNull(a.getException()); |
95 |
< |
assertSame(expected, a.getRawResult()); |
95 |
> |
assertNull(a.getRawResult()); |
96 |
|
|
97 |
|
{ |
98 |
|
Thread.currentThread().interrupt(); |
99 |
< |
long t0 = System.nanoTime(); |
100 |
< |
assertSame(expected, a.join()); |
101 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
99 |
> |
long startTime = System.nanoTime(); |
100 |
> |
assertNull(a.join()); |
101 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
102 |
|
Thread.interrupted(); |
103 |
|
} |
104 |
|
|
105 |
|
{ |
106 |
|
Thread.currentThread().interrupt(); |
107 |
< |
long t0 = System.nanoTime(); |
107 |
> |
long startTime = System.nanoTime(); |
108 |
|
a.quietlyJoin(); // should be no-op |
109 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
109 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
110 |
|
Thread.interrupted(); |
111 |
|
} |
112 |
|
|
113 |
|
assertFalse(a.cancel(false)); |
114 |
|
assertFalse(a.cancel(true)); |
115 |
|
try { |
116 |
< |
assertSame(expected, a.get()); |
116 |
> |
assertNull(a.get()); |
117 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
|
try { |
119 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
119 |
> |
assertNull(a.get(5L, SECONDS)); |
120 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
|
} |
122 |
|
|
139 |
|
Thread.interrupted(); |
140 |
|
|
141 |
|
{ |
142 |
< |
long t0 = System.nanoTime(); |
142 |
> |
long startTime = System.nanoTime(); |
143 |
|
a.quietlyJoin(); // should be no-op |
144 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
144 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
145 |
|
} |
146 |
|
|
147 |
|
try { |
177 |
|
Thread.interrupted(); |
178 |
|
|
179 |
|
{ |
180 |
< |
long t0 = System.nanoTime(); |
180 |
> |
long startTime = System.nanoTime(); |
181 |
|
a.quietlyJoin(); // should be no-op |
182 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
182 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
183 |
|
} |
184 |
|
|
185 |
|
try { |
195 |
|
} catch (ExecutionException success) { |
196 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
197 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
198 |
+ |
|
199 |
+ |
try { |
200 |
+ |
a.invoke(); |
201 |
+ |
shouldThrow(); |
202 |
+ |
} catch (Throwable success) { |
203 |
+ |
assertSame(t, success); |
204 |
+ |
} |
205 |
|
} |
206 |
|
|
207 |
|
public static final class FJException extends RuntimeException { |
208 |
|
FJException() { super(); } |
209 |
|
} |
210 |
|
|
211 |
< |
static final class NoopCountedCompleter extends CountedCompleter { |
212 |
< |
boolean post; // set true if onCompletion called |
213 |
< |
NoopCountedCompleter() { super(); } |
214 |
< |
NoopCountedCompleter(CountedCompleter p) { super(p); } |
215 |
< |
public void compute() {} |
216 |
< |
public final void onCompletion(CountedCompleter caller) { |
217 |
< |
post = true; |
211 |
> |
abstract class CheckedCC extends CountedCompleter<Object> { |
212 |
> |
final AtomicInteger computeN = new AtomicInteger(0); |
213 |
> |
final AtomicInteger onCompletionN = new AtomicInteger(0); |
214 |
> |
final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); |
215 |
> |
final AtomicInteger setRawResultN = new AtomicInteger(0); |
216 |
> |
final AtomicReference<Object> rawResult = new AtomicReference<Object>(null); |
217 |
> |
int computeN() { return computeN.get(); } |
218 |
> |
int onCompletionN() { return onCompletionN.get(); } |
219 |
> |
int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } |
220 |
> |
int setRawResultN() { return setRawResultN.get(); } |
221 |
> |
|
222 |
> |
CheckedCC() { super(); } |
223 |
> |
CheckedCC(CountedCompleter p) { super(p); } |
224 |
> |
CheckedCC(CountedCompleter p, int n) { super(p, n); } |
225 |
> |
abstract void realCompute(); |
226 |
> |
public final void compute() { |
227 |
> |
computeN.incrementAndGet(); |
228 |
> |
realCompute(); |
229 |
> |
} |
230 |
> |
public void onCompletion(CountedCompleter caller) { |
231 |
> |
onCompletionN.incrementAndGet(); |
232 |
> |
super.onCompletion(caller); |
233 |
> |
} |
234 |
> |
public boolean onExceptionalCompletion(Throwable ex, |
235 |
> |
CountedCompleter caller) { |
236 |
> |
onExceptionalCompletionN.incrementAndGet(); |
237 |
> |
assertNotNull(ex); |
238 |
> |
assertTrue(isCompletedAbnormally()); |
239 |
> |
assertTrue(super.onExceptionalCompletion(ex, caller)); |
240 |
> |
return true; |
241 |
> |
} |
242 |
> |
protected void setRawResult(Object t) { |
243 |
> |
setRawResultN.incrementAndGet(); |
244 |
> |
rawResult.set(t); |
245 |
> |
super.setRawResult(t); |
246 |
> |
} |
247 |
> |
void checkIncomplete() { |
248 |
> |
assertEquals(0, computeN()); |
249 |
> |
assertEquals(0, onCompletionN()); |
250 |
> |
assertEquals(0, onExceptionalCompletionN()); |
251 |
> |
assertEquals(0, setRawResultN()); |
252 |
> |
checkNotDone(this); |
253 |
> |
} |
254 |
> |
void checkCompletes(Object rawResult) { |
255 |
> |
checkIncomplete(); |
256 |
> |
int pendingCount = getPendingCount(); |
257 |
> |
complete(rawResult); |
258 |
> |
assertEquals(pendingCount, getPendingCount()); |
259 |
> |
assertEquals(0, computeN()); |
260 |
> |
assertEquals(1, onCompletionN()); |
261 |
> |
assertEquals(0, onExceptionalCompletionN()); |
262 |
> |
assertEquals(1, setRawResultN()); |
263 |
> |
assertSame(rawResult, this.rawResult.get()); |
264 |
> |
checkCompletedNormally(this); |
265 |
> |
} |
266 |
> |
void checkCompletesExceptionally(Throwable ex) { |
267 |
> |
checkIncomplete(); |
268 |
> |
completeExceptionally(ex); |
269 |
> |
checkCompletedExceptionally(ex); |
270 |
> |
} |
271 |
> |
void checkCompletedExceptionally(Throwable ex) { |
272 |
> |
assertEquals(0, computeN()); |
273 |
> |
assertEquals(0, onCompletionN()); |
274 |
> |
assertEquals(1, onExceptionalCompletionN()); |
275 |
> |
assertEquals(0, setRawResultN()); |
276 |
> |
assertNull(this.rawResult.get()); |
277 |
> |
checkCompletedAbnormally(this, ex); |
278 |
|
} |
279 |
|
} |
280 |
|
|
281 |
+ |
final class NoopCC extends CheckedCC { |
282 |
+ |
NoopCC() { super(); } |
283 |
+ |
NoopCC(CountedCompleter p) { super(p); } |
284 |
+ |
NoopCC(CountedCompleter p, int initialPendingCount) { |
285 |
+ |
super(p, initialPendingCount); |
286 |
+ |
} |
287 |
+ |
protected void realCompute() {} |
288 |
+ |
} |
289 |
+ |
|
290 |
|
/** |
291 |
|
* A newly constructed CountedCompleter is not completed; |
292 |
< |
* complete() causes completion. |
292 |
> |
* complete() causes completion. pendingCount is ignored. |
293 |
|
*/ |
294 |
|
public void testComplete() { |
295 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
296 |
< |
assertFalse(a.isDone()); |
297 |
< |
assertFalse(a.isCompletedNormally()); |
298 |
< |
assertFalse(a.isCompletedAbnormally()); |
299 |
< |
assertFalse(a.isCancelled()); |
300 |
< |
assertNull(a.getException()); |
301 |
< |
assertNull(a.getRawResult()); |
302 |
< |
assertFalse(a.post); |
303 |
< |
a.complete(null); |
304 |
< |
assertTrue(a.post); |
305 |
< |
assertTrue(a.isDone()); |
231 |
< |
assertTrue(a.isCompletedNormally()); |
232 |
< |
assertFalse(a.isCompletedAbnormally()); |
233 |
< |
assertFalse(a.isCancelled()); |
234 |
< |
assertNull(a.getException()); |
235 |
< |
assertNull(a.getRawResult()); |
295 |
> |
for (Object x : new Object[] { Boolean.TRUE, null }) { |
296 |
> |
for (int pendingCount : new int[] { 0, 42 }) { |
297 |
> |
testComplete(new NoopCC(), x, pendingCount); |
298 |
> |
testComplete(new NoopCC(new NoopCC()), x, pendingCount); |
299 |
> |
} |
300 |
> |
} |
301 |
> |
} |
302 |
> |
void testComplete(NoopCC cc, Object x, int pendingCount) { |
303 |
> |
cc.setPendingCount(pendingCount); |
304 |
> |
cc.checkCompletes(x); |
305 |
> |
assertEquals(pendingCount, cc.getPendingCount()); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
|
* completeExceptionally completes exceptionally |
310 |
|
*/ |
311 |
|
public void testCompleteExceptionally() { |
312 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
313 |
< |
assertFalse(a.isDone()); |
314 |
< |
assertFalse(a.isCompletedNormally()); |
315 |
< |
assertFalse(a.isCompletedAbnormally()); |
316 |
< |
assertFalse(a.isCancelled()); |
317 |
< |
assertNull(a.getException()); |
318 |
< |
assertNull(a.getRawResult()); |
319 |
< |
assertFalse(a.post); |
320 |
< |
a.completeExceptionally(new FJException()); |
321 |
< |
assertFalse(a.post); |
322 |
< |
assertTrue(a.isDone()); |
323 |
< |
assertFalse(a.isCompletedNormally()); |
324 |
< |
assertTrue(a.isCompletedAbnormally()); |
325 |
< |
assertFalse(a.isCancelled()); |
326 |
< |
assertTrue(a.getException() instanceof FJException); |
327 |
< |
assertNull(a.getRawResult()); |
312 |
> |
new NoopCC() |
313 |
> |
.checkCompletesExceptionally(new FJException()); |
314 |
> |
new NoopCC(new NoopCC()) |
315 |
> |
.checkCompletesExceptionally(new FJException()); |
316 |
> |
} |
317 |
> |
|
318 |
> |
/** |
319 |
> |
* completeExceptionally(null) surprisingly has the same effect as |
320 |
> |
* completeExceptionally(new RuntimeException()) |
321 |
> |
*/ |
322 |
> |
public void testCompleteExceptionally_null() { |
323 |
> |
NoopCC a = new NoopCC(); |
324 |
> |
a.completeExceptionally(null); |
325 |
> |
try { |
326 |
> |
a.invoke(); |
327 |
> |
shouldThrow(); |
328 |
> |
} catch (RuntimeException success) { |
329 |
> |
assertSame(success.getClass(), RuntimeException.class); |
330 |
> |
assertNull(success.getCause()); |
331 |
> |
a.checkCompletedExceptionally(success); |
332 |
> |
} |
333 |
|
} |
334 |
|
|
335 |
|
/** |
336 |
|
* setPendingCount sets the reported pending count |
337 |
|
*/ |
338 |
|
public void testSetPendingCount() { |
339 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
339 |
> |
NoopCC a = new NoopCC(); |
340 |
|
assertEquals(0, a.getPendingCount()); |
341 |
< |
a.setPendingCount(1); |
342 |
< |
assertEquals(1, a.getPendingCount()); |
343 |
< |
a.setPendingCount(27); |
344 |
< |
assertEquals(27, a.getPendingCount()); |
341 |
> |
int[] vals = { |
342 |
> |
-1, 0, 1, |
343 |
> |
Integer.MIN_VALUE, |
344 |
> |
Integer.MAX_VALUE, |
345 |
> |
}; |
346 |
> |
for (int val : vals) { |
347 |
> |
a.setPendingCount(val); |
348 |
> |
assertEquals(val, a.getPendingCount()); |
349 |
> |
} |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
|
* addToPendingCount adds to the reported pending count |
354 |
|
*/ |
355 |
|
public void testAddToPendingCount() { |
356 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
356 |
> |
NoopCC a = new NoopCC(); |
357 |
|
assertEquals(0, a.getPendingCount()); |
358 |
|
a.addToPendingCount(1); |
359 |
|
assertEquals(1, a.getPendingCount()); |
360 |
|
a.addToPendingCount(27); |
361 |
|
assertEquals(28, a.getPendingCount()); |
362 |
+ |
a.addToPendingCount(-28); |
363 |
+ |
assertEquals(0, a.getPendingCount()); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
|
* decrementPendingCountUnlessZero decrements reported pending |
368 |
|
* count unless zero |
369 |
|
*/ |
370 |
< |
public void testDecrementPendingCount() { |
371 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
372 |
< |
assertEquals(0, a.getPendingCount()); |
373 |
< |
a.addToPendingCount(1); |
370 |
> |
public void testDecrementPendingCountUnlessZero() { |
371 |
> |
NoopCC a = new NoopCC(null, 2); |
372 |
> |
assertEquals(2, a.getPendingCount()); |
373 |
> |
assertEquals(2, a.decrementPendingCountUnlessZero()); |
374 |
|
assertEquals(1, a.getPendingCount()); |
375 |
< |
a.decrementPendingCountUnlessZero(); |
375 |
> |
assertEquals(1, a.decrementPendingCountUnlessZero()); |
376 |
|
assertEquals(0, a.getPendingCount()); |
377 |
< |
a.decrementPendingCountUnlessZero(); |
377 |
> |
assertEquals(0, a.decrementPendingCountUnlessZero()); |
378 |
|
assertEquals(0, a.getPendingCount()); |
379 |
+ |
a.setPendingCount(-1); |
380 |
+ |
assertEquals(-1, a.decrementPendingCountUnlessZero()); |
381 |
+ |
assertEquals(-2, a.getPendingCount()); |
382 |
+ |
} |
383 |
+ |
|
384 |
+ |
/** |
385 |
+ |
* compareAndSetPendingCount compares and sets the reported |
386 |
+ |
* pending count |
387 |
+ |
*/ |
388 |
+ |
public void testCompareAndSetPendingCount() { |
389 |
+ |
NoopCC a = new NoopCC(); |
390 |
+ |
assertEquals(0, a.getPendingCount()); |
391 |
+ |
assertTrue(a.compareAndSetPendingCount(0, 1)); |
392 |
+ |
assertEquals(1, a.getPendingCount()); |
393 |
+ |
assertTrue(a.compareAndSetPendingCount(1, 2)); |
394 |
+ |
assertEquals(2, a.getPendingCount()); |
395 |
+ |
assertFalse(a.compareAndSetPendingCount(1, 3)); |
396 |
+ |
assertEquals(2, a.getPendingCount()); |
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
|
* getCompleter returns parent or null if at root |
401 |
|
*/ |
402 |
|
public void testGetCompleter() { |
403 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
403 |
> |
NoopCC a = new NoopCC(); |
404 |
|
assertNull(a.getCompleter()); |
405 |
< |
CountedCompleter b = new NoopCountedCompleter(a); |
406 |
< |
assertEquals(a, b.getCompleter()); |
405 |
> |
CountedCompleter b = new NoopCC(a); |
406 |
> |
assertSame(a, b.getCompleter()); |
407 |
> |
CountedCompleter c = new NoopCC(b); |
408 |
> |
assertSame(b, c.getCompleter()); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
|
* getRoot returns self if no parent, else parent's root |
413 |
|
*/ |
414 |
|
public void testGetRoot() { |
415 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
416 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
417 |
< |
assertEquals(a, a.getRoot()); |
418 |
< |
assertEquals(a, b.getRoot()); |
415 |
> |
NoopCC a = new NoopCC(); |
416 |
> |
NoopCC b = new NoopCC(a); |
417 |
> |
NoopCC c = new NoopCC(b); |
418 |
> |
assertSame(a, a.getRoot()); |
419 |
> |
assertSame(a, b.getRoot()); |
420 |
> |
assertSame(a, c.getRoot()); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
< |
* tryComplete causes completion if pending count is zero |
424 |
> |
* tryComplete decrements pending count unless zero, in which case |
425 |
> |
* causes completion |
426 |
|
*/ |
427 |
< |
public void testTryComplete1() { |
428 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
427 |
> |
public void testTryComplete() { |
428 |
> |
NoopCC a = new NoopCC(); |
429 |
|
assertEquals(0, a.getPendingCount()); |
430 |
+ |
int n = 3; |
431 |
+ |
a.setPendingCount(n); |
432 |
+ |
for (; n > 0; n--) { |
433 |
+ |
assertEquals(n, a.getPendingCount()); |
434 |
+ |
a.tryComplete(); |
435 |
+ |
a.checkIncomplete(); |
436 |
+ |
assertEquals(n - 1, a.getPendingCount()); |
437 |
+ |
} |
438 |
|
a.tryComplete(); |
439 |
< |
assertTrue(a.post); |
440 |
< |
assertTrue(a.isDone()); |
439 |
> |
assertEquals(0, a.computeN()); |
440 |
> |
assertEquals(1, a.onCompletionN()); |
441 |
> |
assertEquals(0, a.onExceptionalCompletionN()); |
442 |
> |
assertEquals(0, a.setRawResultN()); |
443 |
> |
checkCompletedNormally(a); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
< |
* propagateCompletion causes completion without invoking |
448 |
< |
* onCompletion if pending count is zero |
447 |
> |
* propagateCompletion decrements pending count unless zero, in |
448 |
> |
* which case causes completion, without invoking onCompletion |
449 |
|
*/ |
450 |
|
public void testPropagateCompletion() { |
451 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
451 |
> |
NoopCC a = new NoopCC(); |
452 |
|
assertEquals(0, a.getPendingCount()); |
453 |
+ |
int n = 3; |
454 |
+ |
a.setPendingCount(n); |
455 |
+ |
for (; n > 0; n--) { |
456 |
+ |
assertEquals(n, a.getPendingCount()); |
457 |
+ |
a.propagateCompletion(); |
458 |
+ |
a.checkIncomplete(); |
459 |
+ |
assertEquals(n - 1, a.getPendingCount()); |
460 |
+ |
} |
461 |
|
a.propagateCompletion(); |
462 |
< |
assertFalse(a.post); |
463 |
< |
assertTrue(a.isDone()); |
464 |
< |
} |
465 |
< |
|
466 |
< |
/** |
343 |
< |
* tryComplete decrements pending count unless zero |
344 |
< |
*/ |
345 |
< |
public void testTryComplete2() { |
346 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
347 |
< |
assertEquals(0, a.getPendingCount()); |
348 |
< |
a.setPendingCount(1); |
349 |
< |
a.tryComplete(); |
350 |
< |
assertFalse(a.post); |
351 |
< |
assertFalse(a.isDone()); |
352 |
< |
assertEquals(0, a.getPendingCount()); |
353 |
< |
a.tryComplete(); |
354 |
< |
assertTrue(a.post); |
355 |
< |
assertTrue(a.isDone()); |
462 |
> |
assertEquals(0, a.computeN()); |
463 |
> |
assertEquals(0, a.onCompletionN()); |
464 |
> |
assertEquals(0, a.onExceptionalCompletionN()); |
465 |
> |
assertEquals(0, a.setRawResultN()); |
466 |
> |
checkCompletedNormally(a); |
467 |
|
} |
468 |
|
|
469 |
|
/** |
470 |
|
* firstComplete returns this if pending count is zero else null |
471 |
|
*/ |
472 |
|
public void testFirstComplete() { |
473 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
473 |
> |
NoopCC a = new NoopCC(); |
474 |
|
a.setPendingCount(1); |
475 |
|
assertNull(a.firstComplete()); |
476 |
< |
assertEquals(a, a.firstComplete()); |
476 |
> |
a.checkIncomplete(); |
477 |
> |
assertSame(a, a.firstComplete()); |
478 |
> |
a.checkIncomplete(); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
483 |
|
* zero else null |
484 |
|
*/ |
485 |
|
public void testNextComplete() { |
486 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
487 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
486 |
> |
NoopCC a = new NoopCC(); |
487 |
> |
NoopCC b = new NoopCC(a); |
488 |
|
a.setPendingCount(1); |
489 |
|
b.setPendingCount(1); |
490 |
|
assertNull(b.firstComplete()); |
491 |
< |
CountedCompleter c = b.firstComplete(); |
492 |
< |
assertEquals(b, c); |
493 |
< |
CountedCompleter d = c.nextComplete(); |
494 |
< |
assertNull(d); |
495 |
< |
CountedCompleter e = c.nextComplete(); |
496 |
< |
assertEquals(a, e); |
491 |
> |
assertSame(b, b.firstComplete()); |
492 |
> |
assertNull(b.nextComplete()); |
493 |
> |
a.checkIncomplete(); |
494 |
> |
b.checkIncomplete(); |
495 |
> |
assertSame(a, b.nextComplete()); |
496 |
> |
assertSame(a, b.nextComplete()); |
497 |
> |
a.checkIncomplete(); |
498 |
> |
b.checkIncomplete(); |
499 |
> |
assertNull(a.nextComplete()); |
500 |
> |
b.checkIncomplete(); |
501 |
> |
checkCompletedNormally(a); |
502 |
|
} |
503 |
|
|
504 |
|
/** |
505 |
< |
* quietlyCompleteRoot completes root task |
505 |
> |
* quietlyCompleteRoot completes root task and only root task |
506 |
|
*/ |
507 |
|
public void testQuietlyCompleteRoot() { |
508 |
< |
NoopCountedCompleter a = new NoopCountedCompleter(); |
509 |
< |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
508 |
> |
NoopCC a = new NoopCC(); |
509 |
> |
NoopCC b = new NoopCC(a); |
510 |
> |
NoopCC c = new NoopCC(b); |
511 |
|
a.setPendingCount(1); |
512 |
|
b.setPendingCount(1); |
513 |
< |
b.quietlyCompleteRoot(); |
513 |
> |
c.setPendingCount(1); |
514 |
> |
c.quietlyCompleteRoot(); |
515 |
|
assertTrue(a.isDone()); |
516 |
|
assertFalse(b.isDone()); |
517 |
+ |
assertFalse(c.isDone()); |
518 |
|
} |
519 |
|
|
520 |
|
// Invocation tests use some interdependent task classes |
521 |
|
// to better test propagation etc |
522 |
|
|
523 |
< |
|
524 |
< |
// Version of Fibonacci with different classes for left vs right forks |
525 |
< |
abstract static class CCF extends CountedCompleter { |
523 |
> |
/** |
524 |
> |
* Version of Fibonacci with different classes for left vs right forks |
525 |
> |
*/ |
526 |
> |
abstract class CCF extends CheckedCC { |
527 |
|
int number; |
528 |
|
int rnumber; |
529 |
|
|
532 |
|
this.number = n; |
533 |
|
} |
534 |
|
|
535 |
< |
public final void compute() { |
414 |
< |
CountedCompleter p; |
535 |
> |
protected final void realCompute() { |
536 |
|
CCF f = this; |
537 |
|
int n = number; |
538 |
|
while (n >= 2) { |
539 |
|
new RCCF(f, n - 2).fork(); |
540 |
|
f = new LCCF(f, --n); |
541 |
|
} |
542 |
< |
f.number = n; |
422 |
< |
f.onCompletion(f); |
423 |
< |
if ((p = f.getCompleter()) != null) |
424 |
< |
p.tryComplete(); |
425 |
< |
else |
426 |
< |
f.quietlyComplete(); |
542 |
> |
f.complete(null); |
543 |
|
} |
544 |
|
} |
545 |
|
|
546 |
< |
static final class LCCF extends CCF { |
546 |
> |
final class LCCF extends CCF { |
547 |
> |
public LCCF(int n) { this(null, n); } |
548 |
|
public LCCF(CountedCompleter parent, int n) { |
549 |
|
super(parent, n); |
550 |
|
} |
551 |
|
public final void onCompletion(CountedCompleter caller) { |
552 |
+ |
super.onCompletion(caller); |
553 |
|
CCF p = (CCF)getCompleter(); |
554 |
|
int n = number + rnumber; |
555 |
|
if (p != null) |
558 |
|
number = n; |
559 |
|
} |
560 |
|
} |
561 |
< |
static final class RCCF extends CCF { |
561 |
> |
final class RCCF extends CCF { |
562 |
|
public RCCF(CountedCompleter parent, int n) { |
563 |
|
super(parent, n); |
564 |
|
} |
565 |
|
public final void onCompletion(CountedCompleter caller) { |
566 |
+ |
super.onCompletion(caller); |
567 |
|
CCF p = (CCF)getCompleter(); |
568 |
|
int n = number + rnumber; |
569 |
|
if (p != null) |
574 |
|
} |
575 |
|
|
576 |
|
// Version of CCF with forced failure in left completions |
577 |
< |
abstract static class FailingCCF extends CountedCompleter { |
577 |
> |
abstract class FailingCCF extends CheckedCC { |
578 |
|
int number; |
579 |
|
int rnumber; |
580 |
|
|
583 |
|
this.number = n; |
584 |
|
} |
585 |
|
|
586 |
< |
public final void compute() { |
468 |
< |
CountedCompleter p; |
586 |
> |
protected final void realCompute() { |
587 |
|
FailingCCF f = this; |
588 |
|
int n = number; |
589 |
|
while (n >= 2) { |
590 |
|
new RFCCF(f, n - 2).fork(); |
591 |
|
f = new LFCCF(f, --n); |
592 |
|
} |
593 |
< |
f.number = n; |
476 |
< |
f.onCompletion(f); |
477 |
< |
if ((p = f.getCompleter()) != null) |
478 |
< |
p.tryComplete(); |
479 |
< |
else |
480 |
< |
f.quietlyComplete(); |
593 |
> |
f.complete(null); |
594 |
|
} |
595 |
|
} |
596 |
|
|
597 |
< |
static final class LFCCF extends FailingCCF { |
597 |
> |
final class LFCCF extends FailingCCF { |
598 |
> |
public LFCCF(int n) { this(null, n); } |
599 |
|
public LFCCF(CountedCompleter parent, int n) { |
600 |
|
super(parent, n); |
601 |
|
} |
602 |
|
public final void onCompletion(CountedCompleter caller) { |
603 |
+ |
super.onCompletion(caller); |
604 |
|
FailingCCF p = (FailingCCF)getCompleter(); |
605 |
|
int n = number + rnumber; |
606 |
|
if (p != null) |
609 |
|
number = n; |
610 |
|
} |
611 |
|
} |
612 |
< |
static final class RFCCF extends FailingCCF { |
612 |
> |
final class RFCCF extends FailingCCF { |
613 |
|
public RFCCF(CountedCompleter parent, int n) { |
614 |
|
super(parent, n); |
615 |
|
} |
616 |
|
public final void onCompletion(CountedCompleter caller) { |
617 |
+ |
super.onCompletion(caller); |
618 |
|
completeExceptionally(new FJException()); |
619 |
|
} |
620 |
|
} |
627 |
|
public void testInvoke() { |
628 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
629 |
|
protected void realCompute() { |
630 |
< |
CCF f = new LCCF(null, 8); |
630 |
> |
CCF f = new LCCF(8); |
631 |
|
assertNull(f.invoke()); |
632 |
|
assertEquals(21, f.number); |
633 |
|
checkCompletedNormally(f); |
643 |
|
public void testQuietlyInvoke() { |
644 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
645 |
|
protected void realCompute() { |
646 |
< |
CCF f = new LCCF(null, 8); |
646 |
> |
CCF f = new LCCF(8); |
647 |
|
f.quietlyInvoke(); |
648 |
|
assertEquals(21, f.number); |
649 |
|
checkCompletedNormally(f); |
657 |
|
public void testForkJoin() { |
658 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
659 |
|
protected void realCompute() { |
660 |
< |
CCF f = new LCCF(null, 8); |
660 |
> |
CCF f = new LCCF(8); |
661 |
|
assertSame(f, f.fork()); |
662 |
|
assertNull(f.join()); |
663 |
|
assertEquals(21, f.number); |
672 |
|
public void testForkGet() { |
673 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
674 |
|
protected void realCompute() throws Exception { |
675 |
< |
CCF f = new LCCF(null, 8); |
675 |
> |
CCF f = new LCCF(8); |
676 |
|
assertSame(f, f.fork()); |
677 |
|
assertNull(f.get()); |
678 |
|
assertEquals(21, f.number); |
687 |
|
public void testForkTimedGet() { |
688 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
689 |
|
protected void realCompute() throws Exception { |
690 |
< |
CCF f = new LCCF(null, 8); |
690 |
> |
CCF f = new LCCF(8); |
691 |
|
assertSame(f, f.fork()); |
692 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
693 |
|
assertEquals(21, f.number); |
702 |
|
public void testForkTimedGetNPE() { |
703 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
704 |
|
protected void realCompute() throws Exception { |
705 |
< |
CCF f = new LCCF(null, 8); |
705 |
> |
CCF f = new LCCF(8); |
706 |
|
assertSame(f, f.fork()); |
707 |
|
try { |
708 |
|
f.get(5L, null); |
718 |
|
public void testForkQuietlyJoin() { |
719 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
720 |
|
protected void realCompute() { |
721 |
< |
CCF f = new LCCF(null, 8); |
721 |
> |
CCF f = new LCCF(8); |
722 |
|
assertSame(f, f.fork()); |
723 |
|
f.quietlyJoin(); |
724 |
|
assertEquals(21, f.number); |
734 |
|
public void testForkHelpQuiesce() { |
735 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
736 |
|
protected void realCompute() { |
737 |
< |
CCF f = new LCCF(null, 8); |
737 |
> |
CCF f = new LCCF(8); |
738 |
|
assertSame(f, f.fork()); |
739 |
|
helpQuiesce(); |
740 |
|
assertEquals(21, f.number); |
750 |
|
public void testAbnormalInvoke() { |
751 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
752 |
|
protected void realCompute() { |
753 |
< |
FailingCCF f = new LFCCF(null, 8); |
753 |
> |
FailingCCF f = new LFCCF(8); |
754 |
|
try { |
755 |
|
f.invoke(); |
756 |
|
shouldThrow(); |
767 |
|
public void testAbnormalQuietlyInvoke() { |
768 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
769 |
|
protected void realCompute() { |
770 |
< |
FailingCCF f = new LFCCF(null, 8); |
770 |
> |
FailingCCF f = new LFCCF(8); |
771 |
|
f.quietlyInvoke(); |
772 |
|
assertTrue(f.getException() instanceof FJException); |
773 |
|
checkCompletedAbnormally(f, f.getException()); |
781 |
|
public void testAbnormalForkJoin() { |
782 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
783 |
|
protected void realCompute() { |
784 |
< |
FailingCCF f = new LFCCF(null, 8); |
784 |
> |
FailingCCF f = new LFCCF(8); |
785 |
|
assertSame(f, f.fork()); |
786 |
|
try { |
787 |
|
f.join(); |
799 |
|
public void testAbnormalForkGet() { |
800 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
801 |
|
protected void realCompute() throws Exception { |
802 |
< |
FailingCCF f = new LFCCF(null, 8); |
802 |
> |
FailingCCF f = new LFCCF(8); |
803 |
|
assertSame(f, f.fork()); |
804 |
|
try { |
805 |
|
f.get(); |
819 |
|
public void testAbnormalForkTimedGet() { |
820 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
821 |
|
protected void realCompute() throws Exception { |
822 |
< |
FailingCCF f = new LFCCF(null, 8); |
822 |
> |
FailingCCF f = new LFCCF(8); |
823 |
|
assertSame(f, f.fork()); |
824 |
|
try { |
825 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
839 |
|
public void testAbnormalForkQuietlyJoin() { |
840 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
841 |
|
protected void realCompute() { |
842 |
< |
FailingCCF f = new LFCCF(null, 8); |
842 |
> |
FailingCCF f = new LFCCF(8); |
843 |
|
assertSame(f, f.fork()); |
844 |
|
f.quietlyJoin(); |
845 |
|
assertTrue(f.getException() instanceof FJException); |
854 |
|
public void testCancelledInvoke() { |
855 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
856 |
|
protected void realCompute() { |
857 |
< |
CCF f = new LCCF(null, 8); |
857 |
> |
CCF f = new LCCF(8); |
858 |
|
assertTrue(f.cancel(true)); |
859 |
|
try { |
860 |
|
f.invoke(); |
872 |
|
public void testCancelledForkJoin() { |
873 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
874 |
|
protected void realCompute() { |
875 |
< |
CCF f = new LCCF(null, 8); |
875 |
> |
CCF f = new LCCF(8); |
876 |
|
assertTrue(f.cancel(true)); |
877 |
|
assertSame(f, f.fork()); |
878 |
|
try { |
891 |
|
public void testCancelledForkGet() { |
892 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
893 |
|
protected void realCompute() throws Exception { |
894 |
< |
CCF f = new LCCF(null, 8); |
894 |
> |
CCF f = new LCCF(8); |
895 |
|
assertTrue(f.cancel(true)); |
896 |
|
assertSame(f, f.fork()); |
897 |
|
try { |
910 |
|
public void testCancelledForkTimedGet() throws Exception { |
911 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
912 |
|
protected void realCompute() throws Exception { |
913 |
< |
CCF f = new LCCF(null, 8); |
913 |
> |
CCF f = new LCCF(8); |
914 |
|
assertTrue(f.cancel(true)); |
915 |
|
assertSame(f, f.fork()); |
916 |
|
try { |
929 |
|
public void testCancelledForkQuietlyJoin() { |
930 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
931 |
|
protected void realCompute() { |
932 |
< |
CCF f = new LCCF(null, 8); |
932 |
> |
CCF f = new LCCF(8); |
933 |
|
assertTrue(f.cancel(true)); |
934 |
|
assertSame(f, f.fork()); |
935 |
|
f.quietlyJoin(); |
1001 |
|
public void testCompleteExceptionally2() { |
1002 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1003 |
|
protected void realCompute() { |
1004 |
< |
CCF f = new LCCF(null, 8); |
1005 |
< |
f.completeExceptionally(new FJException()); |
1006 |
< |
try { |
1007 |
< |
f.invoke(); |
1008 |
< |
shouldThrow(); |
1009 |
< |
} catch (FJException success) { |
894 |
< |
checkCompletedAbnormally(f, success); |
895 |
< |
} |
1004 |
> |
CCF n = new LCCF(8); |
1005 |
> |
CCF f = new LCCF(n, 8); |
1006 |
> |
FJException ex = new FJException(); |
1007 |
> |
f.completeExceptionally(ex); |
1008 |
> |
f.checkCompletedExceptionally(ex); |
1009 |
> |
n.checkCompletedExceptionally(ex); |
1010 |
|
}}; |
1011 |
|
testInvokeOnPool(mainPool(), a); |
1012 |
|
} |
1017 |
|
public void testInvokeAll2() { |
1018 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1019 |
|
protected void realCompute() { |
1020 |
< |
CCF f = new LCCF(null, 8); |
1021 |
< |
CCF g = new LCCF(null, 9); |
1020 |
> |
CCF f = new LCCF(8); |
1021 |
> |
CCF g = new LCCF(9); |
1022 |
|
invokeAll(f, g); |
1023 |
|
assertEquals(21, f.number); |
1024 |
|
assertEquals(34, g.number); |
1034 |
|
public void testInvokeAll1() { |
1035 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1036 |
|
protected void realCompute() { |
1037 |
< |
CCF f = new LCCF(null, 8); |
1037 |
> |
CCF f = new LCCF(8); |
1038 |
|
invokeAll(f); |
1039 |
|
checkCompletedNormally(f); |
1040 |
|
assertEquals(21, f.number); |
1048 |
|
public void testInvokeAll3() { |
1049 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1050 |
|
protected void realCompute() { |
1051 |
< |
CCF f = new LCCF(null, 8); |
1052 |
< |
CCF g = new LCCF(null, 9); |
1053 |
< |
CCF h = new LCCF(null, 7); |
1051 |
> |
CCF f = new LCCF(8); |
1052 |
> |
CCF g = new LCCF(9); |
1053 |
> |
CCF h = new LCCF(7); |
1054 |
|
invokeAll(f, g, h); |
1055 |
|
assertEquals(21, f.number); |
1056 |
|
assertEquals(34, g.number); |
1068 |
|
public void testInvokeAllCollection() { |
1069 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1070 |
|
protected void realCompute() { |
1071 |
< |
CCF f = new LCCF(null, 8); |
1072 |
< |
CCF g = new LCCF(null, 9); |
1073 |
< |
CCF h = new LCCF(null, 7); |
1071 |
> |
CCF f = new LCCF(8); |
1072 |
> |
CCF g = new LCCF(9); |
1073 |
> |
CCF h = new LCCF(7); |
1074 |
|
HashSet set = new HashSet(); |
1075 |
|
set.add(f); |
1076 |
|
set.add(g); |
1092 |
|
public void testInvokeAllNPE() { |
1093 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1094 |
|
protected void realCompute() { |
1095 |
< |
CCF f = new LCCF(null, 8); |
1096 |
< |
CCF g = new LCCF(null, 9); |
1095 |
> |
CCF f = new LCCF(8); |
1096 |
> |
CCF g = new LCCF(9); |
1097 |
|
CCF h = null; |
1098 |
|
try { |
1099 |
|
invokeAll(f, g, h); |
1109 |
|
public void testAbnormalInvokeAll2() { |
1110 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1111 |
|
protected void realCompute() { |
1112 |
< |
CCF f = new LCCF(null, 8); |
1113 |
< |
FailingCCF g = new LFCCF(null, 9); |
1112 |
> |
CCF f = new LCCF(8); |
1113 |
> |
FailingCCF g = new LFCCF(9); |
1114 |
|
try { |
1115 |
|
invokeAll(f, g); |
1116 |
|
shouldThrow(); |
1127 |
|
public void testAbnormalInvokeAll1() { |
1128 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1129 |
|
protected void realCompute() { |
1130 |
< |
FailingCCF g = new LFCCF(null, 9); |
1130 |
> |
FailingCCF g = new LFCCF(9); |
1131 |
|
try { |
1132 |
|
invokeAll(g); |
1133 |
|
shouldThrow(); |
1144 |
|
public void testAbnormalInvokeAll3() { |
1145 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1146 |
|
protected void realCompute() { |
1147 |
< |
CCF f = new LCCF(null, 8); |
1148 |
< |
FailingCCF g = new LFCCF(null, 9); |
1149 |
< |
CCF h = new LCCF(null, 7); |
1147 |
> |
CCF f = new LCCF(8); |
1148 |
> |
FailingCCF g = new LFCCF(9); |
1149 |
> |
CCF h = new LCCF(7); |
1150 |
|
try { |
1151 |
|
invokeAll(f, g, h); |
1152 |
|
shouldThrow(); |
1158 |
|
} |
1159 |
|
|
1160 |
|
/** |
1161 |
< |
* invokeAll(collection) throws exception if any task does |
1161 |
> |
* invokeAll(collection) throws exception if any task does |
1162 |
|
*/ |
1163 |
|
public void testAbnormalInvokeAllCollection() { |
1164 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1165 |
|
protected void realCompute() { |
1166 |
< |
FailingCCF f = new LFCCF(null, 8); |
1167 |
< |
CCF g = new LCCF(null, 9); |
1168 |
< |
CCF h = new LCCF(null, 7); |
1166 |
> |
FailingCCF f = new LFCCF(8); |
1167 |
> |
CCF g = new LCCF(9); |
1168 |
> |
CCF h = new LCCF(7); |
1169 |
|
HashSet set = new HashSet(); |
1170 |
|
set.add(f); |
1171 |
|
set.add(g); |
1187 |
|
public void testTryUnfork() { |
1188 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1189 |
|
protected void realCompute() { |
1190 |
< |
CCF g = new LCCF(null, 9); |
1190 |
> |
CCF g = new LCCF(9); |
1191 |
|
assertSame(g, g.fork()); |
1192 |
< |
CCF f = new LCCF(null, 8); |
1192 |
> |
CCF f = new LCCF(8); |
1193 |
|
assertSame(f, f.fork()); |
1194 |
|
assertTrue(f.tryUnfork()); |
1195 |
|
helpQuiesce(); |
1206 |
|
public void testGetSurplusQueuedTaskCount() { |
1207 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1208 |
|
protected void realCompute() { |
1209 |
< |
CCF h = new LCCF(null, 7); |
1209 |
> |
CCF h = new LCCF(7); |
1210 |
|
assertSame(h, h.fork()); |
1211 |
< |
CCF g = new LCCF(null, 9); |
1211 |
> |
CCF g = new LCCF(9); |
1212 |
|
assertSame(g, g.fork()); |
1213 |
< |
CCF f = new LCCF(null, 8); |
1213 |
> |
CCF f = new LCCF(8); |
1214 |
|
assertSame(f, f.fork()); |
1215 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1216 |
|
helpQuiesce(); |
1228 |
|
public void testPeekNextLocalTask() { |
1229 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1230 |
|
protected void realCompute() { |
1231 |
< |
CCF g = new LCCF(null, 9); |
1231 |
> |
CCF g = new LCCF(9); |
1232 |
|
assertSame(g, g.fork()); |
1233 |
< |
CCF f = new LCCF(null, 8); |
1233 |
> |
CCF f = new LCCF(8); |
1234 |
|
assertSame(f, f.fork()); |
1235 |
|
assertSame(f, peekNextLocalTask()); |
1236 |
|
assertNull(f.join()); |
1248 |
|
public void testPollNextLocalTask() { |
1249 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1250 |
|
protected void realCompute() { |
1251 |
< |
CCF g = new LCCF(null, 9); |
1251 |
> |
CCF g = new LCCF(9); |
1252 |
|
assertSame(g, g.fork()); |
1253 |
< |
CCF f = new LCCF(null, 8); |
1253 |
> |
CCF f = new LCCF(8); |
1254 |
|
assertSame(f, f.fork()); |
1255 |
|
assertSame(f, pollNextLocalTask()); |
1256 |
|
helpQuiesce(); |
1267 |
|
public void testPollTask() { |
1268 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1269 |
|
protected void realCompute() { |
1270 |
< |
CCF g = new LCCF(null, 9); |
1270 |
> |
CCF g = new LCCF(9); |
1271 |
|
assertSame(g, g.fork()); |
1272 |
< |
CCF f = new LCCF(null, 8); |
1272 |
> |
CCF f = new LCCF(8); |
1273 |
|
assertSame(f, f.fork()); |
1274 |
|
assertSame(f, pollTask()); |
1275 |
|
helpQuiesce(); |
1285 |
|
public void testPeekNextLocalTaskAsync() { |
1286 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1287 |
|
protected void realCompute() { |
1288 |
< |
CCF g = new LCCF(null, 9); |
1288 |
> |
CCF g = new LCCF(9); |
1289 |
|
assertSame(g, g.fork()); |
1290 |
< |
CCF f = new LCCF(null, 8); |
1290 |
> |
CCF f = new LCCF(8); |
1291 |
|
assertSame(f, f.fork()); |
1292 |
|
assertSame(g, peekNextLocalTask()); |
1293 |
|
assertNull(f.join()); |
1306 |
|
public void testPollNextLocalTaskAsync() { |
1307 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1308 |
|
protected void realCompute() { |
1309 |
< |
CCF g = new LCCF(null, 9); |
1309 |
> |
CCF g = new LCCF(9); |
1310 |
|
assertSame(g, g.fork()); |
1311 |
< |
CCF f = new LCCF(null, 8); |
1311 |
> |
CCF f = new LCCF(8); |
1312 |
|
assertSame(f, f.fork()); |
1313 |
|
assertSame(g, pollNextLocalTask()); |
1314 |
|
helpQuiesce(); |
1326 |
|
public void testPollTaskAsync() { |
1327 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1328 |
|
protected void realCompute() { |
1329 |
< |
CCF g = new LCCF(null, 9); |
1329 |
> |
CCF g = new LCCF(9); |
1330 |
|
assertSame(g, g.fork()); |
1331 |
< |
CCF f = new LCCF(null, 8); |
1331 |
> |
CCF f = new LCCF(8); |
1332 |
|
assertSame(f, f.fork()); |
1333 |
|
assertSame(g, pollTask()); |
1334 |
|
helpQuiesce(); |
1349 |
|
public void testInvokeSingleton() { |
1350 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1351 |
|
protected void realCompute() { |
1352 |
< |
CCF f = new LCCF(null, 8); |
1352 |
> |
CCF f = new LCCF(8); |
1353 |
|
assertNull(f.invoke()); |
1354 |
|
assertEquals(21, f.number); |
1355 |
|
checkCompletedNormally(f); |
1365 |
|
public void testQuietlyInvokeSingleton() { |
1366 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1367 |
|
protected void realCompute() { |
1368 |
< |
CCF f = new LCCF(null, 8); |
1368 |
> |
CCF f = new LCCF(8); |
1369 |
|
f.quietlyInvoke(); |
1370 |
|
assertEquals(21, f.number); |
1371 |
|
checkCompletedNormally(f); |
1379 |
|
public void testForkJoinSingleton() { |
1380 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1381 |
|
protected void realCompute() { |
1382 |
< |
CCF f = new LCCF(null, 8); |
1382 |
> |
CCF f = new LCCF(8); |
1383 |
|
assertSame(f, f.fork()); |
1384 |
|
assertNull(f.join()); |
1385 |
|
assertEquals(21, f.number); |
1394 |
|
public void testForkGetSingleton() { |
1395 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1396 |
|
protected void realCompute() throws Exception { |
1397 |
< |
CCF f = new LCCF(null, 8); |
1397 |
> |
CCF f = new LCCF(8); |
1398 |
|
assertSame(f, f.fork()); |
1399 |
|
assertNull(f.get()); |
1400 |
|
assertEquals(21, f.number); |
1409 |
|
public void testForkTimedGetSingleton() { |
1410 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1411 |
|
protected void realCompute() throws Exception { |
1412 |
< |
CCF f = new LCCF(null, 8); |
1412 |
> |
CCF f = new LCCF(8); |
1413 |
|
assertSame(f, f.fork()); |
1414 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1415 |
|
assertEquals(21, f.number); |
1424 |
|
public void testForkTimedGetNPESingleton() { |
1425 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1426 |
|
protected void realCompute() throws Exception { |
1427 |
< |
CCF f = new LCCF(null, 8); |
1427 |
> |
CCF f = new LCCF(8); |
1428 |
|
assertSame(f, f.fork()); |
1429 |
|
try { |
1430 |
|
f.get(5L, null); |
1440 |
|
public void testForkQuietlyJoinSingleton() { |
1441 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1442 |
|
protected void realCompute() { |
1443 |
< |
CCF f = new LCCF(null, 8); |
1443 |
> |
CCF f = new LCCF(8); |
1444 |
|
assertSame(f, f.fork()); |
1445 |
|
f.quietlyJoin(); |
1446 |
|
assertEquals(21, f.number); |
1456 |
|
public void testForkHelpQuiesceSingleton() { |
1457 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1458 |
|
protected void realCompute() { |
1459 |
< |
CCF f = new LCCF(null, 8); |
1459 |
> |
CCF f = new LCCF(8); |
1460 |
|
assertSame(f, f.fork()); |
1461 |
|
helpQuiesce(); |
1462 |
|
assertEquals(0, getQueuedTaskCount()); |
1472 |
|
public void testAbnormalInvokeSingleton() { |
1473 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1474 |
|
protected void realCompute() { |
1475 |
< |
FailingCCF f = new LFCCF(null, 8); |
1475 |
> |
FailingCCF f = new LFCCF(8); |
1476 |
|
try { |
1477 |
|
f.invoke(); |
1478 |
|
shouldThrow(); |
1489 |
|
public void testAbnormalQuietlyInvokeSingleton() { |
1490 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1491 |
|
protected void realCompute() { |
1492 |
< |
FailingCCF f = new LFCCF(null, 8); |
1492 |
> |
FailingCCF f = new LFCCF(8); |
1493 |
|
f.quietlyInvoke(); |
1494 |
|
assertTrue(f.getException() instanceof FJException); |
1495 |
|
checkCompletedAbnormally(f, f.getException()); |
1503 |
|
public void testAbnormalForkJoinSingleton() { |
1504 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1505 |
|
protected void realCompute() { |
1506 |
< |
FailingCCF f = new LFCCF(null, 8); |
1506 |
> |
FailingCCF f = new LFCCF(8); |
1507 |
|
assertSame(f, f.fork()); |
1508 |
|
try { |
1509 |
|
f.join(); |
1521 |
|
public void testAbnormalForkGetSingleton() { |
1522 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1523 |
|
protected void realCompute() throws Exception { |
1524 |
< |
FailingCCF f = new LFCCF(null, 8); |
1524 |
> |
FailingCCF f = new LFCCF(8); |
1525 |
|
assertSame(f, f.fork()); |
1526 |
|
try { |
1527 |
|
f.get(); |
1541 |
|
public void testAbnormalForkTimedGetSingleton() { |
1542 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1543 |
|
protected void realCompute() throws Exception { |
1544 |
< |
FailingCCF f = new LFCCF(null, 8); |
1544 |
> |
FailingCCF f = new LFCCF(8); |
1545 |
|
assertSame(f, f.fork()); |
1546 |
|
try { |
1547 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
1561 |
|
public void testAbnormalForkQuietlyJoinSingleton() { |
1562 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1563 |
|
protected void realCompute() { |
1564 |
< |
FailingCCF f = new LFCCF(null, 8); |
1564 |
> |
FailingCCF f = new LFCCF(8); |
1565 |
|
assertSame(f, f.fork()); |
1566 |
|
f.quietlyJoin(); |
1567 |
|
assertTrue(f.getException() instanceof FJException); |
1576 |
|
public void testCancelledInvokeSingleton() { |
1577 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1578 |
|
protected void realCompute() { |
1579 |
< |
CCF f = new LCCF(null, 8); |
1579 |
> |
CCF f = new LCCF(8); |
1580 |
|
assertTrue(f.cancel(true)); |
1581 |
|
try { |
1582 |
|
f.invoke(); |
1594 |
|
public void testCancelledForkJoinSingleton() { |
1595 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1596 |
|
protected void realCompute() { |
1597 |
< |
CCF f = new LCCF(null, 8); |
1597 |
> |
CCF f = new LCCF(8); |
1598 |
|
assertTrue(f.cancel(true)); |
1599 |
|
assertSame(f, f.fork()); |
1600 |
|
try { |
1613 |
|
public void testCancelledForkGetSingleton() { |
1614 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1615 |
|
protected void realCompute() throws Exception { |
1616 |
< |
CCF f = new LCCF(null, 8); |
1616 |
> |
CCF f = new LCCF(8); |
1617 |
|
assertTrue(f.cancel(true)); |
1618 |
|
assertSame(f, f.fork()); |
1619 |
|
try { |
1632 |
|
public void testCancelledForkTimedGetSingleton() throws Exception { |
1633 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1634 |
|
protected void realCompute() throws Exception { |
1635 |
< |
CCF f = new LCCF(null, 8); |
1635 |
> |
CCF f = new LCCF(8); |
1636 |
|
assertTrue(f.cancel(true)); |
1637 |
|
assertSame(f, f.fork()); |
1638 |
|
try { |
1651 |
|
public void testCancelledForkQuietlyJoinSingleton() { |
1652 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1653 |
|
protected void realCompute() { |
1654 |
< |
CCF f = new LCCF(null, 8); |
1654 |
> |
CCF f = new LCCF(8); |
1655 |
|
assertTrue(f.cancel(true)); |
1656 |
|
assertSame(f, f.fork()); |
1657 |
|
f.quietlyJoin(); |
1666 |
|
public void testCompleteExceptionallySingleton() { |
1667 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1668 |
|
protected void realCompute() { |
1669 |
< |
CCF f = new LCCF(null, 8); |
1670 |
< |
f.completeExceptionally(new FJException()); |
1671 |
< |
try { |
1672 |
< |
f.invoke(); |
1673 |
< |
shouldThrow(); |
1674 |
< |
} catch (FJException success) { |
1561 |
< |
checkCompletedAbnormally(f, success); |
1562 |
< |
} |
1669 |
> |
CCF n = new LCCF(8); |
1670 |
> |
CCF f = new LCCF(n, 8); |
1671 |
> |
FJException ex = new FJException(); |
1672 |
> |
f.completeExceptionally(ex); |
1673 |
> |
f.checkCompletedExceptionally(ex); |
1674 |
> |
n.checkCompletedExceptionally(ex); |
1675 |
|
}}; |
1676 |
|
testInvokeOnPool(singletonPool(), a); |
1677 |
|
} |
1682 |
|
public void testInvokeAll2Singleton() { |
1683 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1684 |
|
protected void realCompute() { |
1685 |
< |
CCF f = new LCCF(null, 8); |
1686 |
< |
CCF g = new LCCF(null, 9); |
1685 |
> |
CCF f = new LCCF(8); |
1686 |
> |
CCF g = new LCCF(9); |
1687 |
|
invokeAll(f, g); |
1688 |
|
assertEquals(21, f.number); |
1689 |
|
assertEquals(34, g.number); |
1699 |
|
public void testInvokeAll1Singleton() { |
1700 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1701 |
|
protected void realCompute() { |
1702 |
< |
CCF f = new LCCF(null, 8); |
1702 |
> |
CCF f = new LCCF(8); |
1703 |
|
invokeAll(f); |
1704 |
|
checkCompletedNormally(f); |
1705 |
|
assertEquals(21, f.number); |
1713 |
|
public void testInvokeAll3Singleton() { |
1714 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1715 |
|
protected void realCompute() { |
1716 |
< |
CCF f = new LCCF(null, 8); |
1717 |
< |
CCF g = new LCCF(null, 9); |
1718 |
< |
CCF h = new LCCF(null, 7); |
1716 |
> |
CCF f = new LCCF(8); |
1717 |
> |
CCF g = new LCCF(9); |
1718 |
> |
CCF h = new LCCF(7); |
1719 |
|
invokeAll(f, g, h); |
1720 |
|
assertEquals(21, f.number); |
1721 |
|
assertEquals(34, g.number); |
1733 |
|
public void testInvokeAllCollectionSingleton() { |
1734 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1735 |
|
protected void realCompute() { |
1736 |
< |
CCF f = new LCCF(null, 8); |
1737 |
< |
CCF g = new LCCF(null, 9); |
1738 |
< |
CCF h = new LCCF(null, 7); |
1736 |
> |
CCF f = new LCCF(8); |
1737 |
> |
CCF g = new LCCF(9); |
1738 |
> |
CCF h = new LCCF(7); |
1739 |
|
HashSet set = new HashSet(); |
1740 |
|
set.add(f); |
1741 |
|
set.add(g); |
1757 |
|
public void testInvokeAllNPESingleton() { |
1758 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1759 |
|
protected void realCompute() { |
1760 |
< |
CCF f = new LCCF(null, 8); |
1761 |
< |
CCF g = new LCCF(null, 9); |
1760 |
> |
CCF f = new LCCF(8); |
1761 |
> |
CCF g = new LCCF(9); |
1762 |
|
CCF h = null; |
1763 |
|
try { |
1764 |
|
invokeAll(f, g, h); |
1774 |
|
public void testAbnormalInvokeAll2Singleton() { |
1775 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1776 |
|
protected void realCompute() { |
1777 |
< |
CCF f = new LCCF(null, 8); |
1778 |
< |
FailingCCF g = new LFCCF(null, 9); |
1777 |
> |
CCF f = new LCCF(8); |
1778 |
> |
FailingCCF g = new LFCCF(9); |
1779 |
|
try { |
1780 |
|
invokeAll(f, g); |
1781 |
|
shouldThrow(); |
1792 |
|
public void testAbnormalInvokeAll1Singleton() { |
1793 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1794 |
|
protected void realCompute() { |
1795 |
< |
FailingCCF g = new LFCCF(null, 9); |
1795 |
> |
FailingCCF g = new LFCCF(9); |
1796 |
|
try { |
1797 |
|
invokeAll(g); |
1798 |
|
shouldThrow(); |
1809 |
|
public void testAbnormalInvokeAll3Singleton() { |
1810 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1811 |
|
protected void realCompute() { |
1812 |
< |
CCF f = new LCCF(null, 8); |
1813 |
< |
FailingCCF g = new LFCCF(null, 9); |
1814 |
< |
CCF h = new LCCF(null, 7); |
1812 |
> |
CCF f = new LCCF(8); |
1813 |
> |
FailingCCF g = new LFCCF(9); |
1814 |
> |
CCF h = new LCCF(7); |
1815 |
|
try { |
1816 |
|
invokeAll(f, g, h); |
1817 |
|
shouldThrow(); |
1823 |
|
} |
1824 |
|
|
1825 |
|
/** |
1826 |
< |
* invokeAll(collection) throws exception if any task does |
1826 |
> |
* invokeAll(collection) throws exception if any task does |
1827 |
|
*/ |
1828 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1829 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1830 |
|
protected void realCompute() { |
1831 |
< |
FailingCCF f = new LFCCF(null, 8); |
1832 |
< |
CCF g = new LCCF(null, 9); |
1833 |
< |
CCF h = new LCCF(null, 7); |
1831 |
> |
FailingCCF f = new LFCCF(8); |
1832 |
> |
CCF g = new LCCF(9); |
1833 |
> |
CCF h = new LCCF(7); |
1834 |
|
HashSet set = new HashSet(); |
1835 |
|
set.add(f); |
1836 |
|
set.add(g); |
1845 |
|
testInvokeOnPool(singletonPool(), a); |
1846 |
|
} |
1847 |
|
|
1848 |
+ |
/** CountedCompleter class javadoc code sample, version 1. */ |
1849 |
+ |
public static <E> void forEach1(E[] array, Consumer<E> action) { |
1850 |
+ |
class Task extends CountedCompleter<Void> { |
1851 |
+ |
final int lo, hi; |
1852 |
+ |
Task(Task parent, int lo, int hi) { |
1853 |
+ |
super(parent); this.lo = lo; this.hi = hi; |
1854 |
+ |
} |
1855 |
+ |
|
1856 |
+ |
public void compute() { |
1857 |
+ |
if (hi - lo >= 2) { |
1858 |
+ |
int mid = (lo + hi) >>> 1; |
1859 |
+ |
// must set pending count before fork |
1860 |
+ |
setPendingCount(2); |
1861 |
+ |
new Task(this, mid, hi).fork(); // right child |
1862 |
+ |
new Task(this, lo, mid).fork(); // left child |
1863 |
+ |
} |
1864 |
+ |
else if (hi > lo) |
1865 |
+ |
action.accept(array[lo]); |
1866 |
+ |
tryComplete(); |
1867 |
+ |
} |
1868 |
+ |
} |
1869 |
+ |
new Task(null, 0, array.length).invoke(); |
1870 |
+ |
} |
1871 |
+ |
|
1872 |
+ |
/** CountedCompleter class javadoc code sample, version 2. */ |
1873 |
+ |
public static <E> void forEach2(E[] array, Consumer<E> action) { |
1874 |
+ |
class Task extends CountedCompleter<Void> { |
1875 |
+ |
final int lo, hi; |
1876 |
+ |
Task(Task parent, int lo, int hi) { |
1877 |
+ |
super(parent); this.lo = lo; this.hi = hi; |
1878 |
+ |
} |
1879 |
+ |
|
1880 |
+ |
public void compute() { |
1881 |
+ |
if (hi - lo >= 2) { |
1882 |
+ |
int mid = (lo + hi) >>> 1; |
1883 |
+ |
setPendingCount(1); // looks off by one, but correct! |
1884 |
+ |
new Task(this, mid, hi).fork(); // right child |
1885 |
+ |
new Task(this, lo, mid).compute(); // direct invoke |
1886 |
+ |
} else { |
1887 |
+ |
if (hi > lo) |
1888 |
+ |
action.accept(array[lo]); |
1889 |
+ |
tryComplete(); |
1890 |
+ |
} |
1891 |
+ |
} |
1892 |
+ |
} |
1893 |
+ |
new Task(null, 0, array.length).invoke(); |
1894 |
+ |
} |
1895 |
+ |
|
1896 |
+ |
/** CountedCompleter class javadoc code sample, version 3. */ |
1897 |
+ |
public static <E> void forEach3(E[] array, Consumer<E> action) { |
1898 |
+ |
class Task extends CountedCompleter<Void> { |
1899 |
+ |
final int lo, hi; |
1900 |
+ |
Task(Task parent, int lo, int hi) { |
1901 |
+ |
super(parent); this.lo = lo; this.hi = hi; |
1902 |
+ |
} |
1903 |
+ |
|
1904 |
+ |
public void compute() { |
1905 |
+ |
int n = hi - lo; |
1906 |
+ |
for (; n >= 2; n /= 2) { |
1907 |
+ |
addToPendingCount(1); |
1908 |
+ |
new Task(this, lo + n/2, lo + n).fork(); |
1909 |
+ |
} |
1910 |
+ |
if (n > 0) |
1911 |
+ |
action.accept(array[lo]); |
1912 |
+ |
propagateCompletion(); |
1913 |
+ |
} |
1914 |
+ |
} |
1915 |
+ |
new Task(null, 0, array.length).invoke(); |
1916 |
+ |
} |
1917 |
+ |
|
1918 |
+ |
/** CountedCompleter class javadoc code sample, version 4. */ |
1919 |
+ |
public static <E> void forEach4(E[] array, Consumer<E> action) { |
1920 |
+ |
class Task extends CountedCompleter<Void> { |
1921 |
+ |
final int lo, hi; |
1922 |
+ |
Task(Task parent, int lo, int hi) { |
1923 |
+ |
super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo)); |
1924 |
+ |
this.lo = lo; this.hi = hi; |
1925 |
+ |
} |
1926 |
+ |
|
1927 |
+ |
public void compute() { |
1928 |
+ |
for (int n = hi - lo; n >= 2; n /= 2) |
1929 |
+ |
new Task(this, lo + n/2, lo + n).fork(); |
1930 |
+ |
action.accept(array[lo]); |
1931 |
+ |
propagateCompletion(); |
1932 |
+ |
} |
1933 |
+ |
} |
1934 |
+ |
if (array.length > 0) |
1935 |
+ |
new Task(null, 0, array.length).invoke(); |
1936 |
+ |
} |
1937 |
+ |
|
1938 |
+ |
void testRecursiveDecomposition( |
1939 |
+ |
BiConsumer<Integer[], Consumer<Integer>> action) { |
1940 |
+ |
int n = ThreadLocalRandom.current().nextInt(8); |
1941 |
+ |
Integer[] a = new Integer[n]; |
1942 |
+ |
for (int i = 0; i < n; i++) a[i] = i + 1; |
1943 |
+ |
AtomicInteger ai = new AtomicInteger(0); |
1944 |
+ |
action.accept(a, (x) -> ai.addAndGet(x)); |
1945 |
+ |
assertEquals(n * (n + 1) / 2, ai.get()); |
1946 |
+ |
} |
1947 |
+ |
|
1948 |
+ |
/** |
1949 |
+ |
* Variants of divide-by-two recursive decomposition into leaf tasks, |
1950 |
+ |
* as described in the CountedCompleter class javadoc code samples |
1951 |
+ |
*/ |
1952 |
+ |
public void testRecursiveDecomposition() { |
1953 |
+ |
testRecursiveDecomposition(CountedCompleterTest::forEach1); |
1954 |
+ |
testRecursiveDecomposition(CountedCompleterTest::forEach2); |
1955 |
+ |
testRecursiveDecomposition(CountedCompleterTest::forEach3); |
1956 |
+ |
testRecursiveDecomposition(CountedCompleterTest::forEach4); |
1957 |
+ |
} |
1958 |
+ |
|
1959 |
|
} |