3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
+ |
import java.util.*; |
7 |
+ |
import java.util.concurrent.Executor; |
8 |
+ |
import java.util.concurrent.Executors; |
9 |
+ |
import java.util.concurrent.ExecutorService; |
10 |
+ |
import java.util.concurrent.AbstractExecutorService; |
11 |
+ |
import java.util.concurrent.CountDownLatch; |
12 |
+ |
import java.util.concurrent.Callable; |
13 |
+ |
import java.util.concurrent.Future; |
14 |
+ |
import java.util.concurrent.ExecutionException; |
15 |
+ |
import java.util.concurrent.CancellationException; |
16 |
+ |
import java.util.concurrent.RejectedExecutionException; |
17 |
+ |
import java.util.concurrent.ForkJoinPool; |
18 |
+ |
import java.util.concurrent.ForkJoinTask; |
19 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
20 |
+ |
import java.util.concurrent.RecursiveAction; |
21 |
+ |
import java.util.concurrent.RecursiveTask; |
22 |
+ |
import java.util.concurrent.TimeUnit; |
23 |
|
import junit.framework.*; |
24 |
< |
import java.util.concurrent.*; |
24 |
> |
import java.util.concurrent.TimeUnit; |
25 |
|
import java.util.concurrent.atomic.*; |
26 |
|
import java.util.*; |
27 |
|
|
28 |
|
public class ForkJoinTaskTest extends JSR166TestCase { |
29 |
|
|
30 |
|
public static void main(String[] args) { |
31 |
< |
junit.textui.TestRunner.run (suite()); |
31 |
> |
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(ForkJoinTaskTest.class); |
34 |
> |
return new TestSuite(ForkJoinTaskTest.class); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
44 |
|
|
45 |
|
static final ForkJoinPool mainPool = new ForkJoinPool(); |
46 |
|
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
47 |
< |
static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); |
48 |
< |
static { |
49 |
< |
asyncSingletonPool.setAsyncMode(true); |
33 |
< |
} |
34 |
< |
|
47 |
> |
static final ForkJoinPool asyncSingletonPool = |
48 |
> |
new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
49 |
> |
null, true); |
50 |
|
static final class FJException extends RuntimeException { |
51 |
|
FJException() { super(); } |
52 |
|
} |
163 |
|
|
164 |
|
} |
165 |
|
|
166 |
< |
static final class AsyncFib extends BinaryAsyncAction { |
166 |
> |
static final class AsyncFib extends BinaryAsyncAction { |
167 |
|
int number; |
168 |
|
public AsyncFib(int n) { |
169 |
|
this.number = n; |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
< |
static final class FailingAsyncFib extends BinaryAsyncAction { |
195 |
> |
static final class FailingAsyncFib extends BinaryAsyncAction { |
196 |
|
int number; |
197 |
|
public FailingAsyncFib(int n) { |
198 |
|
this.number = n; |
224 |
|
* invoke returns when task completes normally. |
225 |
|
* isCompletedAbnormally and isCancelled return false for normally |
226 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
212 |
– |
* |
227 |
|
*/ |
228 |
|
public void testInvoke() { |
229 |
|
RecursiveAction a = new RecursiveAction() { |
306 |
|
try { |
307 |
|
AsyncFib f = new AsyncFib(8); |
308 |
|
f.fork(); |
309 |
< |
f.get(5L, TimeUnit.SECONDS); |
309 |
> |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
310 |
|
threadAssertTrue(f.number == 21); |
311 |
|
threadAssertTrue(f.isDone()); |
312 |
|
} catch (Exception ex) { |
338 |
|
} |
339 |
|
|
340 |
|
/** |
327 |
– |
* helpJoin of a forked task returns when task completes |
328 |
– |
*/ |
329 |
– |
public void testForkHelpJoin() { |
330 |
– |
RecursiveAction a = new RecursiveAction() { |
331 |
– |
public void compute() { |
332 |
– |
AsyncFib f = new AsyncFib(8); |
333 |
– |
f.fork(); |
334 |
– |
f.helpJoin(); |
335 |
– |
threadAssertTrue(f.number == 21); |
336 |
– |
threadAssertTrue(f.isDone()); |
337 |
– |
} |
338 |
– |
}; |
339 |
– |
mainPool.invoke(a); |
340 |
– |
} |
341 |
– |
|
342 |
– |
/** |
341 |
|
* quietlyJoin of a forked task returns when task completes |
342 |
|
*/ |
343 |
|
public void testForkQuietlyJoin() { |
355 |
|
|
356 |
|
|
357 |
|
/** |
360 |
– |
* quietlyHelpJoin of a forked task returns when task completes |
361 |
– |
*/ |
362 |
– |
public void testForkQuietlyHelpJoin() { |
363 |
– |
RecursiveAction a = new RecursiveAction() { |
364 |
– |
public void compute() { |
365 |
– |
AsyncFib f = new AsyncFib(8); |
366 |
– |
f.fork(); |
367 |
– |
f.quietlyHelpJoin(); |
368 |
– |
threadAssertTrue(f.number == 21); |
369 |
– |
threadAssertTrue(f.isDone()); |
370 |
– |
} |
371 |
– |
}; |
372 |
– |
mainPool.invoke(a); |
373 |
– |
} |
374 |
– |
|
375 |
– |
|
376 |
– |
/** |
358 |
|
* helpQuiesce returns when tasks are complete. |
359 |
|
* getQueuedTaskCount returns 0 when quiescent |
360 |
|
*/ |
451 |
|
try { |
452 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
453 |
|
f.fork(); |
454 |
< |
f.get(5L, TimeUnit.SECONDS); |
454 |
> |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
455 |
|
shouldThrow(); |
456 |
|
} catch (ExecutionException success) { |
457 |
|
} catch (Exception ex) { |
463 |
|
} |
464 |
|
|
465 |
|
/** |
485 |
– |
* join of a forked task throws exception when task completes abnormally |
486 |
– |
*/ |
487 |
– |
public void testAbnormalForkHelpJoin() { |
488 |
– |
RecursiveAction a = new RecursiveAction() { |
489 |
– |
public void compute() { |
490 |
– |
try { |
491 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
492 |
– |
f.fork(); |
493 |
– |
f.helpJoin(); |
494 |
– |
shouldThrow(); |
495 |
– |
} catch (FJException success) { |
496 |
– |
} |
497 |
– |
} |
498 |
– |
}; |
499 |
– |
mainPool.invoke(a); |
500 |
– |
} |
501 |
– |
|
502 |
– |
/** |
503 |
– |
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
504 |
– |
* getException of failed task returns its exception. |
505 |
– |
* isCompletedAbnormally of a failed task returns true. |
506 |
– |
* isCancelled of a failed uncancelled task returns false |
507 |
– |
*/ |
508 |
– |
public void testAbnormalForkQuietlyHelpJoin() { |
509 |
– |
RecursiveAction a = new RecursiveAction() { |
510 |
– |
public void compute() { |
511 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
512 |
– |
f.fork(); |
513 |
– |
f.quietlyHelpJoin(); |
514 |
– |
threadAssertTrue(f.isDone()); |
515 |
– |
threadAssertTrue(f.isCompletedAbnormally()); |
516 |
– |
threadAssertFalse(f.isCancelled()); |
517 |
– |
threadAssertTrue(f.getException() instanceof FJException); |
518 |
– |
} |
519 |
– |
}; |
520 |
– |
mainPool.invoke(a); |
521 |
– |
} |
522 |
– |
|
523 |
– |
/** |
466 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
467 |
|
*/ |
468 |
|
public void testAbnormalForkQuietlyJoin() { |
547 |
|
AsyncFib f = new AsyncFib(8); |
548 |
|
f.cancel(true); |
549 |
|
f.fork(); |
550 |
< |
f.get(5L, TimeUnit.SECONDS); |
550 |
> |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
551 |
|
shouldThrow(); |
552 |
|
} catch (CancellationException success) { |
553 |
|
} catch (Exception ex) { |
556 |
|
} |
557 |
|
}; |
558 |
|
mainPool.invoke(a); |
617 |
– |
} |
618 |
– |
|
619 |
– |
/** |
620 |
– |
* join of a forked task throws exception when task cancelled |
621 |
– |
*/ |
622 |
– |
public void testCancelledForkHelpJoin() { |
623 |
– |
RecursiveAction a = new RecursiveAction() { |
624 |
– |
public void compute() { |
625 |
– |
try { |
626 |
– |
AsyncFib f = new AsyncFib(8); |
627 |
– |
f.cancel(true); |
628 |
– |
f.fork(); |
629 |
– |
f.helpJoin(); |
630 |
– |
shouldThrow(); |
631 |
– |
} catch (CancellationException success) { |
632 |
– |
} |
633 |
– |
} |
634 |
– |
}; |
635 |
– |
mainPool.invoke(a); |
636 |
– |
} |
637 |
– |
|
638 |
– |
/** |
639 |
– |
* quietlyHelpJoin of a forked task returns when task cancelled. |
640 |
– |
* getException of cancelled task returns its exception. |
641 |
– |
* isCompletedAbnormally of a cancelled task returns true. |
642 |
– |
* isCancelled of a cancelled task returns true |
643 |
– |
*/ |
644 |
– |
public void testCancelledForkQuietlyHelpJoin() { |
645 |
– |
RecursiveAction a = new RecursiveAction() { |
646 |
– |
public void compute() { |
647 |
– |
AsyncFib f = new AsyncFib(8); |
648 |
– |
f.cancel(true); |
649 |
– |
f.fork(); |
650 |
– |
f.quietlyHelpJoin(); |
651 |
– |
threadAssertTrue(f.isDone()); |
652 |
– |
threadAssertTrue(f.isCompletedAbnormally()); |
653 |
– |
threadAssertTrue(f.isCancelled()); |
654 |
– |
threadAssertTrue(f.getException() instanceof CancellationException); |
655 |
– |
} |
656 |
– |
}; |
657 |
– |
mainPool.invoke(a); |
559 |
|
} |
560 |
|
|
561 |
|
/** |