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 |
|
import junit.framework.*; |
22 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
|
import java.util.concurrent.RecursiveTask; |
24 |
|
import java.util.concurrent.TimeUnit; |
25 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.locks.ReentrantLock; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
|
import java.security.AccessControlException; |
165 |
|
try { |
166 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
167 |
|
p.getFactory()); |
167 |
– |
assertTrue(p.isQuiescent()); |
168 |
|
assertFalse(p.getAsyncMode()); |
169 |
|
assertEquals(0, p.getActiveThreadCount()); |
170 |
|
assertEquals(0, p.getStealCount()); |
199 |
|
} catch (NullPointerException success) {} |
200 |
|
} |
201 |
|
|
202 |
– |
|
202 |
|
/** |
203 |
|
* getParallelism returns size set in constructor |
204 |
|
*/ |
251 |
|
} |
252 |
|
|
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 |
254 |
> |
* After invoking a single task, isQuiescent eventually becomes |
255 |
> |
* true, at which time queues are empty, threads are not active, |
256 |
> |
* the task has completed successfully, and construction |
257 |
> |
* parameters continue to hold |
258 |
|
*/ |
259 |
< |
public void testisQuiescent() throws InterruptedException { |
259 |
> |
public void testIsQuiescent() throws Exception { |
260 |
|
ForkJoinPool p = new ForkJoinPool(2); |
261 |
|
try { |
262 |
|
assertTrue(p.isQuiescent()); |
263 |
< |
p.invoke(new FibTask(20)); |
263 |
> |
long startTime = System.nanoTime(); |
264 |
> |
FibTask f = new FibTask(20); |
265 |
> |
p.invoke(f); |
266 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
267 |
|
p.getFactory()); |
268 |
< |
Thread.sleep(SMALL_DELAY_MS); |
268 |
> |
while (! p.isQuiescent()) { |
269 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
270 |
> |
throw new AssertionFailedError("timed out"); |
271 |
> |
assertFalse(p.getAsyncMode()); |
272 |
> |
assertFalse(p.isShutdown()); |
273 |
> |
assertFalse(p.isTerminating()); |
274 |
> |
assertFalse(p.isTerminated()); |
275 |
> |
Thread.yield(); |
276 |
> |
} |
277 |
> |
|
278 |
|
assertTrue(p.isQuiescent()); |
279 |
|
assertFalse(p.getAsyncMode()); |
280 |
|
assertEquals(0, p.getActiveThreadCount()); |
284 |
|
assertFalse(p.isShutdown()); |
285 |
|
assertFalse(p.isTerminating()); |
286 |
|
assertFalse(p.isTerminated()); |
287 |
+ |
assertTrue(f.isDone()); |
288 |
+ |
assertEquals(6765, (int) f.get()); |
289 |
|
} finally { |
290 |
|
joinPool(p); |
291 |
|
} |
341 |
|
* pollSubmission returns unexecuted submitted task, if present |
342 |
|
*/ |
343 |
|
public void testPollSubmission() { |
344 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
345 |
|
SubFJP p = new SubFJP(); |
346 |
|
try { |
347 |
< |
ForkJoinTask a = p.submit(new ShortRunnable()); |
348 |
< |
ForkJoinTask b = p.submit(new ShortRunnable()); |
349 |
< |
ForkJoinTask c = p.submit(new ShortRunnable()); |
347 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
348 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
349 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
350 |
|
ForkJoinTask r = p.pollSubmission(); |
351 |
|
assertTrue(r == a || r == b || r == c); |
352 |
|
assertFalse(r.isDone()); |
353 |
|
} finally { |
354 |
+ |
done.countDown(); |
355 |
|
joinPool(p); |
356 |
|
} |
357 |
|
} |
360 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
361 |
|
*/ |
362 |
|
public void testDrainTasksTo() { |
363 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
364 |
|
SubFJP p = new SubFJP(); |
365 |
|
try { |
366 |
< |
ForkJoinTask a = p.submit(new ShortRunnable()); |
367 |
< |
ForkJoinTask b = p.submit(new ShortRunnable()); |
368 |
< |
ForkJoinTask c = p.submit(new ShortRunnable()); |
366 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
367 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
368 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
369 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
370 |
|
p.drainTasksTo(al); |
371 |
|
assertTrue(al.size() > 0); |
374 |
|
assertFalse(r.isDone()); |
375 |
|
} |
376 |
|
} finally { |
377 |
+ |
done.countDown(); |
378 |
|
joinPool(p); |
379 |
|
} |
380 |
|
} |
381 |
|
|
365 |
– |
|
382 |
|
// FJ Versions of AbstractExecutorService tests |
383 |
|
|
384 |
|
/** |
387 |
|
public void testExecuteRunnable() throws Throwable { |
388 |
|
ExecutorService e = new ForkJoinPool(1); |
389 |
|
try { |
390 |
< |
TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); |
391 |
< |
assertFalse(task.isDone()); |
390 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
391 |
> |
CheckedRunnable task = new CheckedRunnable() { |
392 |
> |
public void realRun() { |
393 |
> |
done.set(true); |
394 |
> |
}}; |
395 |
|
Future<?> future = e.submit(task); |
396 |
|
assertNull(future.get()); |
397 |
< |
assertTrue(task.isDone()); |
397 |
> |
assertNull(future.get(0, MILLISECONDS)); |
398 |
> |
assertTrue(done.get()); |
399 |
> |
assertTrue(future.isDone()); |
400 |
|
assertFalse(future.isCancelled()); |
401 |
|
} finally { |
402 |
|
joinPool(e); |
403 |
|
} |
404 |
|
} |
405 |
|
|
385 |
– |
|
406 |
|
/** |
407 |
|
* Completed submit(callable) returns result |
408 |
|
*/ |
521 |
|
} |
522 |
|
} |
523 |
|
|
504 |
– |
|
524 |
|
/** |
525 |
|
* submit(null callable) throws NullPointerException |
526 |
|
*/ |
535 |
|
} |
536 |
|
} |
537 |
|
|
519 |
– |
|
538 |
|
/** |
539 |
|
* submit(callable).get() throws InterruptedException if interrupted |
540 |
|
*/ |
763 |
|
} |
764 |
|
} |
765 |
|
|
748 |
– |
|
766 |
|
/** |
767 |
|
* timed invokeAny(null) throws NullPointerException |
768 |
|
*/ |