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.Arrays; |
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
9 |
– |
import java.util.concurrent.SynchronousQueue; |
12 |
|
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
import java.util.concurrent.ForkJoinTask; |
15 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
16 |
|
import java.util.concurrent.RecursiveAction; |
17 |
+ |
import java.util.concurrent.SynchronousQueue; |
18 |
|
import java.util.concurrent.ThreadLocalRandom; |
16 |
– |
import java.util.concurrent.TimeUnit; |
19 |
|
import java.util.concurrent.TimeoutException; |
20 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
< |
import java.util.Arrays; |
22 |
< |
import java.util.HashSet; |
20 |
> |
|
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class RecursiveActionTest extends JSR166TestCase { |
25 |
|
|
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
|
30 |
|
public static Test suite() { |
46 |
|
} |
47 |
|
|
48 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
49 |
< |
try { |
49 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
50 |
|
checkNotDone(a); |
51 |
|
|
52 |
|
assertNull(pool.invoke(a)); |
53 |
|
|
54 |
|
checkCompletedNormally(a); |
53 |
– |
} finally { |
54 |
– |
joinPool(pool); |
55 |
|
} |
56 |
|
} |
57 |
|
|
73 |
|
|
74 |
|
Thread.currentThread().interrupt(); |
75 |
|
try { |
76 |
< |
a.get(5L, SECONDS); |
76 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
77 |
|
shouldThrow(); |
78 |
|
} catch (InterruptedException success) { |
79 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
80 |
|
} |
81 |
|
|
82 |
|
try { |
83 |
< |
a.get(0L, SECONDS); |
83 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
84 |
|
shouldThrow(); |
85 |
|
} catch (TimeoutException success) { |
86 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
98 |
|
assertFalse(a.cancel(true)); |
99 |
|
try { |
100 |
|
assertNull(a.get()); |
101 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
102 |
< |
try { |
103 |
< |
assertNull(a.get(5L, SECONDS)); |
104 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
101 |
> |
assertNull(a.get(randomTimeout(), randomTimeUnit())); |
102 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
103 |
|
} |
104 |
|
|
105 |
|
void checkCancelled(RecursiveAction a) { |
123 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
|
|
125 |
|
try { |
126 |
< |
a.get(5L, SECONDS); |
126 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
127 |
|
shouldThrow(); |
128 |
|
} catch (CancellationException success) { |
129 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
154 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
|
|
156 |
|
try { |
157 |
< |
a.get(5L, SECONDS); |
157 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
158 |
|
shouldThrow(); |
159 |
|
} catch (ExecutionException success) { |
160 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
166 |
|
public FJException(Throwable cause) { super(cause); } |
167 |
|
} |
168 |
|
|
169 |
< |
// A simple recursive action for testing |
169 |
> |
/** A simple recursive action for testing. */ |
170 |
|
final class FibAction extends CheckedRecursiveAction { |
171 |
|
final int number; |
172 |
|
int result; |
173 |
|
FibAction(int n) { number = n; } |
174 |
< |
public void realCompute() { |
174 |
> |
protected void realCompute() { |
175 |
|
int n = number; |
176 |
|
if (n <= 1) |
177 |
|
result = n; |
184 |
|
} |
185 |
|
} |
186 |
|
|
187 |
< |
// A recursive action failing in base case |
187 |
> |
/** A recursive action failing in base case. */ |
188 |
|
static final class FailingFibAction extends RecursiveAction { |
189 |
|
final int number; |
190 |
|
int result; |
209 |
|
*/ |
210 |
|
public void testInvoke() { |
211 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
212 |
< |
public void realCompute() { |
212 |
> |
protected void realCompute() { |
213 |
|
FibAction f = new FibAction(8); |
214 |
|
assertNull(f.invoke()); |
215 |
|
assertEquals(21, f.result); |
225 |
|
*/ |
226 |
|
public void testQuietlyInvoke() { |
227 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
228 |
< |
public void realCompute() { |
228 |
> |
protected void realCompute() { |
229 |
|
FibAction f = new FibAction(8); |
230 |
|
f.quietlyInvoke(); |
231 |
|
assertEquals(21, f.result); |
239 |
|
*/ |
240 |
|
public void testForkJoin() { |
241 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
242 |
< |
public void realCompute() { |
242 |
> |
protected void realCompute() { |
243 |
|
FibAction f = new FibAction(8); |
244 |
|
assertSame(f, f.fork()); |
245 |
|
assertNull(f.join()); |
254 |
|
*/ |
255 |
|
public void testJoinIgnoresInterrupts() { |
256 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
257 |
< |
public void realCompute() { |
257 |
> |
protected void realCompute() { |
258 |
|
FibAction f = new FibAction(8); |
259 |
< |
final Thread myself = Thread.currentThread(); |
259 |
> |
final Thread currentThread = Thread.currentThread(); |
260 |
|
|
261 |
|
// test join() |
262 |
|
assertSame(f, f.fork()); |
263 |
< |
myself.interrupt(); |
266 |
< |
assertTrue(myself.isInterrupted()); |
263 |
> |
currentThread.interrupt(); |
264 |
|
assertNull(f.join()); |
265 |
|
Thread.interrupted(); |
266 |
|
assertEquals(21, f.result); |
269 |
|
f = new FibAction(8); |
270 |
|
f.cancel(true); |
271 |
|
assertSame(f, f.fork()); |
272 |
< |
myself.interrupt(); |
276 |
< |
assertTrue(myself.isInterrupted()); |
272 |
> |
currentThread.interrupt(); |
273 |
|
try { |
274 |
|
f.join(); |
275 |
|
shouldThrow(); |
281 |
|
f = new FibAction(8); |
282 |
|
f.completeExceptionally(new FJException()); |
283 |
|
assertSame(f, f.fork()); |
284 |
< |
myself.interrupt(); |
289 |
< |
assertTrue(myself.isInterrupted()); |
284 |
> |
currentThread.interrupt(); |
285 |
|
try { |
286 |
|
f.join(); |
287 |
|
shouldThrow(); |
293 |
|
// test quietlyJoin() |
294 |
|
f = new FibAction(8); |
295 |
|
assertSame(f, f.fork()); |
296 |
< |
myself.interrupt(); |
302 |
< |
assertTrue(myself.isInterrupted()); |
296 |
> |
currentThread.interrupt(); |
297 |
|
f.quietlyJoin(); |
298 |
|
Thread.interrupted(); |
299 |
|
assertEquals(21, f.result); |
302 |
|
f = new FibAction(8); |
303 |
|
f.cancel(true); |
304 |
|
assertSame(f, f.fork()); |
305 |
< |
myself.interrupt(); |
312 |
< |
assertTrue(myself.isInterrupted()); |
305 |
> |
currentThread.interrupt(); |
306 |
|
f.quietlyJoin(); |
307 |
|
Thread.interrupted(); |
308 |
|
checkCancelled(f); |
310 |
|
f = new FibAction(8); |
311 |
|
f.completeExceptionally(new FJException()); |
312 |
|
assertSame(f, f.fork()); |
313 |
< |
myself.interrupt(); |
321 |
< |
assertTrue(myself.isInterrupted()); |
313 |
> |
currentThread.interrupt(); |
314 |
|
f.quietlyJoin(); |
315 |
|
Thread.interrupted(); |
316 |
|
checkCompletedAbnormally(f, f.getException()); |
328 |
|
final SynchronousQueue<FibAction[]> sq = |
329 |
|
new SynchronousQueue<FibAction[]>(); |
330 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
331 |
< |
public void realCompute() throws InterruptedException { |
331 |
> |
protected void realCompute() throws InterruptedException { |
332 |
|
FibAction[] fibActions = new FibAction[6]; |
333 |
|
for (int i = 0; i < fibActions.length; i++) |
334 |
|
fibActions[i] = new FibAction(8); |
350 |
|
public void realRun() throws InterruptedException { |
351 |
|
FibAction[] fibActions = sq.take(); |
352 |
|
FibAction f; |
353 |
< |
final Thread myself = Thread.currentThread(); |
353 |
> |
final Thread currentThread = Thread.currentThread(); |
354 |
|
|
355 |
|
// test join() ------------ |
356 |
|
|
357 |
|
f = fibActions[0]; |
358 |
|
assertFalse(ForkJoinTask.inForkJoinPool()); |
359 |
< |
myself.interrupt(); |
368 |
< |
assertTrue(myself.isInterrupted()); |
359 |
> |
currentThread.interrupt(); |
360 |
|
assertNull(f.join()); |
361 |
|
assertTrue(Thread.interrupted()); |
362 |
|
assertEquals(21, f.result); |
363 |
|
checkCompletedNormally(f); |
364 |
|
|
365 |
|
f = fibActions[1]; |
366 |
< |
myself.interrupt(); |
376 |
< |
assertTrue(myself.isInterrupted()); |
366 |
> |
currentThread.interrupt(); |
367 |
|
try { |
368 |
|
f.join(); |
369 |
|
shouldThrow(); |
373 |
|
} |
374 |
|
|
375 |
|
f = fibActions[2]; |
376 |
< |
myself.interrupt(); |
387 |
< |
assertTrue(myself.isInterrupted()); |
376 |
> |
currentThread.interrupt(); |
377 |
|
try { |
378 |
|
f.join(); |
379 |
|
shouldThrow(); |
385 |
|
// test quietlyJoin() --------- |
386 |
|
|
387 |
|
f = fibActions[3]; |
388 |
< |
myself.interrupt(); |
400 |
< |
assertTrue(myself.isInterrupted()); |
388 |
> |
currentThread.interrupt(); |
389 |
|
f.quietlyJoin(); |
390 |
|
assertTrue(Thread.interrupted()); |
391 |
|
assertEquals(21, f.result); |
392 |
|
checkCompletedNormally(f); |
393 |
|
|
394 |
|
f = fibActions[4]; |
395 |
< |
myself.interrupt(); |
408 |
< |
assertTrue(myself.isInterrupted()); |
395 |
> |
currentThread.interrupt(); |
396 |
|
f.quietlyJoin(); |
397 |
|
assertTrue(Thread.interrupted()); |
398 |
|
checkCancelled(f); |
399 |
|
|
400 |
|
f = fibActions[5]; |
401 |
< |
myself.interrupt(); |
415 |
< |
assertTrue(myself.isInterrupted()); |
401 |
> |
currentThread.interrupt(); |
402 |
|
f.quietlyJoin(); |
403 |
|
assertTrue(Thread.interrupted()); |
404 |
|
assertTrue(f.getException() instanceof FJException); |
409 |
|
|
410 |
|
t = newStartedThread(r); |
411 |
|
testInvokeOnPool(mainPool(), a); |
412 |
< |
awaitTermination(t, LONG_DELAY_MS); |
412 |
> |
awaitTermination(t); |
413 |
|
|
414 |
|
a.reinitialize(); |
415 |
|
t = newStartedThread(r); |
416 |
|
testInvokeOnPool(singletonPool(), a); |
417 |
< |
awaitTermination(t, LONG_DELAY_MS); |
417 |
> |
awaitTermination(t); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
422 |
|
*/ |
423 |
|
public void testForkGet() { |
424 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
425 |
< |
public void realCompute() throws Exception { |
425 |
> |
protected void realCompute() throws Exception { |
426 |
|
FibAction f = new FibAction(8); |
427 |
|
assertSame(f, f.fork()); |
428 |
|
assertNull(f.get()); |
437 |
|
*/ |
438 |
|
public void testForkTimedGet() { |
439 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
440 |
< |
public void realCompute() throws Exception { |
440 |
> |
protected void realCompute() throws Exception { |
441 |
|
FibAction f = new FibAction(8); |
442 |
|
assertSame(f, f.fork()); |
443 |
< |
assertNull(f.get(5L, SECONDS)); |
443 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
444 |
|
assertEquals(21, f.result); |
445 |
|
checkCompletedNormally(f); |
446 |
|
}}; |
452 |
|
*/ |
453 |
|
public void testForkTimedGetNPE() { |
454 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
455 |
< |
public void realCompute() throws Exception { |
455 |
> |
protected void realCompute() throws Exception { |
456 |
|
FibAction f = new FibAction(8); |
457 |
|
assertSame(f, f.fork()); |
458 |
|
try { |
459 |
< |
f.get(5L, null); |
459 |
> |
f.get(randomTimeout(), null); |
460 |
|
shouldThrow(); |
461 |
|
} catch (NullPointerException success) {} |
462 |
|
}}; |
468 |
|
*/ |
469 |
|
public void testForkQuietlyJoin() { |
470 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
471 |
< |
public void realCompute() { |
471 |
> |
protected void realCompute() { |
472 |
|
FibAction f = new FibAction(8); |
473 |
|
assertSame(f, f.fork()); |
474 |
|
f.quietlyJoin(); |
484 |
|
*/ |
485 |
|
public void testForkHelpQuiesce() { |
486 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
487 |
< |
public void realCompute() { |
487 |
> |
protected void realCompute() { |
488 |
|
FibAction f = new FibAction(8); |
489 |
|
assertSame(f, f.fork()); |
490 |
|
helpQuiesce(); |
491 |
+ |
while (!f.isDone()) // wait out race |
492 |
+ |
; |
493 |
|
assertEquals(21, f.result); |
494 |
|
assertEquals(0, getQueuedTaskCount()); |
495 |
|
checkCompletedNormally(f); |
502 |
|
*/ |
503 |
|
public void testAbnormalInvoke() { |
504 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
505 |
< |
public void realCompute() { |
505 |
> |
protected void realCompute() { |
506 |
|
FailingFibAction f = new FailingFibAction(8); |
507 |
|
try { |
508 |
|
f.invoke(); |
519 |
|
*/ |
520 |
|
public void testAbnormalQuietlyInvoke() { |
521 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
522 |
< |
public void realCompute() { |
522 |
> |
protected void realCompute() { |
523 |
|
FailingFibAction f = new FailingFibAction(8); |
524 |
|
f.quietlyInvoke(); |
525 |
|
assertTrue(f.getException() instanceof FJException); |
533 |
|
*/ |
534 |
|
public void testAbnormalForkJoin() { |
535 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
536 |
< |
public void realCompute() { |
536 |
> |
protected void realCompute() { |
537 |
|
FailingFibAction f = new FailingFibAction(8); |
538 |
|
assertSame(f, f.fork()); |
539 |
|
try { |
551 |
|
*/ |
552 |
|
public void testAbnormalForkGet() { |
553 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
554 |
< |
public void realCompute() throws Exception { |
554 |
> |
protected void realCompute() throws Exception { |
555 |
|
FailingFibAction f = new FailingFibAction(8); |
556 |
|
assertSame(f, f.fork()); |
557 |
|
try { |
571 |
|
*/ |
572 |
|
public void testAbnormalForkTimedGet() { |
573 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
574 |
< |
public void realCompute() throws Exception { |
574 |
> |
protected void realCompute() throws Exception { |
575 |
|
FailingFibAction f = new FailingFibAction(8); |
576 |
|
assertSame(f, f.fork()); |
577 |
|
try { |
578 |
< |
f.get(5L, TimeUnit.SECONDS); |
578 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
579 |
|
shouldThrow(); |
580 |
|
} catch (ExecutionException success) { |
581 |
|
Throwable cause = success.getCause(); |
591 |
|
*/ |
592 |
|
public void testAbnormalForkQuietlyJoin() { |
593 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
594 |
< |
public void realCompute() { |
594 |
> |
protected void realCompute() { |
595 |
|
FailingFibAction f = new FailingFibAction(8); |
596 |
|
assertSame(f, f.fork()); |
597 |
|
f.quietlyJoin(); |
606 |
|
*/ |
607 |
|
public void testCancelledInvoke() { |
608 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
609 |
< |
public void realCompute() { |
609 |
> |
protected void realCompute() { |
610 |
|
FibAction f = new FibAction(8); |
611 |
|
assertTrue(f.cancel(true)); |
612 |
|
try { |
624 |
|
*/ |
625 |
|
public void testCancelledForkJoin() { |
626 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
627 |
< |
public void realCompute() { |
627 |
> |
protected void realCompute() { |
628 |
|
FibAction f = new FibAction(8); |
629 |
|
assertTrue(f.cancel(true)); |
630 |
|
assertSame(f, f.fork()); |
643 |
|
*/ |
644 |
|
public void testCancelledForkGet() { |
645 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
646 |
< |
public void realCompute() throws Exception { |
646 |
> |
protected void realCompute() throws Exception { |
647 |
|
FibAction f = new FibAction(8); |
648 |
|
assertTrue(f.cancel(true)); |
649 |
|
assertSame(f, f.fork()); |
662 |
|
*/ |
663 |
|
public void testCancelledForkTimedGet() { |
664 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
665 |
< |
public void realCompute() throws Exception { |
665 |
> |
protected void realCompute() throws Exception { |
666 |
|
FibAction f = new FibAction(8); |
667 |
|
assertTrue(f.cancel(true)); |
668 |
|
assertSame(f, f.fork()); |
669 |
|
try { |
670 |
< |
f.get(5L, SECONDS); |
670 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
671 |
|
shouldThrow(); |
672 |
|
} catch (CancellationException success) { |
673 |
|
checkCancelled(f); |
681 |
|
*/ |
682 |
|
public void testCancelledForkQuietlyJoin() { |
683 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
684 |
< |
public void realCompute() { |
684 |
> |
protected void realCompute() { |
685 |
|
FibAction f = new FibAction(8); |
686 |
|
assertTrue(f.cancel(true)); |
687 |
|
assertSame(f, f.fork()); |
697 |
|
public void testGetPool() { |
698 |
|
final ForkJoinPool mainPool = mainPool(); |
699 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
700 |
< |
public void realCompute() { |
700 |
> |
protected void realCompute() { |
701 |
|
assertSame(mainPool, getPool()); |
702 |
|
}}; |
703 |
|
testInvokeOnPool(mainPool, a); |
708 |
|
*/ |
709 |
|
public void testGetPool2() { |
710 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
711 |
< |
public void realCompute() { |
711 |
> |
protected void realCompute() { |
712 |
|
assertNull(getPool()); |
713 |
|
}}; |
714 |
|
assertNull(a.invoke()); |
719 |
|
*/ |
720 |
|
public void testInForkJoinPool() { |
721 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
722 |
< |
public void realCompute() { |
722 |
> |
protected void realCompute() { |
723 |
|
assertTrue(inForkJoinPool()); |
724 |
|
}}; |
725 |
|
testInvokeOnPool(mainPool(), a); |
730 |
|
*/ |
731 |
|
public void testInForkJoinPool2() { |
732 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
733 |
< |
public void realCompute() { |
733 |
> |
protected void realCompute() { |
734 |
|
assertFalse(inForkJoinPool()); |
735 |
|
}}; |
736 |
|
assertNull(a.invoke()); |
742 |
|
public void testWorkerGetPool() { |
743 |
|
final ForkJoinPool mainPool = mainPool(); |
744 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
745 |
< |
public void realCompute() { |
745 |
> |
protected void realCompute() { |
746 |
|
ForkJoinWorkerThread w = |
747 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
748 |
|
assertSame(mainPool, w.getPool()); |
756 |
|
public void testWorkerGetPoolIndex() { |
757 |
|
final ForkJoinPool mainPool = mainPool(); |
758 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
759 |
< |
public void realCompute() { |
759 |
> |
protected void realCompute() { |
760 |
|
ForkJoinWorkerThread w = |
761 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
762 |
|
assertTrue(w.getPoolIndex() >= 0); |
771 |
|
*/ |
772 |
|
public void testSetRawResult() { |
773 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
774 |
< |
public void realCompute() { |
774 |
> |
protected void realCompute() { |
775 |
|
setRawResult(null); |
776 |
|
assertNull(getRawResult()); |
777 |
|
}}; |
783 |
|
*/ |
784 |
|
public void testReinitialize() { |
785 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
786 |
< |
public void realCompute() { |
786 |
> |
protected void realCompute() { |
787 |
|
FibAction f = new FibAction(8); |
788 |
|
checkNotDone(f); |
789 |
|
|
803 |
|
*/ |
804 |
|
public void testReinitializeAbnormal() { |
805 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
806 |
< |
public void realCompute() { |
806 |
> |
protected void realCompute() { |
807 |
|
FailingFibAction f = new FailingFibAction(8); |
808 |
|
checkNotDone(f); |
809 |
|
|
826 |
|
*/ |
827 |
|
public void testCompleteExceptionally() { |
828 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
829 |
< |
public void realCompute() { |
829 |
> |
protected void realCompute() { |
830 |
|
FibAction f = new FibAction(8); |
831 |
|
f.completeExceptionally(new FJException()); |
832 |
|
try { |
844 |
|
*/ |
845 |
|
public void testComplete() { |
846 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
847 |
< |
public void realCompute() { |
847 |
> |
protected void realCompute() { |
848 |
|
FibAction f = new FibAction(8); |
849 |
|
f.complete(null); |
850 |
|
assertNull(f.invoke()); |
859 |
|
*/ |
860 |
|
public void testInvokeAll2() { |
861 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
862 |
< |
public void realCompute() { |
862 |
> |
protected void realCompute() { |
863 |
|
FibAction f = new FibAction(8); |
864 |
|
FibAction g = new FibAction(9); |
865 |
|
invokeAll(f, g); |
876 |
|
*/ |
877 |
|
public void testInvokeAll1() { |
878 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
879 |
< |
public void realCompute() { |
879 |
> |
protected void realCompute() { |
880 |
|
FibAction f = new FibAction(8); |
881 |
|
invokeAll(f); |
882 |
|
checkCompletedNormally(f); |
890 |
|
*/ |
891 |
|
public void testInvokeAll3() { |
892 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
893 |
< |
public void realCompute() { |
893 |
> |
protected void realCompute() { |
894 |
|
FibAction f = new FibAction(8); |
895 |
|
FibAction g = new FibAction(9); |
896 |
|
FibAction h = new FibAction(7); |
913 |
|
*/ |
914 |
|
public void testInvokeAllCollection() { |
915 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
916 |
< |
public void realCompute() { |
916 |
> |
protected void realCompute() { |
917 |
|
FibAction f = new FibAction(8); |
918 |
|
FibAction g = new FibAction(9); |
919 |
|
FibAction h = new FibAction(7); |
940 |
|
*/ |
941 |
|
public void testInvokeAllNPE() { |
942 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
943 |
< |
public void realCompute() { |
943 |
> |
protected void realCompute() { |
944 |
|
FibAction f = new FibAction(8); |
945 |
|
FibAction g = new FibAction(9); |
946 |
|
FibAction h = null; |
957 |
|
*/ |
958 |
|
public void testAbnormalInvokeAll2() { |
959 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
960 |
< |
public void realCompute() { |
960 |
> |
protected void realCompute() { |
961 |
|
FibAction f = new FibAction(8); |
962 |
|
FailingFibAction g = new FailingFibAction(9); |
963 |
|
try { |
975 |
|
*/ |
976 |
|
public void testAbnormalInvokeAll1() { |
977 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
978 |
< |
public void realCompute() { |
978 |
> |
protected void realCompute() { |
979 |
|
FailingFibAction g = new FailingFibAction(9); |
980 |
|
try { |
981 |
|
invokeAll(g); |
992 |
|
*/ |
993 |
|
public void testAbnormalInvokeAll3() { |
994 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
995 |
< |
public void realCompute() { |
995 |
> |
protected void realCompute() { |
996 |
|
FibAction f = new FibAction(8); |
997 |
|
FailingFibAction g = new FailingFibAction(9); |
998 |
|
FibAction h = new FibAction(7); |
1011 |
|
*/ |
1012 |
|
public void testAbnormalInvokeAllCollection() { |
1013 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1014 |
< |
public void realCompute() { |
1014 |
> |
protected void realCompute() { |
1015 |
|
FailingFibAction f = new FailingFibAction(8); |
1016 |
|
FibAction g = new FibAction(9); |
1017 |
|
FibAction h = new FibAction(7); |
1035 |
|
*/ |
1036 |
|
public void testTryUnfork() { |
1037 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1038 |
< |
public void realCompute() { |
1038 |
> |
protected void realCompute() { |
1039 |
|
FibAction g = new FibAction(9); |
1040 |
|
assertSame(g, g.fork()); |
1041 |
|
FibAction f = new FibAction(8); |
1054 |
|
*/ |
1055 |
|
public void testGetSurplusQueuedTaskCount() { |
1056 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1057 |
< |
public void realCompute() { |
1057 |
> |
protected void realCompute() { |
1058 |
|
FibAction h = new FibAction(7); |
1059 |
|
assertSame(h, h.fork()); |
1060 |
|
FibAction g = new FibAction(9); |
1076 |
|
*/ |
1077 |
|
public void testPeekNextLocalTask() { |
1078 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1079 |
< |
public void realCompute() { |
1079 |
> |
protected void realCompute() { |
1080 |
|
FibAction g = new FibAction(9); |
1081 |
|
assertSame(g, g.fork()); |
1082 |
|
FibAction f = new FibAction(8); |
1097 |
|
*/ |
1098 |
|
public void testPollNextLocalTask() { |
1099 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1100 |
< |
public void realCompute() { |
1100 |
> |
protected void realCompute() { |
1101 |
|
FibAction g = new FibAction(9); |
1102 |
|
assertSame(g, g.fork()); |
1103 |
|
FibAction f = new FibAction(8); |
1115 |
|
*/ |
1116 |
|
public void testPollTask() { |
1117 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1118 |
< |
public void realCompute() { |
1118 |
> |
protected void realCompute() { |
1119 |
|
FibAction g = new FibAction(9); |
1120 |
|
assertSame(g, g.fork()); |
1121 |
|
FibAction f = new FibAction(8); |
1133 |
|
*/ |
1134 |
|
public void testPeekNextLocalTaskAsync() { |
1135 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1136 |
< |
public void realCompute() { |
1136 |
> |
protected void realCompute() { |
1137 |
|
FibAction g = new FibAction(9); |
1138 |
|
assertSame(g, g.fork()); |
1139 |
|
FibAction f = new FibAction(8); |
1153 |
|
*/ |
1154 |
|
public void testPollNextLocalTaskAsync() { |
1155 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1156 |
< |
public void realCompute() { |
1156 |
> |
protected void realCompute() { |
1157 |
|
FibAction g = new FibAction(9); |
1158 |
|
assertSame(g, g.fork()); |
1159 |
|
FibAction f = new FibAction(8); |
1172 |
|
*/ |
1173 |
|
public void testPollTaskAsync() { |
1174 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1175 |
< |
public void realCompute() { |
1175 |
> |
protected void realCompute() { |
1176 |
|
FibAction g = new FibAction(9); |
1177 |
|
assertSame(g, g.fork()); |
1178 |
|
FibAction f = new FibAction(8); |
1185 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1186 |
|
} |
1187 |
|
|
1188 |
+ |
/** Demo from RecursiveAction javadoc */ |
1189 |
|
static class SortTask extends RecursiveAction { |
1190 |
|
final long[] array; final int lo, hi; |
1191 |
|
SortTask(long[] array, int lo, int hi) { |
1192 |
|
this.array = array; this.lo = lo; this.hi = hi; |
1193 |
|
} |
1194 |
< |
final static int THRESHOLD = 100; |
1194 |
> |
SortTask(long[] array) { this(array, 0, array.length); } |
1195 |
|
protected void compute() { |
1196 |
|
if (hi - lo < THRESHOLD) |
1197 |
< |
sequentiallySort(array, lo, hi); |
1197 |
> |
sortSequentially(lo, hi); |
1198 |
|
else { |
1199 |
|
int mid = (lo + hi) >>> 1; |
1200 |
|
invokeAll(new SortTask(array, lo, mid), |
1201 |
|
new SortTask(array, mid, hi)); |
1202 |
< |
merge(array, lo, mid, hi); |
1202 |
> |
merge(lo, mid, hi); |
1203 |
|
} |
1204 |
|
} |
1205 |
< |
static void sequentiallySort(long[] array, int lo, int hi) { |
1205 |
> |
// implementation details follow: |
1206 |
> |
static final int THRESHOLD = 100; |
1207 |
> |
void sortSequentially(int lo, int hi) { |
1208 |
|
Arrays.sort(array, lo, hi); |
1209 |
|
} |
1210 |
< |
static void merge(long[] array, int lo, int mid, int hi) { |
1211 |
< |
int n = hi - lo; |
1212 |
< |
long[] buf = new long[n]; |
1213 |
< |
int a = lo, b = mid; |
1214 |
< |
for (int i = 0; i < n; i++) |
1224 |
< |
buf[i] = (b == hi || (a < mid && array[a] < array[b])) ? |
1225 |
< |
array[a++] : array[b++]; |
1226 |
< |
System.arraycopy(buf, 0, array, lo, n); |
1210 |
> |
void merge(int lo, int mid, int hi) { |
1211 |
> |
long[] buf = Arrays.copyOfRange(array, lo, mid); |
1212 |
> |
for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
1213 |
> |
array[j] = (k == hi || buf[i] < array[k]) ? |
1214 |
> |
buf[i++] : array[k++]; |
1215 |
|
} |
1216 |
|
} |
1217 |
|
|
1224 |
|
for (int i = 0; i < array.length; i++) |
1225 |
|
array[i] = rnd.nextLong(); |
1226 |
|
long[] arrayClone = array.clone(); |
1227 |
< |
testInvokeOnPool(mainPool(), |
1240 |
< |
new SortTask(array, 0, array.length)); |
1227 |
> |
testInvokeOnPool(mainPool(), new SortTask(array)); |
1228 |
|
Arrays.sort(arrayClone); |
1229 |
|
assertTrue(Arrays.equals(array, arrayClone)); |
1230 |
|
} |