1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
8 |
> |
import java.util.ArrayList; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.List; |
11 |
> |
import java.util.concurrent.Executors; |
12 |
> |
import java.util.concurrent.ExecutorService; |
13 |
> |
import java.util.concurrent.CountDownLatch; |
14 |
> |
import java.util.concurrent.Callable; |
15 |
> |
import java.util.concurrent.Future; |
16 |
> |
import java.util.concurrent.ExecutionException; |
17 |
> |
import java.util.concurrent.RejectedExecutionException; |
18 |
> |
import java.util.concurrent.ForkJoinPool; |
19 |
> |
import java.util.concurrent.ForkJoinTask; |
20 |
> |
import java.util.concurrent.ForkJoinWorkerThread; |
21 |
> |
import java.util.concurrent.RecursiveTask; |
22 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
23 |
> |
import java.util.concurrent.locks.ReentrantLock; |
24 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
25 |
< |
import java.util.concurrent.locks.*; |
26 |
< |
import java.security.*; |
25 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
26 |
> |
import java.security.PrivilegedAction; |
27 |
> |
import java.security.PrivilegedExceptionAction; |
28 |
|
|
29 |
|
public class ForkJoinPoolTest extends JSR166TestCase { |
30 |
|
public static void main(String[] args) { |
31 |
< |
junit.textui.TestRunner.run (suite()); |
31 |
> |
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
33 |
+ |
|
34 |
|
public static Test suite() { |
35 |
|
return new TestSuite(ForkJoinPoolTest.class); |
36 |
|
} |
37 |
|
|
38 |
< |
/** |
38 |
> |
/* |
39 |
|
* Testing coverage notes: |
40 |
|
* |
41 |
|
* 1. shutdown and related methods are tested via super.joinPool. |
54 |
|
// Some classes to test extension and factory methods |
55 |
|
|
56 |
|
static class MyHandler implements Thread.UncaughtExceptionHandler { |
57 |
< |
int catches = 0; |
57 |
> |
volatile int catches = 0; |
58 |
|
public void uncaughtException(Thread t, Throwable e) { |
59 |
|
++catches; |
60 |
|
} |
63 |
|
// to test handlers |
64 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
65 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
66 |
< |
protected void onStart() { throw new Error(); } |
66 |
> |
protected void onStart() { super.onStart(); throw new Error(); } |
67 |
|
} |
68 |
|
|
69 |
|
static class FailingThreadFactory |
70 |
|
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
71 |
< |
int calls = 0; |
71 |
> |
volatile int calls = 0; |
72 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
73 |
|
if (++calls > 1) return null; |
74 |
|
return new FailingFJWSubclass(p); |
103 |
|
static final class FibTask extends RecursiveTask<Integer> { |
104 |
|
final int number; |
105 |
|
FibTask(int n) { number = n; } |
106 |
< |
public Integer compute() { |
106 |
> |
protected Integer compute() { |
107 |
|
int n = number; |
108 |
|
if (n <= 1) |
109 |
|
return n; |
131 |
|
this.locker = locker; |
132 |
|
this.lock = lock; |
133 |
|
} |
134 |
< |
public Integer compute() { |
134 |
> |
protected Integer compute() { |
135 |
|
int n; |
136 |
|
LockingFibTask f1 = null; |
137 |
|
LockingFibTask f2 = null; |
157 |
|
* tasks, and quiescent running state. |
158 |
|
*/ |
159 |
|
public void testDefaultInitialState() { |
160 |
< |
ForkJoinPool p = null; |
160 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
161 |
|
try { |
162 |
< |
p = new ForkJoinPool(1); |
163 |
< |
assertTrue(p.getFactory() == |
149 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
150 |
< |
assertTrue(p.isQuiescent()); |
151 |
< |
assertTrue(p.getMaintainsParallelism()); |
162 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
163 |
> |
p.getFactory()); |
164 |
|
assertFalse(p.getAsyncMode()); |
165 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
166 |
< |
assertTrue(p.getStealCount() == 0); |
167 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
168 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
165 |
> |
assertEquals(0, p.getActiveThreadCount()); |
166 |
> |
assertEquals(0, p.getStealCount()); |
167 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
168 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
169 |
|
assertFalse(p.hasQueuedSubmissions()); |
170 |
|
assertFalse(p.isShutdown()); |
171 |
|
assertFalse(p.isTerminating()); |
190 |
|
*/ |
191 |
|
public void testConstructor2() { |
192 |
|
try { |
193 |
< |
new ForkJoinPool(1, null); |
193 |
> |
new ForkJoinPool(1, null, null, false); |
194 |
|
shouldThrow(); |
195 |
|
} catch (NullPointerException success) {} |
196 |
|
} |
197 |
|
|
186 |
– |
|
198 |
|
/** |
199 |
|
* getParallelism returns size set in constructor |
200 |
|
*/ |
201 |
|
public void testGetParallelism() { |
202 |
< |
ForkJoinPool p = null; |
192 |
< |
try { |
193 |
< |
p = new ForkJoinPool(1); |
194 |
< |
assertTrue(p.getParallelism() == 1); |
195 |
< |
} finally { |
196 |
< |
joinPool(p); |
197 |
< |
} |
198 |
< |
} |
199 |
< |
|
200 |
< |
/** |
201 |
< |
* setParallelism changes reported parallelism level. |
202 |
< |
*/ |
203 |
< |
public void testSetParallelism() { |
204 |
< |
ForkJoinPool p = null; |
205 |
< |
try { |
206 |
< |
p = new ForkJoinPool(1); |
207 |
< |
assertTrue(p.getParallelism() == 1); |
208 |
< |
p.setParallelism(2); |
209 |
< |
assertTrue(p.getParallelism() == 2); |
210 |
< |
} finally { |
211 |
< |
joinPool(p); |
212 |
< |
} |
213 |
< |
} |
214 |
< |
|
215 |
< |
/** |
216 |
< |
* setParallelism with argument <= 0 throws exception |
217 |
< |
*/ |
218 |
< |
public void testSetParallelism2() { |
219 |
< |
ForkJoinPool p = null; |
202 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
203 |
|
try { |
204 |
< |
p = new ForkJoinPool(1); |
222 |
< |
assertTrue(p.getParallelism() == 1); |
223 |
< |
p.setParallelism(-2); |
224 |
< |
shouldThrow(); |
225 |
< |
} catch (IllegalArgumentException success) { |
204 |
> |
assertEquals(1, p.getParallelism()); |
205 |
|
} finally { |
206 |
|
joinPool(p); |
207 |
|
} |
211 |
|
* getPoolSize returns number of started workers. |
212 |
|
*/ |
213 |
|
public void testGetPoolSize() { |
214 |
< |
ForkJoinPool p = null; |
214 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
215 |
|
try { |
216 |
< |
p = new ForkJoinPool(1); |
238 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
216 |
> |
assertEquals(0, p.getActiveThreadCount()); |
217 |
|
Future<String> future = p.submit(new StringTask()); |
218 |
< |
assertTrue(p.getPoolSize() == 1); |
241 |
< |
|
242 |
< |
} finally { |
243 |
< |
joinPool(p); |
244 |
< |
} |
245 |
< |
} |
246 |
< |
|
247 |
< |
/** |
248 |
< |
* setMaximumPoolSize changes size reported by getMaximumPoolSize. |
249 |
< |
*/ |
250 |
< |
public void testSetMaximumPoolSize() { |
251 |
< |
ForkJoinPool p = null; |
252 |
< |
try { |
253 |
< |
p = new ForkJoinPool(1); |
254 |
< |
p.setMaximumPoolSize(2); |
255 |
< |
assertTrue(p.getMaximumPoolSize() == 2); |
218 |
> |
assertEquals(1, p.getPoolSize()); |
219 |
|
} finally { |
220 |
|
joinPool(p); |
221 |
|
} |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* setMaximumPoolSize with argument <= 0 throws exception |
225 |
> |
* awaitTermination on a non-shutdown pool times out |
226 |
|
*/ |
227 |
< |
public void testSetMaximumPoolSize2() { |
228 |
< |
ForkJoinPool p = null; |
229 |
< |
try { |
230 |
< |
p = new ForkJoinPool(1); |
231 |
< |
p.setMaximumPoolSize(-2); |
232 |
< |
shouldThrow(); |
233 |
< |
} catch (IllegalArgumentException success) { |
234 |
< |
} finally { |
235 |
< |
joinPool(p); |
236 |
< |
} |
237 |
< |
} |
238 |
< |
|
239 |
< |
/** |
240 |
< |
* setMaintainsParallelism changes policy reported by |
241 |
< |
* getMaintainsParallelism. |
242 |
< |
*/ |
243 |
< |
public void testSetMaintainsParallelism() { |
244 |
< |
ForkJoinPool p = null; |
245 |
< |
try { |
246 |
< |
p = new ForkJoinPool(1); |
247 |
< |
p.setMaintainsParallelism(false); |
248 |
< |
assertFalse(p.getMaintainsParallelism()); |
286 |
< |
} finally { |
287 |
< |
joinPool(p); |
288 |
< |
} |
289 |
< |
} |
290 |
< |
|
291 |
< |
/** |
292 |
< |
* setAsyncMode changes policy reported by |
293 |
< |
* getAsyncMode. |
294 |
< |
*/ |
295 |
< |
public void testSetAsyncMode() { |
296 |
< |
ForkJoinPool p = null; |
297 |
< |
try { |
298 |
< |
p = new ForkJoinPool(1); |
299 |
< |
p.setAsyncMode(true); |
300 |
< |
assertTrue(p.getAsyncMode()); |
301 |
< |
} finally { |
302 |
< |
joinPool(p); |
303 |
< |
} |
227 |
> |
public void testAwaitTermination_timesOut() throws InterruptedException { |
228 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
229 |
> |
assertFalse(p.isTerminated()); |
230 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
231 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
232 |
> |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
233 |
> |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
234 |
> |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
235 |
> |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
236 |
> |
long timeoutNanos = 999999L; |
237 |
> |
long startTime = System.nanoTime(); |
238 |
> |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
239 |
> |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
240 |
> |
assertFalse(p.isTerminated()); |
241 |
> |
startTime = System.nanoTime(); |
242 |
> |
long timeoutMillis = timeoutMillis(); |
243 |
> |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
244 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
245 |
> |
assertFalse(p.isTerminated()); |
246 |
> |
p.shutdown(); |
247 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
248 |
> |
assertTrue(p.isTerminated()); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
255 |
|
* performs its defined action |
256 |
|
*/ |
257 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
258 |
< |
ForkJoinPool p = null; |
259 |
< |
try { |
260 |
< |
p = new ForkJoinPool(1, new FailingThreadFactory()); |
261 |
< |
MyHandler eh = new MyHandler(); |
262 |
< |
p.setUncaughtExceptionHandler(eh); |
263 |
< |
assertEquals(eh, p.getUncaughtExceptionHandler()); |
264 |
< |
p.execute(new FailingTask()); |
265 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
266 |
< |
assertTrue(eh.catches > 0); |
258 |
> |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
259 |
> |
final Thread.UncaughtExceptionHandler eh = |
260 |
> |
new Thread.UncaughtExceptionHandler() { |
261 |
> |
public void uncaughtException(Thread t, Throwable e) { |
262 |
> |
uehInvoked.countDown(); |
263 |
> |
}}; |
264 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
265 |
> |
eh, false); |
266 |
> |
try { |
267 |
> |
assertSame(eh, p.getUncaughtExceptionHandler()); |
268 |
> |
try { |
269 |
> |
p.execute(new FibTask(8)); |
270 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
271 |
> |
} catch (RejectedExecutionException ok) { |
272 |
> |
} |
273 |
|
} finally { |
274 |
< |
p.shutdownNow(); |
274 |
> |
p.shutdownNow(); // failure might have prevented processing task |
275 |
|
joinPool(p); |
276 |
|
} |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
< |
* setUncaughtExceptionHandler of null removes handler |
280 |
> |
* After invoking a single task, isQuiescent eventually becomes |
281 |
> |
* true, at which time queues are empty, threads are not active, |
282 |
> |
* the task has completed successfully, and construction |
283 |
> |
* parameters continue to hold |
284 |
|
*/ |
285 |
< |
public void testSetUncaughtExceptionHandler2() { |
286 |
< |
ForkJoinPool p = null; |
285 |
> |
public void testIsQuiescent() throws Exception { |
286 |
> |
ForkJoinPool p = new ForkJoinPool(2); |
287 |
|
try { |
288 |
< |
p = new ForkJoinPool(1); |
289 |
< |
p.setUncaughtExceptionHandler(null); |
290 |
< |
assertNull(p.getUncaughtExceptionHandler()); |
291 |
< |
} finally { |
292 |
< |
joinPool(p); |
293 |
< |
} |
294 |
< |
} |
295 |
< |
|
288 |
> |
assertTrue(p.isQuiescent()); |
289 |
> |
long startTime = System.nanoTime(); |
290 |
> |
FibTask f = new FibTask(20); |
291 |
> |
p.invoke(f); |
292 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
293 |
> |
p.getFactory()); |
294 |
> |
while (! p.isQuiescent()) { |
295 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
296 |
> |
throw new AssertionFailedError("timed out"); |
297 |
> |
assertFalse(p.getAsyncMode()); |
298 |
> |
assertFalse(p.isShutdown()); |
299 |
> |
assertFalse(p.isTerminating()); |
300 |
> |
assertFalse(p.isTerminated()); |
301 |
> |
Thread.yield(); |
302 |
> |
} |
303 |
|
|
343 |
– |
/** |
344 |
– |
* After invoking a single task, isQuiescent is true, |
345 |
– |
* queues are empty, threads are not active, and |
346 |
– |
* construction parameters continue to hold |
347 |
– |
*/ |
348 |
– |
public void testisQuiescent() throws InterruptedException { |
349 |
– |
ForkJoinPool p = null; |
350 |
– |
try { |
351 |
– |
p = new ForkJoinPool(2); |
352 |
– |
p.invoke(new FibTask(20)); |
353 |
– |
assertTrue(p.getFactory() == |
354 |
– |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
355 |
– |
Thread.sleep(MEDIUM_DELAY_MS); |
304 |
|
assertTrue(p.isQuiescent()); |
357 |
– |
assertTrue(p.getMaintainsParallelism()); |
305 |
|
assertFalse(p.getAsyncMode()); |
306 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
307 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
308 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
306 |
> |
assertEquals(0, p.getActiveThreadCount()); |
307 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
308 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
309 |
|
assertFalse(p.hasQueuedSubmissions()); |
310 |
|
assertFalse(p.isShutdown()); |
311 |
|
assertFalse(p.isTerminating()); |
312 |
|
assertFalse(p.isTerminated()); |
313 |
+ |
assertTrue(f.isDone()); |
314 |
+ |
assertEquals(6765, (int) f.get()); |
315 |
|
} finally { |
316 |
|
joinPool(p); |
317 |
|
} |
321 |
|
* Completed submit(ForkJoinTask) returns result |
322 |
|
*/ |
323 |
|
public void testSubmitForkJoinTask() throws Throwable { |
324 |
< |
ForkJoinPool p = null; |
324 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
325 |
|
try { |
377 |
– |
p = new ForkJoinPool(1); |
326 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
327 |
< |
int r = f.get(); |
380 |
< |
assertTrue(r == 21); |
327 |
> |
assertEquals(21, (int) f.get()); |
328 |
|
} finally { |
329 |
|
joinPool(p); |
330 |
|
} |
334 |
|
* A task submitted after shutdown is rejected |
335 |
|
*/ |
336 |
|
public void testSubmitAfterShutdown() { |
337 |
< |
ForkJoinPool p = null; |
337 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
338 |
|
try { |
392 |
– |
p = new ForkJoinPool(1); |
339 |
|
p.shutdown(); |
340 |
|
assertTrue(p.isShutdown()); |
341 |
< |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
342 |
< |
shouldThrow(); |
343 |
< |
} catch (RejectedExecutionException success) { |
341 |
> |
try { |
342 |
> |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
343 |
> |
shouldThrow(); |
344 |
> |
} catch (RejectedExecutionException success) {} |
345 |
|
} finally { |
346 |
|
joinPool(p); |
347 |
|
} |
351 |
|
* Pool maintains parallelism when using ManagedBlocker |
352 |
|
*/ |
353 |
|
public void testBlockingForkJoinTask() throws Throwable { |
354 |
< |
ForkJoinPool p = null; |
354 |
> |
ForkJoinPool p = new ForkJoinPool(4); |
355 |
|
try { |
409 |
– |
p = new ForkJoinPool(4); |
356 |
|
ReentrantLock lock = new ReentrantLock(); |
357 |
|
ManagedLocker locker = new ManagedLocker(lock); |
358 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
358 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
359 |
|
p.execute(f); |
360 |
< |
assertTrue(p.getPoolSize() >= 4); |
415 |
< |
int r = f.get(); |
416 |
< |
assertTrue(r == 832040); |
360 |
> |
assertEquals(6765, (int) f.get()); |
361 |
|
} finally { |
362 |
|
p.shutdownNow(); // don't wait out shutdown |
363 |
|
} |
367 |
|
* pollSubmission returns unexecuted submitted task, if present |
368 |
|
*/ |
369 |
|
public void testPollSubmission() { |
370 |
< |
SubFJP p = null; |
370 |
> |
final CountDownLatch done = new CountDownLatch(1); |
371 |
> |
SubFJP p = new SubFJP(); |
372 |
|
try { |
373 |
< |
p = new SubFJP(); |
374 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
375 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
431 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
373 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
374 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
375 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
376 |
|
ForkJoinTask r = p.pollSubmission(); |
377 |
|
assertTrue(r == a || r == b || r == c); |
378 |
|
assertFalse(r.isDone()); |
379 |
|
} finally { |
380 |
+ |
done.countDown(); |
381 |
|
joinPool(p); |
382 |
|
} |
383 |
|
} |
386 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
387 |
|
*/ |
388 |
|
public void testDrainTasksTo() { |
389 |
< |
SubFJP p = null; |
389 |
> |
final CountDownLatch done = new CountDownLatch(1); |
390 |
> |
SubFJP p = new SubFJP(); |
391 |
|
try { |
392 |
< |
p = new SubFJP(); |
393 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
394 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
449 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
392 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
393 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
394 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
395 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
396 |
|
p.drainTasksTo(al); |
397 |
|
assertTrue(al.size() > 0); |
400 |
|
assertFalse(r.isDone()); |
401 |
|
} |
402 |
|
} finally { |
403 |
+ |
done.countDown(); |
404 |
|
joinPool(p); |
405 |
|
} |
406 |
|
} |
407 |
|
|
462 |
– |
|
408 |
|
// FJ Versions of AbstractExecutorService tests |
409 |
|
|
410 |
|
/** |
412 |
|
*/ |
413 |
|
public void testExecuteRunnable() throws Throwable { |
414 |
|
ExecutorService e = new ForkJoinPool(1); |
415 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
416 |
< |
assertFalse(task.done); |
417 |
< |
Future<?> future = e.submit(task); |
418 |
< |
future.get(); |
419 |
< |
assertTrue(task.done); |
415 |
> |
try { |
416 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
417 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
418 |
> |
public void realRun() { |
419 |
> |
done.set(true); |
420 |
> |
}}); |
421 |
> |
assertNull(future.get()); |
422 |
> |
assertNull(future.get(0, MILLISECONDS)); |
423 |
> |
assertTrue(done.get()); |
424 |
> |
assertTrue(future.isDone()); |
425 |
> |
assertFalse(future.isCancelled()); |
426 |
> |
} finally { |
427 |
> |
joinPool(e); |
428 |
> |
} |
429 |
|
} |
430 |
|
|
477 |
– |
|
431 |
|
/** |
432 |
|
* Completed submit(callable) returns result |
433 |
|
*/ |
434 |
|
public void testSubmitCallable() throws Throwable { |
435 |
|
ExecutorService e = new ForkJoinPool(1); |
436 |
< |
Future<String> future = e.submit(new StringTask()); |
437 |
< |
String result = future.get(); |
438 |
< |
assertSame(TEST_STRING, result); |
436 |
> |
try { |
437 |
> |
Future<String> future = e.submit(new StringTask()); |
438 |
> |
assertSame(TEST_STRING, future.get()); |
439 |
> |
assertTrue(future.isDone()); |
440 |
> |
assertFalse(future.isCancelled()); |
441 |
> |
} finally { |
442 |
> |
joinPool(e); |
443 |
> |
} |
444 |
|
} |
445 |
|
|
446 |
|
/** |
448 |
|
*/ |
449 |
|
public void testSubmitRunnable() throws Throwable { |
450 |
|
ExecutorService e = new ForkJoinPool(1); |
451 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
452 |
< |
future.get(); |
453 |
< |
assertTrue(future.isDone()); |
451 |
> |
try { |
452 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
453 |
> |
assertNull(future.get()); |
454 |
> |
assertTrue(future.isDone()); |
455 |
> |
assertFalse(future.isCancelled()); |
456 |
> |
} finally { |
457 |
> |
joinPool(e); |
458 |
> |
} |
459 |
|
} |
460 |
|
|
461 |
|
/** |
463 |
|
*/ |
464 |
|
public void testSubmitRunnable2() throws Throwable { |
465 |
|
ExecutorService e = new ForkJoinPool(1); |
466 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
467 |
< |
String result = future.get(); |
468 |
< |
assertSame(TEST_STRING, result); |
466 |
> |
try { |
467 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
468 |
> |
assertSame(TEST_STRING, future.get()); |
469 |
> |
assertTrue(future.isDone()); |
470 |
> |
assertFalse(future.isCancelled()); |
471 |
> |
} finally { |
472 |
> |
joinPool(e); |
473 |
> |
} |
474 |
|
} |
475 |
|
|
476 |
|
/** |
477 |
|
* A submitted privileged action runs to completion |
478 |
|
*/ |
479 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
479 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
480 |
> |
final Callable callable = Executors.callable(new PrivilegedAction() { |
481 |
> |
public Object run() { return TEST_STRING; }}); |
482 |
|
Runnable r = new CheckedRunnable() { |
483 |
< |
public void realRun() throws Exception { |
484 |
< |
ExecutorService e = new ForkJoinPool(1); |
485 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
486 |
< |
public Object run() { |
487 |
< |
return TEST_STRING; |
488 |
< |
}})); |
489 |
< |
|
490 |
< |
Object result = future.get(); |
491 |
< |
assertSame(TEST_STRING, result); |
522 |
< |
}}; |
483 |
> |
public void realRun() throws Exception { |
484 |
> |
ExecutorService e = new ForkJoinPool(1); |
485 |
> |
try { |
486 |
> |
Future future = e.submit(callable); |
487 |
> |
assertSame(TEST_STRING, future.get()); |
488 |
> |
} finally { |
489 |
> |
joinPool(e); |
490 |
> |
} |
491 |
> |
}}; |
492 |
|
|
493 |
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
494 |
|
} |
496 |
|
/** |
497 |
|
* A submitted privileged exception action runs to completion |
498 |
|
*/ |
499 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
499 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
500 |
> |
final Callable callable = |
501 |
> |
Executors.callable(new PrivilegedExceptionAction() { |
502 |
> |
public Object run() { return TEST_STRING; }}); |
503 |
|
Runnable r = new CheckedRunnable() { |
504 |
< |
public void realRun() throws Exception { |
505 |
< |
ExecutorService e = new ForkJoinPool(1); |
506 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
507 |
< |
public Object run() { |
508 |
< |
return TEST_STRING; |
509 |
< |
}})); |
510 |
< |
|
511 |
< |
Object result = future.get(); |
512 |
< |
assertSame(TEST_STRING, result); |
541 |
< |
}}; |
504 |
> |
public void realRun() throws Exception { |
505 |
> |
ExecutorService e = new ForkJoinPool(1); |
506 |
> |
try { |
507 |
> |
Future future = e.submit(callable); |
508 |
> |
assertSame(TEST_STRING, future.get()); |
509 |
> |
} finally { |
510 |
> |
joinPool(e); |
511 |
> |
} |
512 |
> |
}}; |
513 |
|
|
514 |
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
515 |
|
} |
517 |
|
/** |
518 |
|
* A submitted failed privileged exception action reports exception |
519 |
|
*/ |
520 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
520 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
521 |
> |
final Callable callable = |
522 |
> |
Executors.callable(new PrivilegedExceptionAction() { |
523 |
> |
public Object run() { throw new IndexOutOfBoundsException(); }}); |
524 |
|
Runnable r = new CheckedRunnable() { |
525 |
< |
public void realRun() throws Exception { |
526 |
< |
ExecutorService e = new ForkJoinPool(1); |
527 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
528 |
< |
public Object run() throws Exception { |
555 |
< |
throw new IndexOutOfBoundsException(); |
556 |
< |
}})); |
557 |
< |
|
525 |
> |
public void realRun() throws Exception { |
526 |
> |
ExecutorService e = new ForkJoinPool(1); |
527 |
> |
try { |
528 |
> |
Future future = e.submit(callable); |
529 |
|
try { |
530 |
< |
Object result = future.get(); |
530 |
> |
future.get(); |
531 |
|
shouldThrow(); |
532 |
|
} catch (ExecutionException success) { |
533 |
|
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
534 |
< |
}}}; |
534 |
> |
} |
535 |
> |
} finally { |
536 |
> |
joinPool(e); |
537 |
> |
} |
538 |
> |
}}; |
539 |
|
|
540 |
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
541 |
|
} |
544 |
|
* execute(null runnable) throws NullPointerException |
545 |
|
*/ |
546 |
|
public void testExecuteNullRunnable() { |
547 |
+ |
ExecutorService e = new ForkJoinPool(1); |
548 |
|
try { |
549 |
< |
ExecutorService e = new ForkJoinPool(1); |
574 |
< |
TrackedShortRunnable task = null; |
575 |
< |
Future<?> future = e.submit(task); |
549 |
> |
Future<?> future = e.submit((Runnable) null); |
550 |
|
shouldThrow(); |
551 |
< |
} catch (NullPointerException success) {} |
551 |
> |
} catch (NullPointerException success) { |
552 |
> |
} finally { |
553 |
> |
joinPool(e); |
554 |
> |
} |
555 |
|
} |
556 |
|
|
580 |
– |
|
557 |
|
/** |
558 |
|
* submit(null callable) throws NullPointerException |
559 |
|
*/ |
560 |
|
public void testSubmitNullCallable() { |
561 |
+ |
ExecutorService e = new ForkJoinPool(1); |
562 |
|
try { |
563 |
< |
ExecutorService e = new ForkJoinPool(1); |
587 |
< |
StringTask t = null; |
588 |
< |
Future<String> future = e.submit(t); |
563 |
> |
Future<String> future = e.submit((Callable) null); |
564 |
|
shouldThrow(); |
565 |
< |
} catch (NullPointerException success) {} |
565 |
> |
} catch (NullPointerException success) { |
566 |
> |
} finally { |
567 |
> |
joinPool(e); |
568 |
> |
} |
569 |
|
} |
570 |
|
|
593 |
– |
|
571 |
|
/** |
572 |
< |
* Blocking on submit(callable) throws InterruptedException if |
596 |
< |
* caller interrupted. |
572 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
573 |
|
*/ |
574 |
|
public void testInterruptedSubmit() throws InterruptedException { |
575 |
< |
final ForkJoinPool p = new ForkJoinPool(1); |
576 |
< |
|
577 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
578 |
< |
public void realRun() throws Throwable { |
579 |
< |
p.submit(new CheckedCallable<Object>() { |
580 |
< |
public Object realCall() throws Throwable { |
581 |
< |
try { |
582 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
583 |
< |
} catch (InterruptedException ok) { |
584 |
< |
} |
585 |
< |
return null; |
586 |
< |
}}).get(); |
587 |
< |
}}); |
588 |
< |
|
589 |
< |
t.start(); |
590 |
< |
Thread.sleep(SHORT_DELAY_MS); |
591 |
< |
t.interrupt(); |
592 |
< |
t.join(); |
593 |
< |
p.shutdownNow(); |
594 |
< |
joinPool(p); |
575 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
576 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
577 |
> |
final ExecutorService p = new ForkJoinPool(1); |
578 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
579 |
> |
public Void realCall() throws InterruptedException { |
580 |
> |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
581 |
> |
return null; |
582 |
> |
}}; |
583 |
> |
try { |
584 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
585 |
> |
public void realRun() throws Exception { |
586 |
> |
Future<Void> future = p.submit(awaiter); |
587 |
> |
submitted.countDown(); |
588 |
> |
future.get(); |
589 |
> |
}}); |
590 |
> |
t.start(); |
591 |
> |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
592 |
> |
t.interrupt(); |
593 |
> |
t.join(); |
594 |
> |
} finally { |
595 |
> |
quittingTime.countDown(); |
596 |
> |
joinPool(p); |
597 |
> |
} |
598 |
|
} |
599 |
|
|
600 |
|
/** |
605 |
|
ForkJoinPool p = new ForkJoinPool(1); |
606 |
|
try { |
607 |
|
p.submit(new Callable() { |
608 |
< |
public Object call() { |
609 |
< |
int i = 5/0; |
631 |
< |
return Boolean.TRUE; |
632 |
< |
}}).get(); |
608 |
> |
public Object call() { throw new ArithmeticException(); }}) |
609 |
> |
.get(); |
610 |
|
shouldThrow(); |
611 |
|
} catch (ExecutionException success) { |
612 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
613 |
+ |
} finally { |
614 |
+ |
joinPool(p); |
615 |
|
} |
637 |
– |
|
638 |
– |
joinPool(p); |
616 |
|
} |
617 |
|
|
618 |
|
/** |
794 |
|
} |
795 |
|
} |
796 |
|
|
820 |
– |
|
797 |
|
/** |
798 |
|
* timed invokeAny(null) throws NullPointerException |
799 |
|
*/ |