113 |
|
public void realRun() throws InterruptedException { |
114 |
|
threadStarted.countDown(); |
115 |
|
assertEquals(1, p.getActiveCount()); |
116 |
< |
done.await(); |
116 |
> |
await(done); |
117 |
|
}}); |
118 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
118 |
> |
await(threadStarted); |
119 |
|
assertEquals(1, p.getActiveCount()); |
120 |
|
done.countDown(); |
121 |
|
} |
329 |
|
*/ |
330 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
331 |
|
final int THREADS = 3; |
332 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
333 |
|
final ThreadPoolExecutor p = |
334 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
335 |
|
LONG_DELAY_MS, MILLISECONDS, |
336 |
|
new ArrayBlockingQueue<Runnable>(10)); |
337 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
338 |
< |
final CountDownLatch done = new CountDownLatch(1); |
337 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
338 |
|
assertEquals(0, p.getLargestPoolSize()); |
339 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
340 |
|
for (int i = 0; i < THREADS; i++) |
341 |
|
p.execute(new CheckedRunnable() { |
342 |
|
public void realRun() throws InterruptedException { |
343 |
|
threadsStarted.countDown(); |
344 |
< |
done.await(); |
344 |
> |
await(done); |
345 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
346 |
|
}}); |
347 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
347 |
> |
await(threadsStarted); |
348 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
– |
done.countDown(); // release pool |
349 |
|
} |
350 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
351 |
|
} |
373 |
|
* become active |
374 |
|
*/ |
375 |
|
public void testGetPoolSize() throws InterruptedException { |
376 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
377 |
|
final ThreadPoolExecutor p = |
378 |
|
new ThreadPoolExecutor(1, 1, |
379 |
|
LONG_DELAY_MS, MILLISECONDS, |
380 |
|
new ArrayBlockingQueue<Runnable>(10)); |
381 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
382 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
< |
final CountDownLatch done = new CountDownLatch(1); |
381 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
382 |
|
assertEquals(0, p.getPoolSize()); |
383 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
384 |
|
p.execute(new CheckedRunnable() { |
385 |
|
public void realRun() throws InterruptedException { |
386 |
|
threadStarted.countDown(); |
387 |
|
assertEquals(1, p.getPoolSize()); |
388 |
< |
done.await(); |
388 |
> |
await(done); |
389 |
|
}}); |
390 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
390 |
> |
await(threadStarted); |
391 |
|
assertEquals(1, p.getPoolSize()); |
393 |
– |
done.countDown(); // release pool |
392 |
|
} |
393 |
|
} |
394 |
|
|
396 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
397 |
|
*/ |
398 |
|
public void testGetTaskCount() throws InterruptedException { |
399 |
+ |
final int TASKS = 3; |
400 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
401 |
|
final ThreadPoolExecutor p = |
402 |
|
new ThreadPoolExecutor(1, 1, |
403 |
|
LONG_DELAY_MS, MILLISECONDS, |
404 |
|
new ArrayBlockingQueue<Runnable>(10)); |
405 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
405 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
406 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
407 |
– |
final CountDownLatch done = new CountDownLatch(1); |
407 |
|
assertEquals(0, p.getTaskCount()); |
408 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
409 |
|
p.execute(new CheckedRunnable() { |
410 |
|
public void realRun() throws InterruptedException { |
411 |
|
threadStarted.countDown(); |
412 |
< |
assertEquals(1, p.getTaskCount()); |
413 |
< |
done.await(); |
412 |
> |
await(done); |
413 |
|
}}); |
414 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
414 |
> |
await(threadStarted); |
415 |
|
assertEquals(1, p.getTaskCount()); |
416 |
< |
done.countDown(); |
416 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
417 |
> |
for (int i = 0; i < TASKS; i++) { |
418 |
> |
assertEquals(1 + i, p.getTaskCount()); |
419 |
> |
p.execute(new CheckedRunnable() { |
420 |
> |
public void realRun() throws InterruptedException { |
421 |
> |
threadStarted.countDown(); |
422 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
423 |
> |
await(done); |
424 |
> |
}}); |
425 |
> |
} |
426 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
427 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
428 |
|
} |
429 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
430 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
431 |
|
} |
432 |
|
|
433 |
|
/** |
493 |
|
public void realRun() throws InterruptedException { |
494 |
|
assertFalse(p.isTerminating()); |
495 |
|
threadStarted.countDown(); |
496 |
< |
done.await(); |
496 |
> |
await(done); |
497 |
|
}}); |
498 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
498 |
> |
await(threadStarted); |
499 |
|
assertFalse(p.isTerminating()); |
500 |
|
done.countDown(); |
501 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
521 |
|
public void realRun() throws InterruptedException { |
522 |
|
assertFalse(p.isTerminating()); |
523 |
|
threadStarted.countDown(); |
524 |
< |
done.await(); |
524 |
> |
await(done); |
525 |
|
}}); |
526 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
526 |
> |
await(threadStarted); |
527 |
|
assertFalse(p.isTerminating()); |
528 |
|
done.countDown(); |
529 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
551 |
|
public Boolean realCall() throws InterruptedException { |
552 |
|
threadStarted.countDown(); |
553 |
|
assertSame(q, p.getQueue()); |
554 |
< |
done.await(); |
554 |
> |
await(done); |
555 |
|
return Boolean.TRUE; |
556 |
|
}}; |
557 |
|
tasks[i] = new FutureTask(task); |
558 |
|
p.execute(tasks[i]); |
559 |
|
} |
560 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
560 |
> |
await(threadStarted); |
561 |
|
assertSame(q, p.getQueue()); |
562 |
|
assertFalse(q.contains(tasks[0])); |
563 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
583 |
|
tasks[i] = new CheckedRunnable() { |
584 |
|
public void realRun() throws InterruptedException { |
585 |
|
threadStarted.countDown(); |
586 |
< |
done.await(); |
586 |
> |
await(done); |
587 |
|
}}; |
588 |
|
p.execute(tasks[i]); |
589 |
|
} |
590 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
590 |
> |
await(threadStarted); |
591 |
|
assertFalse(p.remove(tasks[0])); |
592 |
|
assertTrue(q.contains(tasks[4])); |
593 |
|
assertTrue(q.contains(tasks[3])); |
612 |
|
new ThreadPoolExecutor(1, 1, |
613 |
|
LONG_DELAY_MS, MILLISECONDS, |
614 |
|
q); |
615 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
615 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
616 |
|
FutureTask[] tasks = new FutureTask[5]; |
617 |
|
for (int i = 0; i < tasks.length; i++) { |
618 |
|
Callable task = new CheckedCallable<Boolean>() { |
619 |
|
public Boolean realCall() throws InterruptedException { |
620 |
|
threadStarted.countDown(); |
621 |
< |
done.await(); |
621 |
> |
await(done); |
622 |
|
return Boolean.TRUE; |
623 |
|
}}; |
624 |
|
tasks[i] = new FutureTask(task); |
625 |
|
p.execute(tasks[i]); |
626 |
|
} |
627 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
627 |
> |
await(threadStarted); |
628 |
|
assertEquals(tasks.length, p.getTaskCount()); |
629 |
|
assertEquals(tasks.length - 1, q.size()); |
630 |
|
assertEquals(1L, p.getActiveCount()); |
637 |
|
p.purge(); // Nothing to do |
638 |
|
assertEquals(tasks.length - 3, q.size()); |
639 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
628 |
– |
done.countDown(); |
640 |
|
} |
641 |
|
} |
642 |
|
|
652 |
|
new ThreadPoolExecutor(poolSize, poolSize, |
653 |
|
LONG_DELAY_MS, MILLISECONDS, |
654 |
|
new ArrayBlockingQueue<Runnable>(10)); |
655 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
655 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
656 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
657 |
|
threadsStarted.countDown(); |
658 |
|
try { |
662 |
|
}}; |
663 |
|
for (int i = 0; i < count; i++) |
664 |
|
p.execute(waiter); |
665 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
665 |
> |
await(threadsStarted); |
666 |
|
assertEquals(poolSize, p.getActiveCount()); |
667 |
|
assertEquals(0, p.getCompletedTaskCount()); |
668 |
|
final List<Runnable> queuedTasks; |
1024 |
|
* get of submitted callable throws InterruptedException if interrupted |
1025 |
|
*/ |
1026 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1027 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1028 |
|
final ThreadPoolExecutor p = |
1029 |
|
new ThreadPoolExecutor(1, 1, |
1030 |
|
60, SECONDS, |
1031 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1032 |
|
|
1033 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1033 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1034 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1023 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1035 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1036 |
|
public void realRun() throws Exception { |
1037 |
|
Callable task = new CheckedCallable<Boolean>() { |
1038 |
|
public Boolean realCall() throws InterruptedException { |
1039 |
|
threadStarted.countDown(); |
1040 |
< |
done.await(); |
1040 |
> |
await(done); |
1041 |
|
return Boolean.TRUE; |
1042 |
|
}}; |
1043 |
|
p.submit(task).get(); |
1044 |
|
}}); |
1045 |
|
|
1046 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
1046 |
> |
await(threadStarted); |
1047 |
|
t.interrupt(); |
1048 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1038 |
< |
done.countDown(); |
1048 |
> |
awaitTermination(t); |
1049 |
|
} |
1050 |
|
} |
1051 |
|
|
1061 |
|
final CountDownLatch done = new CountDownLatch(1); |
1062 |
|
Runnable task = new CheckedRunnable() { |
1063 |
|
public void realRun() throws InterruptedException { |
1064 |
< |
done.await(); |
1064 |
> |
await(done); |
1065 |
|
}}; |
1066 |
|
for (int i = 0; i < 2; ++i) |
1067 |
|
p.execute(task); |
1088 |
|
final CountDownLatch done = new CountDownLatch(1); |
1089 |
|
Runnable task = new CheckedRunnable() { |
1090 |
|
public void realRun() throws InterruptedException { |
1091 |
< |
done.await(); |
1091 |
> |
await(done); |
1092 |
|
}}; |
1093 |
|
for (int i = 0; i < 2; ++i) |
1094 |
|
p.submit(task); |
1115 |
|
final CountDownLatch done = new CountDownLatch(1); |
1116 |
|
Runnable task = new CheckedRunnable() { |
1117 |
|
public void realRun() throws InterruptedException { |
1118 |
< |
done.await(); |
1118 |
> |
await(done); |
1119 |
|
}}; |
1120 |
|
for (int i = 0; i < 2; ++i) |
1121 |
|
p.submit(Executors.callable(task)); |
1144 |
|
final CountDownLatch done = new CountDownLatch(1); |
1145 |
|
Runnable blocker = new CheckedRunnable() { |
1146 |
|
public void realRun() throws InterruptedException { |
1147 |
< |
done.await(); |
1147 |
> |
await(done); |
1148 |
|
}}; |
1149 |
|
p.execute(blocker); |
1150 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1843 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1844 |
|
l.add(new NPETask()); |
1845 |
|
List<Future<String>> futures = |
1846 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1846 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1847 |
|
assertEquals(1, futures.size()); |
1848 |
|
try { |
1849 |
|
futures.get(0).get(); |
2044 |
|
}}; |
2045 |
|
futures.add(e.submit(r)); |
2046 |
|
} |
2047 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2047 |
> |
await(blockerStarted); |
2048 |
|
for (Future<?> future : futures) future.cancel(false); |
2049 |
|
for (Future<?> future : futures) { |
2050 |
|
try { |