545 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
546 |
|
*/ |
547 |
|
public void testGetTaskCount() throws InterruptedException { |
548 |
+ |
final int TASKS = 3; |
549 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
550 |
|
final ThreadPoolExecutor p = |
551 |
|
new CustomTPE(1, 1, |
552 |
|
LONG_DELAY_MS, MILLISECONDS, |
553 |
|
new ArrayBlockingQueue<Runnable>(10)); |
554 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
554 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
555 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
– |
final CountDownLatch done = new CountDownLatch(1); |
556 |
|
assertEquals(0, p.getTaskCount()); |
557 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
558 |
|
p.execute(new CheckedRunnable() { |
559 |
|
public void realRun() throws InterruptedException { |
560 |
|
threadStarted.countDown(); |
559 |
– |
assertEquals(1, p.getTaskCount()); |
561 |
|
done.await(); |
562 |
|
}}); |
563 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
564 |
|
assertEquals(1, p.getTaskCount()); |
565 |
< |
done.countDown(); |
565 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
566 |
> |
for (int i = 0; i < TASKS; i++) { |
567 |
> |
assertEquals(1 + i, p.getTaskCount()); |
568 |
> |
p.execute(new CheckedRunnable() { |
569 |
> |
public void realRun() throws InterruptedException { |
570 |
> |
threadStarted.countDown(); |
571 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
572 |
> |
done.await(); |
573 |
> |
}}); |
574 |
> |
} |
575 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
576 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
577 |
|
} |
578 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
579 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
580 |
|
} |
581 |
|
|
582 |
|
/** |
729 |
|
new CustomTPE(1, 1, |
730 |
|
LONG_DELAY_MS, MILLISECONDS, |
731 |
|
q); |
732 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
732 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
733 |
|
FutureTask[] tasks = new FutureTask[5]; |
734 |
|
for (int i = 0; i < tasks.length; i++) { |
735 |
|
Callable task = new CheckedCallable<Boolean>() { |
741 |
|
tasks[i] = new FutureTask(task); |
742 |
|
p.execute(tasks[i]); |
743 |
|
} |
744 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
744 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
745 |
|
assertEquals(tasks.length, p.getTaskCount()); |
746 |
|
assertEquals(tasks.length - 1, q.size()); |
747 |
|
assertEquals(1L, p.getActiveCount()); |
754 |
|
p.purge(); // Nothing to do |
755 |
|
assertEquals(tasks.length - 3, q.size()); |
756 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
743 |
– |
done.countDown(); |
757 |
|
} |
758 |
|
} |
759 |
|
|
769 |
|
new CustomTPE(poolSize, poolSize, |
770 |
|
LONG_DELAY_MS, MILLISECONDS, |
771 |
|
new ArrayBlockingQueue<Runnable>(10)); |
772 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
772 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
773 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
774 |
|
threadsStarted.countDown(); |
775 |
|
try { |