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 |
|
|
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 |
|
} |
339 |
|
} |
340 |
|
|
341 |
|
/** |
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 |
– |
/** |
342 |
|
* quietlyJoin of a forked task returns when task completes |
343 |
|
*/ |
344 |
|
public void testForkQuietlyJoin() { |
356 |
|
|
357 |
|
|
358 |
|
/** |
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 |
– |
/** |
359 |
|
* helpQuiesce returns when tasks are complete. |
360 |
|
* getQueuedTaskCount returns 0 when quiescent |
361 |
|
*/ |
464 |
|
} |
465 |
|
|
466 |
|
/** |
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 |
– |
/** |
467 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
468 |
|
*/ |
469 |
|
public void testAbnormalForkQuietlyJoin() { |
557 |
|
} |
558 |
|
}; |
559 |
|
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); |
560 |
|
} |
561 |
|
|
562 |
|
/** |