4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
9 |
> |
|
10 |
> |
import java.security.PrivilegedAction; |
11 |
> |
import java.security.PrivilegedExceptionAction; |
12 |
|
import java.util.ArrayList; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.List; |
11 |
– |
import java.util.concurrent.Executors; |
12 |
– |
import java.util.concurrent.ExecutorService; |
13 |
– |
import java.util.concurrent.AbstractExecutorService; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
|
import java.util.concurrent.Callable; |
16 |
< |
import java.util.concurrent.Future; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
|
import java.util.concurrent.ExecutionException; |
18 |
< |
import java.util.concurrent.CancellationException; |
19 |
< |
import java.util.concurrent.RejectedExecutionException; |
18 |
> |
import java.util.concurrent.Executors; |
19 |
> |
import java.util.concurrent.ExecutorService; |
20 |
|
import java.util.concurrent.ForkJoinPool; |
21 |
|
import java.util.concurrent.ForkJoinTask; |
22 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
+ |
import java.util.concurrent.Future; |
24 |
|
import java.util.concurrent.RecursiveTask; |
25 |
< |
import java.util.concurrent.TimeUnit; |
25 |
> |
import java.util.concurrent.RejectedExecutionException; |
26 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
27 |
|
import java.util.concurrent.locks.ReentrantLock; |
28 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
29 |
< |
import java.security.AccessControlException; |
30 |
< |
import java.security.Policy; |
31 |
< |
import java.security.PrivilegedAction; |
31 |
< |
import java.security.PrivilegedExceptionAction; |
28 |
> |
|
29 |
> |
import junit.framework.AssertionFailedError; |
30 |
> |
import junit.framework.Test; |
31 |
> |
import junit.framework.TestSuite; |
32 |
|
|
33 |
|
public class ForkJoinPoolTest extends JSR166TestCase { |
34 |
|
public static void main(String[] args) { |
35 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
107 |
|
static final class FibTask extends RecursiveTask<Integer> { |
108 |
|
final int number; |
109 |
|
FibTask(int n) { number = n; } |
110 |
< |
public Integer compute() { |
110 |
> |
protected Integer compute() { |
111 |
|
int n = number; |
112 |
|
if (n <= 1) |
113 |
|
return n; |
135 |
|
this.locker = locker; |
136 |
|
this.lock = lock; |
137 |
|
} |
138 |
< |
public Integer compute() { |
138 |
> |
protected Integer compute() { |
139 |
|
int n; |
140 |
|
LockingFibTask f1 = null; |
141 |
|
LockingFibTask f2 = null; |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
+ |
* awaitTermination on a non-shutdown pool times out |
230 |
+ |
*/ |
231 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
232 |
+ |
ForkJoinPool p = new ForkJoinPool(1); |
233 |
+ |
assertFalse(p.isTerminated()); |
234 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
235 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
236 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
237 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
238 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
239 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
240 |
+ |
long timeoutNanos = 999999L; |
241 |
+ |
long startTime = System.nanoTime(); |
242 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
243 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
244 |
+ |
assertFalse(p.isTerminated()); |
245 |
+ |
startTime = System.nanoTime(); |
246 |
+ |
long timeoutMillis = timeoutMillis(); |
247 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
248 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
249 |
+ |
assertFalse(p.isTerminated()); |
250 |
+ |
p.shutdown(); |
251 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
252 |
+ |
assertTrue(p.isTerminated()); |
253 |
+ |
} |
254 |
+ |
|
255 |
+ |
/** |
256 |
|
* setUncaughtExceptionHandler changes handler for uncaught exceptions. |
257 |
|
* |
258 |
|
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
418 |
|
ExecutorService e = new ForkJoinPool(1); |
419 |
|
try { |
420 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
421 |
< |
CheckedRunnable task = new CheckedRunnable() { |
421 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
422 |
|
public void realRun() { |
423 |
|
done.set(true); |
424 |
< |
}}; |
398 |
< |
Future<?> future = e.submit(task); |
424 |
> |
}}); |
425 |
|
assertNull(future.get()); |
426 |
|
assertNull(future.get(0, MILLISECONDS)); |
427 |
|
assertTrue(done.get()); |
981 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
982 |
|
*/ |
983 |
|
public void testTimedInvokeAll5() throws Throwable { |
984 |
< |
ExecutorService e = new ForkJoinPool(1); |
985 |
< |
try { |
984 |
> |
try (PoolCloser<ForkJoinPool> poolCloser |
985 |
> |
= new PoolCloser(new ForkJoinPool(1))) { |
986 |
> |
ForkJoinPool e = poolCloser.pool; |
987 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
988 |
|
l.add(new StringTask()); |
989 |
|
l.add(new StringTask()); |
990 |
|
List<Future<String>> futures |
991 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
991 |
> |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
992 |
|
assertEquals(2, futures.size()); |
993 |
|
for (Future<String> future : futures) |
994 |
|
assertSame(TEST_STRING, future.get()); |
968 |
– |
} finally { |
969 |
– |
joinPool(e); |
995 |
|
} |
996 |
|
} |
997 |
|
|