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.AbstractExecutorService; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.Callable; |
16 |
> |
import java.util.concurrent.Future; |
17 |
> |
import java.util.concurrent.ExecutionException; |
18 |
> |
import java.util.concurrent.CancellationException; |
19 |
> |
import java.util.concurrent.RejectedExecutionException; |
20 |
> |
import java.util.concurrent.ForkJoinPool; |
21 |
> |
import java.util.concurrent.ForkJoinTask; |
22 |
> |
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
> |
import java.util.concurrent.RecursiveTask; |
24 |
> |
import java.util.concurrent.TimeUnit; |
25 |
> |
import java.util.concurrent.locks.ReentrantLock; |
26 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
27 |
< |
import java.util.concurrent.locks.*; |
28 |
< |
import java.security.*; |
27 |
> |
import java.security.AccessControlException; |
28 |
> |
import java.security.Policy; |
29 |
> |
import java.security.PrivilegedAction; |
30 |
> |
import java.security.PrivilegedExceptionAction; |
31 |
|
|
32 |
|
public class ForkJoinPoolTest extends JSR166TestCase { |
33 |
|
public static void main(String[] args) { |
34 |
< |
junit.textui.TestRunner.run (suite()); |
34 |
> |
junit.textui.TestRunner.run(suite()); |
35 |
|
} |
36 |
+ |
|
37 |
|
public static Test suite() { |
38 |
|
return new TestSuite(ForkJoinPoolTest.class); |
39 |
|
} |
57 |
|
// Some classes to test extension and factory methods |
58 |
|
|
59 |
|
static class MyHandler implements Thread.UncaughtExceptionHandler { |
60 |
< |
int catches = 0; |
60 |
> |
volatile int catches = 0; |
61 |
|
public void uncaughtException(Thread t, Throwable e) { |
62 |
|
++catches; |
63 |
|
} |
66 |
|
// to test handlers |
67 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
68 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
69 |
< |
protected void onStart() { throw new Error(); } |
69 |
> |
protected void onStart() { super.onStart(); throw new Error(); } |
70 |
|
} |
71 |
|
|
72 |
|
static class FailingThreadFactory |
73 |
|
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
74 |
< |
int calls = 0; |
74 |
> |
volatile int calls = 0; |
75 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
76 |
|
if (++calls > 1) return null; |
77 |
|
return new FailingFJWSubclass(p); |
160 |
|
* tasks, and quiescent running state. |
161 |
|
*/ |
162 |
|
public void testDefaultInitialState() { |
163 |
< |
ForkJoinPool p = null; |
163 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
164 |
|
try { |
165 |
< |
p = new ForkJoinPool(1); |
166 |
< |
assertTrue(p.getFactory() == |
149 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
150 |
< |
assertTrue(p.isQuiescent()); |
151 |
< |
assertTrue(p.getMaintainsParallelism()); |
165 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
166 |
> |
p.getFactory()); |
167 |
|
assertFalse(p.getAsyncMode()); |
168 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
169 |
< |
assertTrue(p.getStealCount() == 0); |
170 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
171 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
168 |
> |
assertEquals(0, p.getActiveThreadCount()); |
169 |
> |
assertEquals(0, p.getStealCount()); |
170 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
171 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
172 |
|
assertFalse(p.hasQueuedSubmissions()); |
173 |
|
assertFalse(p.isShutdown()); |
174 |
|
assertFalse(p.isTerminating()); |
193 |
|
*/ |
194 |
|
public void testConstructor2() { |
195 |
|
try { |
196 |
< |
new ForkJoinPool(1, null); |
196 |
> |
new ForkJoinPool(1, null, null, false); |
197 |
|
shouldThrow(); |
198 |
|
} catch (NullPointerException success) {} |
199 |
|
} |
203 |
|
* getParallelism returns size set in constructor |
204 |
|
*/ |
205 |
|
public void testGetParallelism() { |
206 |
< |
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; |
206 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
207 |
|
try { |
208 |
< |
p = new ForkJoinPool(1); |
222 |
< |
assertTrue(p.getParallelism() == 1); |
223 |
< |
p.setParallelism(-2); |
224 |
< |
shouldThrow(); |
225 |
< |
} catch (IllegalArgumentException success) { |
208 |
> |
assertEquals(1, p.getParallelism()); |
209 |
|
} finally { |
210 |
|
joinPool(p); |
211 |
|
} |
215 |
|
* getPoolSize returns number of started workers. |
216 |
|
*/ |
217 |
|
public void testGetPoolSize() { |
218 |
< |
ForkJoinPool p = null; |
218 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
219 |
|
try { |
220 |
< |
p = new ForkJoinPool(1); |
238 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
220 |
> |
assertEquals(0, p.getActiveThreadCount()); |
221 |
|
Future<String> future = p.submit(new StringTask()); |
222 |
< |
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); |
256 |
< |
} finally { |
257 |
< |
joinPool(p); |
258 |
< |
} |
259 |
< |
} |
260 |
< |
|
261 |
< |
/** |
262 |
< |
* setMaximumPoolSize with argument <= 0 throws exception |
263 |
< |
*/ |
264 |
< |
public void testSetMaximumPoolSize2() { |
265 |
< |
ForkJoinPool p = null; |
266 |
< |
try { |
267 |
< |
p = new ForkJoinPool(1); |
268 |
< |
p.setMaximumPoolSize(-2); |
269 |
< |
shouldThrow(); |
270 |
< |
} catch (IllegalArgumentException success) { |
271 |
< |
} finally { |
272 |
< |
joinPool(p); |
273 |
< |
} |
274 |
< |
} |
275 |
< |
|
276 |
< |
/** |
277 |
< |
* setMaintainsParallelism changes policy reported by |
278 |
< |
* getMaintainsParallelism. |
279 |
< |
*/ |
280 |
< |
public void testSetMaintainsParallelism() { |
281 |
< |
ForkJoinPool p = null; |
282 |
< |
try { |
283 |
< |
p = new ForkJoinPool(1); |
284 |
< |
p.setMaintainsParallelism(false); |
285 |
< |
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()); |
222 |
> |
assertEquals(1, p.getPoolSize()); |
223 |
|
} finally { |
224 |
|
joinPool(p); |
225 |
|
} |
232 |
|
* performs its defined action |
233 |
|
*/ |
234 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
235 |
< |
ForkJoinPool p = null; |
236 |
< |
try { |
237 |
< |
p = new ForkJoinPool(1, new FailingThreadFactory()); |
238 |
< |
MyHandler eh = new MyHandler(); |
239 |
< |
p.setUncaughtExceptionHandler(eh); |
240 |
< |
assertEquals(eh, p.getUncaughtExceptionHandler()); |
241 |
< |
p.execute(new FailingTask()); |
242 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
243 |
< |
assertTrue(eh.catches > 0); |
244 |
< |
} finally { |
245 |
< |
p.shutdownNow(); |
246 |
< |
joinPool(p); |
325 |
< |
} |
326 |
< |
} |
327 |
< |
|
328 |
< |
/** |
329 |
< |
* setUncaughtExceptionHandler of null removes handler |
330 |
< |
*/ |
331 |
< |
public void testSetUncaughtExceptionHandler2() { |
332 |
< |
ForkJoinPool p = null; |
333 |
< |
try { |
334 |
< |
p = new ForkJoinPool(1); |
335 |
< |
p.setUncaughtExceptionHandler(null); |
336 |
< |
assertNull(p.getUncaughtExceptionHandler()); |
235 |
> |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
236 |
> |
final Thread.UncaughtExceptionHandler eh = |
237 |
> |
new Thread.UncaughtExceptionHandler() { |
238 |
> |
public void uncaughtException(Thread t, Throwable e) { |
239 |
> |
uehInvoked.countDown(); |
240 |
> |
}}; |
241 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
242 |
> |
eh, false); |
243 |
> |
try { |
244 |
> |
assertSame(eh, p.getUncaughtExceptionHandler()); |
245 |
> |
p.execute(new FibTask(8)); |
246 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
247 |
|
} finally { |
248 |
+ |
p.shutdownNow(); // failure might have prevented processing task |
249 |
|
joinPool(p); |
250 |
|
} |
251 |
|
} |
252 |
|
|
342 |
– |
|
253 |
|
/** |
254 |
|
* After invoking a single task, isQuiescent is true, |
255 |
|
* queues are empty, threads are not active, and |
256 |
|
* construction parameters continue to hold |
257 |
|
*/ |
258 |
|
public void testisQuiescent() throws InterruptedException { |
259 |
< |
ForkJoinPool p = null; |
259 |
> |
ForkJoinPool p = new ForkJoinPool(2); |
260 |
|
try { |
261 |
< |
p = new ForkJoinPool(2); |
261 |
> |
assertTrue(p.isQuiescent()); |
262 |
|
p.invoke(new FibTask(20)); |
263 |
< |
assertTrue(p.getFactory() == |
264 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
265 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
263 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
264 |
> |
p.getFactory()); |
265 |
> |
Thread.sleep(SMALL_DELAY_MS); |
266 |
|
assertTrue(p.isQuiescent()); |
357 |
– |
assertTrue(p.getMaintainsParallelism()); |
267 |
|
assertFalse(p.getAsyncMode()); |
268 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
269 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
270 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
268 |
> |
assertEquals(0, p.getActiveThreadCount()); |
269 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
270 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
271 |
|
assertFalse(p.hasQueuedSubmissions()); |
272 |
|
assertFalse(p.isShutdown()); |
273 |
|
assertFalse(p.isTerminating()); |
281 |
|
* Completed submit(ForkJoinTask) returns result |
282 |
|
*/ |
283 |
|
public void testSubmitForkJoinTask() throws Throwable { |
284 |
< |
ForkJoinPool p = null; |
284 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
285 |
|
try { |
377 |
– |
p = new ForkJoinPool(1); |
286 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
287 |
< |
int r = f.get(); |
380 |
< |
assertTrue(r == 21); |
287 |
> |
assertEquals(21, (int) f.get()); |
288 |
|
} finally { |
289 |
|
joinPool(p); |
290 |
|
} |
294 |
|
* A task submitted after shutdown is rejected |
295 |
|
*/ |
296 |
|
public void testSubmitAfterShutdown() { |
297 |
< |
ForkJoinPool p = null; |
297 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
298 |
|
try { |
392 |
– |
p = new ForkJoinPool(1); |
299 |
|
p.shutdown(); |
300 |
|
assertTrue(p.isShutdown()); |
301 |
< |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
302 |
< |
shouldThrow(); |
303 |
< |
} catch (RejectedExecutionException success) { |
301 |
> |
try { |
302 |
> |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
303 |
> |
shouldThrow(); |
304 |
> |
} catch (RejectedExecutionException success) {} |
305 |
|
} finally { |
306 |
|
joinPool(p); |
307 |
|
} |
311 |
|
* Pool maintains parallelism when using ManagedBlocker |
312 |
|
*/ |
313 |
|
public void testBlockingForkJoinTask() throws Throwable { |
314 |
< |
ForkJoinPool p = null; |
314 |
> |
ForkJoinPool p = new ForkJoinPool(4); |
315 |
|
try { |
409 |
– |
p = new ForkJoinPool(4); |
316 |
|
ReentrantLock lock = new ReentrantLock(); |
317 |
|
ManagedLocker locker = new ManagedLocker(lock); |
318 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
318 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
319 |
|
p.execute(f); |
320 |
< |
assertTrue(p.getPoolSize() >= 4); |
415 |
< |
int r = f.get(); |
416 |
< |
assertTrue(r == 832040); |
320 |
> |
assertEquals(6765, (int) f.get()); |
321 |
|
} finally { |
322 |
|
p.shutdownNow(); // don't wait out shutdown |
323 |
|
} |
327 |
|
* pollSubmission returns unexecuted submitted task, if present |
328 |
|
*/ |
329 |
|
public void testPollSubmission() { |
330 |
< |
SubFJP p = null; |
330 |
> |
final CountDownLatch done = new CountDownLatch(1); |
331 |
> |
SubFJP p = new SubFJP(); |
332 |
|
try { |
333 |
< |
p = new SubFJP(); |
334 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
335 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
431 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
333 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
334 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
335 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
336 |
|
ForkJoinTask r = p.pollSubmission(); |
337 |
|
assertTrue(r == a || r == b || r == c); |
338 |
|
assertFalse(r.isDone()); |
339 |
|
} finally { |
340 |
+ |
done.countDown(); |
341 |
|
joinPool(p); |
342 |
|
} |
343 |
|
} |
346 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
347 |
|
*/ |
348 |
|
public void testDrainTasksTo() { |
349 |
< |
SubFJP p = null; |
349 |
> |
final CountDownLatch done = new CountDownLatch(1); |
350 |
> |
SubFJP p = new SubFJP(); |
351 |
|
try { |
352 |
< |
p = new SubFJP(); |
353 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
354 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
449 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
352 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
353 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
354 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
355 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
356 |
|
p.drainTasksTo(al); |
357 |
|
assertTrue(al.size() > 0); |
360 |
|
assertFalse(r.isDone()); |
361 |
|
} |
362 |
|
} finally { |
363 |
+ |
done.countDown(); |
364 |
|
joinPool(p); |
365 |
|
} |
366 |
|
} |
373 |
|
*/ |
374 |
|
public void testExecuteRunnable() throws Throwable { |
375 |
|
ExecutorService e = new ForkJoinPool(1); |
376 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
377 |
< |
assertFalse(task.done); |
378 |
< |
Future<?> future = e.submit(task); |
379 |
< |
future.get(); |
380 |
< |
assertTrue(task.done); |
376 |
> |
try { |
377 |
> |
TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); |
378 |
> |
assertFalse(task.isDone()); |
379 |
> |
Future<?> future = e.submit(task); |
380 |
> |
assertNull(future.get()); |
381 |
> |
assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
382 |
> |
assertTrue(task.isDone()); |
383 |
> |
assertTrue(future.isDone()); |
384 |
> |
assertFalse(future.isCancelled()); |
385 |
> |
} finally { |
386 |
> |
joinPool(e); |
387 |
> |
} |
388 |
|
} |
389 |
|
|
390 |
|
|
393 |
|
*/ |
394 |
|
public void testSubmitCallable() throws Throwable { |
395 |
|
ExecutorService e = new ForkJoinPool(1); |
396 |
< |
Future<String> future = e.submit(new StringTask()); |
397 |
< |
String result = future.get(); |
398 |
< |
assertSame(TEST_STRING, result); |
396 |
> |
try { |
397 |
> |
Future<String> future = e.submit(new StringTask()); |
398 |
> |
assertSame(TEST_STRING, future.get()); |
399 |
> |
assertTrue(future.isDone()); |
400 |
> |
assertFalse(future.isCancelled()); |
401 |
> |
} finally { |
402 |
> |
joinPool(e); |
403 |
> |
} |
404 |
|
} |
405 |
|
|
406 |
|
/** |
408 |
|
*/ |
409 |
|
public void testSubmitRunnable() throws Throwable { |
410 |
|
ExecutorService e = new ForkJoinPool(1); |
411 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
412 |
< |
future.get(); |
413 |
< |
assertTrue(future.isDone()); |
411 |
> |
try { |
412 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
413 |
> |
assertNull(future.get()); |
414 |
> |
assertTrue(future.isDone()); |
415 |
> |
assertFalse(future.isCancelled()); |
416 |
> |
} finally { |
417 |
> |
joinPool(e); |
418 |
> |
} |
419 |
|
} |
420 |
|
|
421 |
|
/** |
423 |
|
*/ |
424 |
|
public void testSubmitRunnable2() throws Throwable { |
425 |
|
ExecutorService e = new ForkJoinPool(1); |
426 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
427 |
< |
String result = future.get(); |
428 |
< |
assertSame(TEST_STRING, result); |
426 |
> |
try { |
427 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
428 |
> |
assertSame(TEST_STRING, future.get()); |
429 |
> |
assertTrue(future.isDone()); |
430 |
> |
assertFalse(future.isCancelled()); |
431 |
> |
} finally { |
432 |
> |
joinPool(e); |
433 |
> |
} |
434 |
|
} |
435 |
|
|
436 |
|
/** |
437 |
|
* A submitted privileged action runs to completion |
438 |
|
*/ |
439 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
439 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
440 |
|
Runnable r = new CheckedRunnable() { |
441 |
|
public void realRun() throws Exception { |
442 |
|
ExecutorService e = new ForkJoinPool(1); |
445 |
|
return TEST_STRING; |
446 |
|
}})); |
447 |
|
|
448 |
< |
Object result = future.get(); |
521 |
< |
assertSame(TEST_STRING, result); |
448 |
> |
assertSame(TEST_STRING, future.get()); |
449 |
|
}}; |
450 |
|
|
451 |
< |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
451 |
> |
runWithPermissions(r, |
452 |
> |
new RuntimePermission("modifyThread")); |
453 |
|
} |
454 |
|
|
455 |
|
/** |
456 |
|
* A submitted privileged exception action runs to completion |
457 |
|
*/ |
458 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
458 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
459 |
|
Runnable r = new CheckedRunnable() { |
460 |
|
public void realRun() throws Exception { |
461 |
|
ExecutorService e = new ForkJoinPool(1); |
464 |
|
return TEST_STRING; |
465 |
|
}})); |
466 |
|
|
467 |
< |
Object result = future.get(); |
540 |
< |
assertSame(TEST_STRING, result); |
467 |
> |
assertSame(TEST_STRING, future.get()); |
468 |
|
}}; |
469 |
|
|
470 |
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
473 |
|
/** |
474 |
|
* A submitted failed privileged exception action reports exception |
475 |
|
*/ |
476 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
476 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
477 |
|
Runnable r = new CheckedRunnable() { |
478 |
|
public void realRun() throws Exception { |
479 |
|
ExecutorService e = new ForkJoinPool(1); |
483 |
|
}})); |
484 |
|
|
485 |
|
try { |
486 |
< |
Object result = future.get(); |
486 |
> |
future.get(); |
487 |
|
shouldThrow(); |
488 |
|
} catch (ExecutionException success) { |
489 |
|
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
496 |
|
* execute(null runnable) throws NullPointerException |
497 |
|
*/ |
498 |
|
public void testExecuteNullRunnable() { |
499 |
+ |
ExecutorService e = new ForkJoinPool(1); |
500 |
|
try { |
501 |
< |
ExecutorService e = new ForkJoinPool(1); |
574 |
< |
TrackedShortRunnable task = null; |
575 |
< |
Future<?> future = e.submit(task); |
501 |
> |
Future<?> future = e.submit((Runnable) null); |
502 |
|
shouldThrow(); |
503 |
< |
} catch (NullPointerException success) {} |
503 |
> |
} catch (NullPointerException success) { |
504 |
> |
} finally { |
505 |
> |
joinPool(e); |
506 |
> |
} |
507 |
|
} |
508 |
|
|
509 |
|
|
511 |
|
* submit(null callable) throws NullPointerException |
512 |
|
*/ |
513 |
|
public void testSubmitNullCallable() { |
514 |
+ |
ExecutorService e = new ForkJoinPool(1); |
515 |
|
try { |
516 |
< |
ExecutorService e = new ForkJoinPool(1); |
587 |
< |
StringTask t = null; |
588 |
< |
Future<String> future = e.submit(t); |
516 |
> |
Future<String> future = e.submit((Callable) null); |
517 |
|
shouldThrow(); |
518 |
< |
} catch (NullPointerException success) {} |
518 |
> |
} catch (NullPointerException success) { |
519 |
> |
} finally { |
520 |
> |
joinPool(e); |
521 |
> |
} |
522 |
|
} |
523 |
|
|
524 |
|
|
525 |
|
/** |
526 |
< |
* Blocking on submit(callable) throws InterruptedException if |
596 |
< |
* caller interrupted. |
526 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
527 |
|
*/ |
528 |
|
public void testInterruptedSubmit() throws InterruptedException { |
529 |
< |
final ForkJoinPool p = new ForkJoinPool(1); |
530 |
< |
|
531 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
532 |
< |
public void realRun() throws Throwable { |
533 |
< |
p.submit(new CheckedCallable<Object>() { |
534 |
< |
public Object realCall() throws Throwable { |
535 |
< |
try { |
536 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
537 |
< |
} catch (InterruptedException ok) { |
538 |
< |
} |
539 |
< |
return null; |
540 |
< |
}}).get(); |
541 |
< |
}}); |
542 |
< |
|
543 |
< |
t.start(); |
544 |
< |
Thread.sleep(SHORT_DELAY_MS); |
545 |
< |
t.interrupt(); |
546 |
< |
t.join(); |
547 |
< |
p.shutdownNow(); |
548 |
< |
joinPool(p); |
529 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
530 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
531 |
> |
final ExecutorService p = new ForkJoinPool(1); |
532 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
533 |
> |
public Void realCall() throws InterruptedException { |
534 |
> |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
535 |
> |
return null; |
536 |
> |
}}; |
537 |
> |
try { |
538 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
539 |
> |
public void realRun() throws Exception { |
540 |
> |
Future<Void> future = p.submit(awaiter); |
541 |
> |
submitted.countDown(); |
542 |
> |
future.get(); |
543 |
> |
}}); |
544 |
> |
t.start(); |
545 |
> |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
546 |
> |
t.interrupt(); |
547 |
> |
t.join(); |
548 |
> |
} finally { |
549 |
> |
quittingTime.countDown(); |
550 |
> |
joinPool(p); |
551 |
> |
} |
552 |
|
} |
553 |
|
|
554 |
|
/** |
566 |
|
shouldThrow(); |
567 |
|
} catch (ExecutionException success) { |
568 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
569 |
+ |
} finally { |
570 |
+ |
joinPool(p); |
571 |
|
} |
637 |
– |
|
638 |
– |
joinPool(p); |
572 |
|
} |
573 |
|
|
574 |
|
/** |