4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
> |
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
11 |
+ |
import java.util.concurrent.CountedCompleter; |
12 |
|
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
import java.util.concurrent.ForkJoinTask; |
12 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
15 |
|
import java.util.concurrent.RecursiveAction; |
14 |
– |
import java.util.concurrent.CountedCompleter; |
15 |
– |
import java.util.concurrent.ThreadLocalRandom; |
16 |
– |
import java.util.concurrent.TimeUnit; |
16 |
|
import java.util.concurrent.TimeoutException; |
17 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
18 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
19 |
< |
import java.util.Arrays; |
21 |
< |
import java.util.HashSet; |
17 |
> |
|
18 |
> |
import junit.framework.Test; |
19 |
> |
import junit.framework.TestSuite; |
20 |
|
|
21 |
|
public class ForkJoinPool8Test extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
|
26 |
|
public static Test suite() { |
83 |
|
|
84 |
|
Thread.currentThread().interrupt(); |
85 |
|
try { |
86 |
< |
a.get(5L, SECONDS); |
86 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
87 |
|
shouldThrow(); |
88 |
|
} catch (InterruptedException success) { |
89 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
90 |
|
} |
91 |
|
|
92 |
|
try { |
93 |
< |
a.get(0L, SECONDS); |
93 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
94 |
|
shouldThrow(); |
95 |
|
} catch (TimeoutException success) { |
96 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
108 |
|
assertFalse(a.cancel(true)); |
109 |
|
try { |
110 |
|
assertNull(a.get()); |
111 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
114 |
< |
try { |
115 |
< |
assertNull(a.get(5L, SECONDS)); |
111 |
> |
assertNull(a.get(randomTimeout(), randomTimeUnit())); |
112 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
113 |
|
} |
114 |
|
|
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
|
|
135 |
|
try { |
136 |
< |
a.get(5L, SECONDS); |
136 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
137 |
|
shouldThrow(); |
138 |
|
} catch (CancellationException success) { |
139 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
164 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
165 |
|
|
166 |
|
try { |
167 |
< |
a.get(5L, SECONDS); |
167 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
168 |
|
shouldThrow(); |
169 |
|
} catch (ExecutionException success) { |
170 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
176 |
|
public FJException(Throwable cause) { super(cause); } |
177 |
|
} |
178 |
|
|
179 |
< |
// A simple recursive action for testing |
179 |
> |
/** A simple recursive action for testing. */ |
180 |
|
final class FibAction extends CheckedRecursiveAction { |
181 |
|
final int number; |
182 |
|
int result; |
183 |
|
FibAction(int n) { number = n; } |
184 |
< |
public void realCompute() { |
184 |
> |
protected void realCompute() { |
185 |
|
int n = number; |
186 |
|
if (n <= 1) |
187 |
|
result = n; |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
< |
// A recursive action failing in base case |
197 |
> |
/** A recursive action failing in base case. */ |
198 |
|
static final class FailingFibAction extends RecursiveAction { |
199 |
|
final int number; |
200 |
|
int result; |
219 |
|
*/ |
220 |
|
public void testInvoke() { |
221 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
222 |
< |
public void realCompute() { |
222 |
> |
protected void realCompute() { |
223 |
|
FibAction f = new FibAction(8); |
224 |
|
assertNull(f.invoke()); |
225 |
|
assertEquals(21, f.result); |
235 |
|
*/ |
236 |
|
public void testQuietlyInvoke() { |
237 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
238 |
< |
public void realCompute() { |
238 |
> |
protected void realCompute() { |
239 |
|
FibAction f = new FibAction(8); |
240 |
|
f.quietlyInvoke(); |
241 |
|
assertEquals(21, f.result); |
249 |
|
*/ |
250 |
|
public void testForkJoin() { |
251 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
252 |
< |
public void realCompute() { |
252 |
> |
protected void realCompute() { |
253 |
|
FibAction f = new FibAction(8); |
254 |
|
assertSame(f, f.fork()); |
255 |
|
assertNull(f.join()); |
264 |
|
*/ |
265 |
|
public void testJoinIgnoresInterrupts() { |
266 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
267 |
< |
public void realCompute() { |
267 |
> |
protected void realCompute() { |
268 |
|
FibAction f = new FibAction(8); |
269 |
< |
final Thread myself = Thread.currentThread(); |
269 |
> |
final Thread currentThread = Thread.currentThread(); |
270 |
|
|
271 |
|
// test join() |
272 |
|
assertSame(f, f.fork()); |
273 |
< |
myself.interrupt(); |
278 |
< |
assertTrue(myself.isInterrupted()); |
273 |
> |
currentThread.interrupt(); |
274 |
|
assertNull(f.join()); |
275 |
|
Thread.interrupted(); |
276 |
|
assertEquals(21, f.result); |
279 |
|
f = new FibAction(8); |
280 |
|
f.cancel(true); |
281 |
|
assertSame(f, f.fork()); |
282 |
< |
myself.interrupt(); |
288 |
< |
assertTrue(myself.isInterrupted()); |
282 |
> |
currentThread.interrupt(); |
283 |
|
try { |
284 |
|
f.join(); |
285 |
|
shouldThrow(); |
291 |
|
f = new FibAction(8); |
292 |
|
f.completeExceptionally(new FJException()); |
293 |
|
assertSame(f, f.fork()); |
294 |
< |
myself.interrupt(); |
301 |
< |
assertTrue(myself.isInterrupted()); |
294 |
> |
currentThread.interrupt(); |
295 |
|
try { |
296 |
|
f.join(); |
297 |
|
shouldThrow(); |
303 |
|
// test quietlyJoin() |
304 |
|
f = new FibAction(8); |
305 |
|
assertSame(f, f.fork()); |
306 |
< |
myself.interrupt(); |
314 |
< |
assertTrue(myself.isInterrupted()); |
306 |
> |
currentThread.interrupt(); |
307 |
|
f.quietlyJoin(); |
308 |
|
Thread.interrupted(); |
309 |
|
assertEquals(21, f.result); |
312 |
|
f = new FibAction(8); |
313 |
|
f.cancel(true); |
314 |
|
assertSame(f, f.fork()); |
315 |
< |
myself.interrupt(); |
324 |
< |
assertTrue(myself.isInterrupted()); |
315 |
> |
currentThread.interrupt(); |
316 |
|
f.quietlyJoin(); |
317 |
|
Thread.interrupted(); |
318 |
|
checkCancelled(f); |
320 |
|
f = new FibAction(8); |
321 |
|
f.completeExceptionally(new FJException()); |
322 |
|
assertSame(f, f.fork()); |
323 |
< |
myself.interrupt(); |
333 |
< |
assertTrue(myself.isInterrupted()); |
323 |
> |
currentThread.interrupt(); |
324 |
|
f.quietlyJoin(); |
325 |
|
Thread.interrupted(); |
326 |
|
checkCompletedAbnormally(f, f.getException()); |
330 |
|
checkInvoke(a); |
331 |
|
} |
332 |
|
|
343 |
– |
|
333 |
|
/** |
334 |
|
* get of a forked task returns when task completes |
335 |
|
*/ |
336 |
|
public void testForkGet() { |
337 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
338 |
< |
public void realCompute() throws Exception { |
338 |
> |
protected void realCompute() throws Exception { |
339 |
|
FibAction f = new FibAction(8); |
340 |
|
assertSame(f, f.fork()); |
341 |
|
assertNull(f.get()); |
350 |
|
*/ |
351 |
|
public void testForkTimedGet() { |
352 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
353 |
< |
public void realCompute() throws Exception { |
353 |
> |
protected void realCompute() throws Exception { |
354 |
|
FibAction f = new FibAction(8); |
355 |
|
assertSame(f, f.fork()); |
356 |
< |
assertNull(f.get(5L, SECONDS)); |
356 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
357 |
|
assertEquals(21, f.result); |
358 |
|
checkCompletedNormally(f); |
359 |
|
}}; |
365 |
|
*/ |
366 |
|
public void testForkTimedGetNPE() { |
367 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
368 |
< |
public void realCompute() throws Exception { |
368 |
> |
protected void realCompute() throws Exception { |
369 |
|
FibAction f = new FibAction(8); |
370 |
|
assertSame(f, f.fork()); |
371 |
|
try { |
372 |
< |
f.get(5L, null); |
372 |
> |
f.get(randomTimeout(), null); |
373 |
|
shouldThrow(); |
374 |
|
} catch (NullPointerException success) {} |
375 |
|
}}; |
381 |
|
*/ |
382 |
|
public void testForkQuietlyJoin() { |
383 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
384 |
< |
public void realCompute() { |
384 |
> |
protected void realCompute() { |
385 |
|
FibAction f = new FibAction(8); |
386 |
|
assertSame(f, f.fork()); |
387 |
|
f.quietlyJoin(); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
406 |
– |
* helpQuiesce returns when tasks are complete. |
407 |
– |
* getQueuedTaskCount returns 0 when quiescent |
408 |
– |
*/ |
409 |
– |
public void testForkHelpQuiesce() { |
410 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
411 |
– |
public void realCompute() { |
412 |
– |
FibAction f = new FibAction(8); |
413 |
– |
assertSame(f, f.fork()); |
414 |
– |
helpQuiesce(); |
415 |
– |
assertEquals(21, f.result); |
416 |
– |
assertEquals(0, getQueuedTaskCount()); |
417 |
– |
checkCompletedNormally(f); |
418 |
– |
}}; |
419 |
– |
checkInvoke(a); |
420 |
– |
} |
421 |
– |
|
422 |
– |
/** |
395 |
|
* invoke task throws exception when task completes abnormally |
396 |
|
*/ |
397 |
|
public void testAbnormalInvoke() { |
398 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
399 |
< |
public void realCompute() { |
399 |
> |
protected void realCompute() { |
400 |
|
FailingFibAction f = new FailingFibAction(8); |
401 |
|
try { |
402 |
|
f.invoke(); |
413 |
|
*/ |
414 |
|
public void testAbnormalQuietlyInvoke() { |
415 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
416 |
< |
public void realCompute() { |
416 |
> |
protected void realCompute() { |
417 |
|
FailingFibAction f = new FailingFibAction(8); |
418 |
|
f.quietlyInvoke(); |
419 |
|
assertTrue(f.getException() instanceof FJException); |
427 |
|
*/ |
428 |
|
public void testAbnormalForkJoin() { |
429 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
430 |
< |
public void realCompute() { |
430 |
> |
protected void realCompute() { |
431 |
|
FailingFibAction f = new FailingFibAction(8); |
432 |
|
assertSame(f, f.fork()); |
433 |
|
try { |
445 |
|
*/ |
446 |
|
public void testAbnormalForkGet() { |
447 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
448 |
< |
public void realCompute() throws Exception { |
448 |
> |
protected void realCompute() throws Exception { |
449 |
|
FailingFibAction f = new FailingFibAction(8); |
450 |
|
assertSame(f, f.fork()); |
451 |
|
try { |
465 |
|
*/ |
466 |
|
public void testAbnormalForkTimedGet() { |
467 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
468 |
< |
public void realCompute() throws Exception { |
468 |
> |
protected void realCompute() throws Exception { |
469 |
|
FailingFibAction f = new FailingFibAction(8); |
470 |
|
assertSame(f, f.fork()); |
471 |
|
try { |
472 |
< |
f.get(5L, TimeUnit.SECONDS); |
472 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
473 |
|
shouldThrow(); |
474 |
|
} catch (ExecutionException success) { |
475 |
|
Throwable cause = success.getCause(); |
485 |
|
*/ |
486 |
|
public void testAbnormalForkQuietlyJoin() { |
487 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
488 |
< |
public void realCompute() { |
488 |
> |
protected void realCompute() { |
489 |
|
FailingFibAction f = new FailingFibAction(8); |
490 |
|
assertSame(f, f.fork()); |
491 |
|
f.quietlyJoin(); |
500 |
|
*/ |
501 |
|
public void testCancelledInvoke() { |
502 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
503 |
< |
public void realCompute() { |
503 |
> |
protected void realCompute() { |
504 |
|
FibAction f = new FibAction(8); |
505 |
|
assertTrue(f.cancel(true)); |
506 |
|
try { |
518 |
|
*/ |
519 |
|
public void testCancelledForkJoin() { |
520 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
521 |
< |
public void realCompute() { |
521 |
> |
protected void realCompute() { |
522 |
|
FibAction f = new FibAction(8); |
523 |
|
assertTrue(f.cancel(true)); |
524 |
|
assertSame(f, f.fork()); |
537 |
|
*/ |
538 |
|
public void testCancelledForkGet() { |
539 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
540 |
< |
public void realCompute() throws Exception { |
540 |
> |
protected void realCompute() throws Exception { |
541 |
|
FibAction f = new FibAction(8); |
542 |
|
assertTrue(f.cancel(true)); |
543 |
|
assertSame(f, f.fork()); |
556 |
|
*/ |
557 |
|
public void testCancelledForkTimedGet() { |
558 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
559 |
< |
public void realCompute() throws Exception { |
559 |
> |
protected void realCompute() throws Exception { |
560 |
|
FibAction f = new FibAction(8); |
561 |
|
assertTrue(f.cancel(true)); |
562 |
|
assertSame(f, f.fork()); |
563 |
|
try { |
564 |
< |
f.get(5L, SECONDS); |
564 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
565 |
|
shouldThrow(); |
566 |
|
} catch (CancellationException success) { |
567 |
|
checkCancelled(f); |
575 |
|
*/ |
576 |
|
public void testCancelledForkQuietlyJoin() { |
577 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
578 |
< |
public void realCompute() { |
578 |
> |
protected void realCompute() { |
579 |
|
FibAction f = new FibAction(8); |
580 |
|
assertTrue(f.cancel(true)); |
581 |
|
assertSame(f, f.fork()); |
590 |
|
*/ |
591 |
|
public void testInForkJoinPool2() { |
592 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
593 |
< |
public void realCompute() { |
593 |
> |
protected void realCompute() { |
594 |
|
assertFalse(inForkJoinPool()); |
595 |
|
}}; |
596 |
|
assertNull(a.invoke()); |
601 |
|
*/ |
602 |
|
public void testReinitialize() { |
603 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
604 |
< |
public void realCompute() { |
604 |
> |
protected void realCompute() { |
605 |
|
FibAction f = new FibAction(8); |
606 |
|
checkNotDone(f); |
607 |
|
|
621 |
|
*/ |
622 |
|
public void testReinitializeAbnormal() { |
623 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
624 |
< |
public void realCompute() { |
624 |
> |
protected void realCompute() { |
625 |
|
FailingFibAction f = new FailingFibAction(8); |
626 |
|
checkNotDone(f); |
627 |
|
|
644 |
|
*/ |
645 |
|
public void testCompleteExceptionally() { |
646 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
647 |
< |
public void realCompute() { |
647 |
> |
protected void realCompute() { |
648 |
|
FibAction f = new FibAction(8); |
649 |
|
f.completeExceptionally(new FJException()); |
650 |
|
try { |
662 |
|
*/ |
663 |
|
public void testComplete() { |
664 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
665 |
< |
public void realCompute() { |
665 |
> |
protected void realCompute() { |
666 |
|
FibAction f = new FibAction(8); |
667 |
|
f.complete(null); |
668 |
|
assertNull(f.invoke()); |
677 |
|
*/ |
678 |
|
public void testInvokeAll2() { |
679 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
680 |
< |
public void realCompute() { |
680 |
> |
protected void realCompute() { |
681 |
|
FibAction f = new FibAction(8); |
682 |
|
FibAction g = new FibAction(9); |
683 |
|
invokeAll(f, g); |
694 |
|
*/ |
695 |
|
public void testInvokeAll1() { |
696 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
697 |
< |
public void realCompute() { |
697 |
> |
protected void realCompute() { |
698 |
|
FibAction f = new FibAction(8); |
699 |
|
invokeAll(f); |
700 |
|
checkCompletedNormally(f); |
708 |
|
*/ |
709 |
|
public void testInvokeAll3() { |
710 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
711 |
< |
public void realCompute() { |
711 |
> |
protected void realCompute() { |
712 |
|
FibAction f = new FibAction(8); |
713 |
|
FibAction g = new FibAction(9); |
714 |
|
FibAction h = new FibAction(7); |
731 |
|
*/ |
732 |
|
public void testInvokeAllCollection() { |
733 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
734 |
< |
public void realCompute() { |
734 |
> |
protected void realCompute() { |
735 |
|
FibAction f = new FibAction(8); |
736 |
|
FibAction g = new FibAction(9); |
737 |
|
FibAction h = new FibAction(7); |
758 |
|
*/ |
759 |
|
public void testInvokeAllNPE() { |
760 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
761 |
< |
public void realCompute() { |
761 |
> |
protected void realCompute() { |
762 |
|
FibAction f = new FibAction(8); |
763 |
|
FibAction g = new FibAction(9); |
764 |
|
FibAction h = null; |
775 |
|
*/ |
776 |
|
public void testAbnormalInvokeAll2() { |
777 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
778 |
< |
public void realCompute() { |
778 |
> |
protected void realCompute() { |
779 |
|
FibAction f = new FibAction(8); |
780 |
|
FailingFibAction g = new FailingFibAction(9); |
781 |
|
try { |
793 |
|
*/ |
794 |
|
public void testAbnormalInvokeAll1() { |
795 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
796 |
< |
public void realCompute() { |
796 |
> |
protected void realCompute() { |
797 |
|
FailingFibAction g = new FailingFibAction(9); |
798 |
|
try { |
799 |
|
invokeAll(g); |
810 |
|
*/ |
811 |
|
public void testAbnormalInvokeAll3() { |
812 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
813 |
< |
public void realCompute() { |
813 |
> |
protected void realCompute() { |
814 |
|
FibAction f = new FibAction(8); |
815 |
|
FailingFibAction g = new FailingFibAction(9); |
816 |
|
FibAction h = new FibAction(7); |
829 |
|
*/ |
830 |
|
public void testAbnormalInvokeAllCollection() { |
831 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
832 |
< |
public void realCompute() { |
832 |
> |
protected void realCompute() { |
833 |
|
FailingFibAction f = new FailingFibAction(8); |
834 |
|
FibAction g = new FibAction(9); |
835 |
|
FibAction h = new FibAction(7); |
849 |
|
|
850 |
|
// CountedCompleter versions |
851 |
|
|
880 |
– |
public abstract class CheckedFJTask extends RecursiveAction { |
881 |
– |
protected abstract void realCompute() throws Throwable; |
882 |
– |
|
883 |
– |
public final void compute() { |
884 |
– |
try { |
885 |
– |
realCompute(); |
886 |
– |
} catch (Throwable t) { |
887 |
– |
threadUnexpectedException(t); |
888 |
– |
} |
889 |
– |
} |
890 |
– |
} |
891 |
– |
|
852 |
|
abstract static class CCF extends CountedCompleter { |
853 |
|
int number; |
854 |
|
int rnumber; |
902 |
|
} |
903 |
|
} |
904 |
|
|
905 |
< |
// Version of CCF with forced failure in left completions |
905 |
> |
/** Version of CCF with forced failure in left completions. */ |
906 |
|
abstract static class FailingCCF extends CountedCompleter { |
907 |
|
int number; |
908 |
|
int rnumber; |
957 |
|
* completed tasks; getRawResult returns null. |
958 |
|
*/ |
959 |
|
public void testInvokeCC() { |
960 |
< |
ForkJoinTask a = new CheckedFJTask() { |
961 |
< |
public void realCompute() { |
960 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
961 |
> |
protected void realCompute() { |
962 |
|
CCF f = new LCCF(null, 8); |
963 |
|
assertNull(f.invoke()); |
964 |
|
assertEquals(21, f.number); |
973 |
|
* completed tasks |
974 |
|
*/ |
975 |
|
public void testQuietlyInvokeCC() { |
976 |
< |
ForkJoinTask a = new CheckedFJTask() { |
977 |
< |
public void realCompute() { |
976 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
977 |
> |
protected void realCompute() { |
978 |
|
CCF f = new LCCF(null, 8); |
979 |
|
f.quietlyInvoke(); |
980 |
|
assertEquals(21, f.number); |
987 |
|
* join of a forked task returns when task completes |
988 |
|
*/ |
989 |
|
public void testForkJoinCC() { |
990 |
< |
ForkJoinTask a = new CheckedFJTask() { |
991 |
< |
public void realCompute() { |
990 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
991 |
> |
protected void realCompute() { |
992 |
|
CCF f = new LCCF(null, 8); |
993 |
|
assertSame(f, f.fork()); |
994 |
|
assertNull(f.join()); |
1002 |
|
* get of a forked task returns when task completes |
1003 |
|
*/ |
1004 |
|
public void testForkGetCC() { |
1005 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1006 |
< |
public void realCompute() throws Exception { |
1005 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1006 |
> |
protected void realCompute() throws Exception { |
1007 |
|
CCF f = new LCCF(null, 8); |
1008 |
|
assertSame(f, f.fork()); |
1009 |
|
assertNull(f.get()); |
1017 |
|
* timed get of a forked task returns when task completes |
1018 |
|
*/ |
1019 |
|
public void testForkTimedGetCC() { |
1020 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1021 |
< |
public void realCompute() throws Exception { |
1020 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1021 |
> |
protected void realCompute() throws Exception { |
1022 |
|
CCF f = new LCCF(null, 8); |
1023 |
|
assertSame(f, f.fork()); |
1024 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1032 |
|
* timed get with null time unit throws NPE |
1033 |
|
*/ |
1034 |
|
public void testForkTimedGetNPECC() { |
1035 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1036 |
< |
public void realCompute() throws Exception { |
1035 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1036 |
> |
protected void realCompute() throws Exception { |
1037 |
|
CCF f = new LCCF(null, 8); |
1038 |
|
assertSame(f, f.fork()); |
1039 |
|
try { |
1040 |
< |
f.get(5L, null); |
1040 |
> |
f.get(randomTimeout(), null); |
1041 |
|
shouldThrow(); |
1042 |
|
} catch (NullPointerException success) {} |
1043 |
|
}}; |
1048 |
|
* quietlyJoin of a forked task returns when task completes |
1049 |
|
*/ |
1050 |
|
public void testForkQuietlyJoinCC() { |
1051 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1052 |
< |
public void realCompute() { |
1051 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1052 |
> |
protected void realCompute() { |
1053 |
|
CCF f = new LCCF(null, 8); |
1054 |
|
assertSame(f, f.fork()); |
1055 |
|
f.quietlyJoin(); |
1060 |
|
} |
1061 |
|
|
1062 |
|
/** |
1103 |
– |
* helpQuiesce returns when tasks are complete. |
1104 |
– |
* getQueuedTaskCount returns 0 when quiescent |
1105 |
– |
*/ |
1106 |
– |
public void testForkHelpQuiesceCC() { |
1107 |
– |
ForkJoinTask a = new CheckedFJTask() { |
1108 |
– |
public void realCompute() { |
1109 |
– |
CCF f = new LCCF(null, 8); |
1110 |
– |
assertSame(f, f.fork()); |
1111 |
– |
helpQuiesce(); |
1112 |
– |
assertEquals(21, f.number); |
1113 |
– |
assertEquals(0, getQueuedTaskCount()); |
1114 |
– |
checkCompletedNormally(f); |
1115 |
– |
}}; |
1116 |
– |
checkInvoke(a); |
1117 |
– |
} |
1118 |
– |
|
1119 |
– |
/** |
1063 |
|
* invoke task throws exception when task completes abnormally |
1064 |
|
*/ |
1065 |
|
public void testAbnormalInvokeCC() { |
1066 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1067 |
< |
public void realCompute() { |
1066 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1067 |
> |
protected void realCompute() { |
1068 |
|
FailingCCF f = new LFCCF(null, 8); |
1069 |
|
try { |
1070 |
|
f.invoke(); |
1080 |
|
* quietlyInvoke task returns when task completes abnormally |
1081 |
|
*/ |
1082 |
|
public void testAbnormalQuietlyInvokeCC() { |
1083 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1084 |
< |
public void realCompute() { |
1083 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1084 |
> |
protected void realCompute() { |
1085 |
|
FailingCCF f = new LFCCF(null, 8); |
1086 |
|
f.quietlyInvoke(); |
1087 |
|
assertTrue(f.getException() instanceof FJException); |
1094 |
|
* join of a forked task throws exception when task completes abnormally |
1095 |
|
*/ |
1096 |
|
public void testAbnormalForkJoinCC() { |
1097 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1098 |
< |
public void realCompute() { |
1097 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1098 |
> |
protected void realCompute() { |
1099 |
|
FailingCCF f = new LFCCF(null, 8); |
1100 |
|
assertSame(f, f.fork()); |
1101 |
|
try { |
1112 |
|
* get of a forked task throws exception when task completes abnormally |
1113 |
|
*/ |
1114 |
|
public void testAbnormalForkGetCC() { |
1115 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1116 |
< |
public void realCompute() throws Exception { |
1115 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1116 |
> |
protected void realCompute() throws Exception { |
1117 |
|
FailingCCF f = new LFCCF(null, 8); |
1118 |
|
assertSame(f, f.fork()); |
1119 |
|
try { |
1132 |
|
* timed get of a forked task throws exception when task completes abnormally |
1133 |
|
*/ |
1134 |
|
public void testAbnormalForkTimedGetCC() { |
1135 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1136 |
< |
public void realCompute() throws Exception { |
1135 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1136 |
> |
protected void realCompute() throws Exception { |
1137 |
|
FailingCCF f = new LFCCF(null, 8); |
1138 |
|
assertSame(f, f.fork()); |
1139 |
|
try { |
1152 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
1153 |
|
*/ |
1154 |
|
public void testAbnormalForkQuietlyJoinCC() { |
1155 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1156 |
< |
public void realCompute() { |
1155 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1156 |
> |
protected void realCompute() { |
1157 |
|
FailingCCF f = new LFCCF(null, 8); |
1158 |
|
assertSame(f, f.fork()); |
1159 |
|
f.quietlyJoin(); |
1167 |
|
* invoke task throws exception when task cancelled |
1168 |
|
*/ |
1169 |
|
public void testCancelledInvokeCC() { |
1170 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1171 |
< |
public void realCompute() { |
1170 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1171 |
> |
protected void realCompute() { |
1172 |
|
CCF f = new LCCF(null, 8); |
1173 |
|
assertTrue(f.cancel(true)); |
1174 |
|
try { |
1185 |
|
* join of a forked task throws exception when task cancelled |
1186 |
|
*/ |
1187 |
|
public void testCancelledForkJoinCC() { |
1188 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1189 |
< |
public void realCompute() { |
1188 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1189 |
> |
protected void realCompute() { |
1190 |
|
CCF f = new LCCF(null, 8); |
1191 |
|
assertTrue(f.cancel(true)); |
1192 |
|
assertSame(f, f.fork()); |
1204 |
|
* get of a forked task throws exception when task cancelled |
1205 |
|
*/ |
1206 |
|
public void testCancelledForkGetCC() { |
1207 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1208 |
< |
public void realCompute() throws Exception { |
1207 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1208 |
> |
protected void realCompute() throws Exception { |
1209 |
|
CCF f = new LCCF(null, 8); |
1210 |
|
assertTrue(f.cancel(true)); |
1211 |
|
assertSame(f, f.fork()); |
1223 |
|
* timed get of a forked task throws exception when task cancelled |
1224 |
|
*/ |
1225 |
|
public void testCancelledForkTimedGetCC() throws Exception { |
1226 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1227 |
< |
public void realCompute() throws Exception { |
1226 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1227 |
> |
protected void realCompute() throws Exception { |
1228 |
|
CCF f = new LCCF(null, 8); |
1229 |
|
assertTrue(f.cancel(true)); |
1230 |
|
assertSame(f, f.fork()); |
1242 |
|
* quietlyJoin of a forked task returns when task cancelled |
1243 |
|
*/ |
1244 |
|
public void testCancelledForkQuietlyJoinCC() { |
1245 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1246 |
< |
public void realCompute() { |
1245 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1246 |
> |
protected void realCompute() { |
1247 |
|
CCF f = new LCCF(null, 8); |
1248 |
|
assertTrue(f.cancel(true)); |
1249 |
|
assertSame(f, f.fork()); |
1257 |
|
* getPool of non-FJ task returns null |
1258 |
|
*/ |
1259 |
|
public void testGetPool2CC() { |
1260 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1261 |
< |
public void realCompute() { |
1260 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1261 |
> |
protected void realCompute() { |
1262 |
|
assertNull(getPool()); |
1263 |
|
}}; |
1264 |
|
assertNull(a.invoke()); |
1268 |
|
* inForkJoinPool of non-FJ task returns false |
1269 |
|
*/ |
1270 |
|
public void testInForkJoinPool2CC() { |
1271 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1272 |
< |
public void realCompute() { |
1271 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1272 |
> |
protected void realCompute() { |
1273 |
|
assertFalse(inForkJoinPool()); |
1274 |
|
}}; |
1275 |
|
assertNull(a.invoke()); |
1279 |
|
* setRawResult(null) succeeds |
1280 |
|
*/ |
1281 |
|
public void testSetRawResultCC() { |
1282 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1283 |
< |
public void realCompute() { |
1282 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1283 |
> |
protected void realCompute() { |
1284 |
|
setRawResult(null); |
1285 |
|
assertNull(getRawResult()); |
1286 |
|
}}; |
1291 |
|
* invoke task throws exception after invoking completeExceptionally |
1292 |
|
*/ |
1293 |
|
public void testCompleteExceptionally2CC() { |
1294 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1295 |
< |
public void realCompute() { |
1294 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1295 |
> |
protected void realCompute() { |
1296 |
|
CCF f = new LCCF(null, 8); |
1297 |
|
f.completeExceptionally(new FJException()); |
1298 |
|
try { |
1309 |
|
* invokeAll(t1, t2) invokes all task arguments |
1310 |
|
*/ |
1311 |
|
public void testInvokeAll2CC() { |
1312 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1313 |
< |
public void realCompute() { |
1312 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1313 |
> |
protected void realCompute() { |
1314 |
|
CCF f = new LCCF(null, 8); |
1315 |
|
CCF g = new LCCF(null, 9); |
1316 |
|
invokeAll(f, g); |
1326 |
|
* invokeAll(tasks) with 1 argument invokes task |
1327 |
|
*/ |
1328 |
|
public void testInvokeAll1CC() { |
1329 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1330 |
< |
public void realCompute() { |
1329 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1330 |
> |
protected void realCompute() { |
1331 |
|
CCF f = new LCCF(null, 8); |
1332 |
|
invokeAll(f); |
1333 |
|
checkCompletedNormally(f); |
1340 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1341 |
|
*/ |
1342 |
|
public void testInvokeAll3CC() { |
1343 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1344 |
< |
public void realCompute() { |
1343 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1344 |
> |
protected void realCompute() { |
1345 |
|
CCF f = new LCCF(null, 8); |
1346 |
|
CCF g = new LCCF(null, 9); |
1347 |
|
CCF h = new LCCF(null, 7); |
1360 |
|
* invokeAll(collection) invokes all tasks in the collection |
1361 |
|
*/ |
1362 |
|
public void testInvokeAllCollectionCC() { |
1363 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1364 |
< |
public void realCompute() { |
1363 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1364 |
> |
protected void realCompute() { |
1365 |
|
CCF f = new LCCF(null, 8); |
1366 |
|
CCF g = new LCCF(null, 9); |
1367 |
|
CCF h = new LCCF(null, 7); |
1384 |
|
* invokeAll(tasks) with any null task throws NPE |
1385 |
|
*/ |
1386 |
|
public void testInvokeAllNPECC() { |
1387 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1388 |
< |
public void realCompute() { |
1387 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1388 |
> |
protected void realCompute() { |
1389 |
|
CCF f = new LCCF(null, 8); |
1390 |
|
CCF g = new LCCF(null, 9); |
1391 |
|
CCF h = null; |
1401 |
|
* invokeAll(t1, t2) throw exception if any task does |
1402 |
|
*/ |
1403 |
|
public void testAbnormalInvokeAll2CC() { |
1404 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1405 |
< |
public void realCompute() { |
1404 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1405 |
> |
protected void realCompute() { |
1406 |
|
CCF f = new LCCF(null, 8); |
1407 |
|
FailingCCF g = new LFCCF(null, 9); |
1408 |
|
try { |
1419 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1420 |
|
*/ |
1421 |
|
public void testAbnormalInvokeAll1CC() { |
1422 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1423 |
< |
public void realCompute() { |
1422 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1423 |
> |
protected void realCompute() { |
1424 |
|
FailingCCF g = new LFCCF(null, 9); |
1425 |
|
try { |
1426 |
|
invokeAll(g); |
1436 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1437 |
|
*/ |
1438 |
|
public void testAbnormalInvokeAll3CC() { |
1439 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1440 |
< |
public void realCompute() { |
1439 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1440 |
> |
protected void realCompute() { |
1441 |
|
CCF f = new LCCF(null, 8); |
1442 |
|
FailingCCF g = new LFCCF(null, 9); |
1443 |
|
CCF h = new LCCF(null, 7); |
1452 |
|
} |
1453 |
|
|
1454 |
|
/** |
1455 |
< |
* invokeAll(collection) throws exception if any task does |
1455 |
> |
* invokeAll(collection) throws exception if any task does |
1456 |
|
*/ |
1457 |
|
public void testAbnormalInvokeAllCollectionCC() { |
1458 |
< |
ForkJoinTask a = new CheckedFJTask() { |
1459 |
< |
public void realCompute() { |
1458 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1459 |
> |
protected void realCompute() { |
1460 |
|
FailingCCF f = new LFCCF(null, 8); |
1461 |
|
CCF g = new LCCF(null, 9); |
1462 |
|
CCF h = new LCCF(null, 7); |
1474 |
|
checkInvoke(a); |
1475 |
|
} |
1476 |
|
|
1477 |
+ |
/** |
1478 |
+ |
* awaitQuiescence by a worker is equivalent in effect to |
1479 |
+ |
* ForkJoinTask.helpQuiesce() |
1480 |
+ |
*/ |
1481 |
+ |
public void testAwaitQuiescence1() throws Exception { |
1482 |
+ |
final ForkJoinPool p = new ForkJoinPool(); |
1483 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1484 |
+ |
final long startTime = System.nanoTime(); |
1485 |
+ |
assertTrue(p.isQuiescent()); |
1486 |
+ |
ForkJoinTask a = new CheckedRecursiveAction() { |
1487 |
+ |
protected void realCompute() { |
1488 |
+ |
FibAction f = new FibAction(8); |
1489 |
+ |
assertSame(f, f.fork()); |
1490 |
+ |
assertSame(p, ForkJoinTask.getPool()); |
1491 |
+ |
boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); |
1492 |
+ |
assertTrue(quiescent); |
1493 |
+ |
assertFalse(p.isQuiescent()); |
1494 |
+ |
while (!f.isDone()) { |
1495 |
+ |
assertFalse(p.getAsyncMode()); |
1496 |
+ |
assertFalse(p.isShutdown()); |
1497 |
+ |
assertFalse(p.isTerminating()); |
1498 |
+ |
assertFalse(p.isTerminated()); |
1499 |
+ |
Thread.yield(); |
1500 |
+ |
} |
1501 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1502 |
+ |
assertFalse(p.isQuiescent()); |
1503 |
+ |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1504 |
+ |
assertEquals(21, f.result); |
1505 |
+ |
}}; |
1506 |
+ |
p.execute(a); |
1507 |
+ |
while (!a.isDone() || !p.isQuiescent()) { |
1508 |
+ |
assertFalse(p.getAsyncMode()); |
1509 |
+ |
assertFalse(p.isShutdown()); |
1510 |
+ |
assertFalse(p.isTerminating()); |
1511 |
+ |
assertFalse(p.isTerminated()); |
1512 |
+ |
Thread.yield(); |
1513 |
+ |
} |
1514 |
+ |
assertEquals(0, p.getQueuedTaskCount()); |
1515 |
+ |
assertFalse(p.getAsyncMode()); |
1516 |
+ |
assertEquals(0, p.getQueuedSubmissionCount()); |
1517 |
+ |
assertFalse(p.hasQueuedSubmissions()); |
1518 |
+ |
while (p.getActiveThreadCount() != 0 |
1519 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1520 |
+ |
Thread.yield(); |
1521 |
+ |
assertFalse(p.isShutdown()); |
1522 |
+ |
assertFalse(p.isTerminating()); |
1523 |
+ |
assertFalse(p.isTerminated()); |
1524 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1525 |
+ |
} |
1526 |
+ |
} |
1527 |
+ |
|
1528 |
+ |
/** |
1529 |
+ |
* awaitQuiescence returns when pool isQuiescent() or the indicated |
1530 |
+ |
* timeout elapsed |
1531 |
+ |
*/ |
1532 |
+ |
public void testAwaitQuiescence2() throws Exception { |
1533 |
+ |
/* |
1534 |
+ |
* """It is possible to disable or limit the use of threads in the |
1535 |
+ |
* common pool by setting the parallelism property to zero. However |
1536 |
+ |
* doing so may cause unjoined tasks to never be executed.""" |
1537 |
+ |
*/ |
1538 |
+ |
if ("0".equals(System.getProperty( |
1539 |
+ |
"java.util.concurrent.ForkJoinPool.common.parallelism"))) |
1540 |
+ |
return; |
1541 |
+ |
final ForkJoinPool p = new ForkJoinPool(); |
1542 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1543 |
+ |
assertTrue(p.isQuiescent()); |
1544 |
+ |
final long startTime = System.nanoTime(); |
1545 |
+ |
ForkJoinTask a = new CheckedRecursiveAction() { |
1546 |
+ |
protected void realCompute() { |
1547 |
+ |
FibAction f = new FibAction(8); |
1548 |
+ |
assertSame(f, f.fork()); |
1549 |
+ |
while (!f.isDone() |
1550 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) { |
1551 |
+ |
assertFalse(p.getAsyncMode()); |
1552 |
+ |
assertFalse(p.isShutdown()); |
1553 |
+ |
assertFalse(p.isTerminating()); |
1554 |
+ |
assertFalse(p.isTerminated()); |
1555 |
+ |
Thread.yield(); |
1556 |
+ |
} |
1557 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1558 |
+ |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1559 |
+ |
assertEquals(21, f.result); |
1560 |
+ |
}}; |
1561 |
+ |
p.execute(a); |
1562 |
+ |
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); |
1563 |
+ |
assertTrue(p.isQuiescent()); |
1564 |
+ |
assertTrue(a.isDone()); |
1565 |
+ |
assertEquals(0, p.getQueuedTaskCount()); |
1566 |
+ |
assertFalse(p.getAsyncMode()); |
1567 |
+ |
assertEquals(0, p.getQueuedSubmissionCount()); |
1568 |
+ |
assertFalse(p.hasQueuedSubmissions()); |
1569 |
+ |
while (p.getActiveThreadCount() != 0 |
1570 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1571 |
+ |
Thread.yield(); |
1572 |
+ |
assertFalse(p.isShutdown()); |
1573 |
+ |
assertFalse(p.isTerminating()); |
1574 |
+ |
assertFalse(p.isTerminated()); |
1575 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1576 |
+ |
} |
1577 |
+ |
} |
1578 |
+ |
|
1579 |
|
} |