473 |
|
* submitted |
474 |
|
*/ |
475 |
|
public void testGetTaskCount() throws InterruptedException { |
476 |
+ |
final int TASKS = 3; |
477 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
478 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
479 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
479 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
480 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
479 |
– |
final CountDownLatch done = new CountDownLatch(1); |
480 |
– |
final int TASKS = 5; |
481 |
|
assertEquals(0, p.getTaskCount()); |
482 |
< |
for (int i = 0; i < TASKS; i++) |
482 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
483 |
> |
p.execute(new CheckedRunnable() { |
484 |
> |
public void realRun() throws InterruptedException { |
485 |
> |
threadStarted.countDown(); |
486 |
> |
done.await(); |
487 |
> |
}}); |
488 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
489 |
> |
assertEquals(1, p.getTaskCount()); |
490 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
491 |
> |
for (int i = 0; i < TASKS; i++) { |
492 |
> |
assertEquals(1 + i, p.getTaskCount()); |
493 |
|
p.execute(new CheckedRunnable() { |
494 |
|
public void realRun() throws InterruptedException { |
495 |
|
threadStarted.countDown(); |
496 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
497 |
|
done.await(); |
498 |
|
}}); |
499 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
500 |
< |
assertEquals(TASKS, p.getTaskCount()); |
501 |
< |
done.countDown(); |
499 |
> |
} |
500 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
501 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
502 |
|
} |
503 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
504 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
662 |
|
* purge removes cancelled tasks from the queue |
663 |
|
*/ |
664 |
|
public void testPurge() throws InterruptedException { |
665 |
+ |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
666 |
+ |
final Runnable releaser = new Runnable() { public void run() { |
667 |
+ |
for (ScheduledFuture task : tasks) |
668 |
+ |
if (task != null) task.cancel(true); }}; |
669 |
|
final CustomExecutor p = new CustomExecutor(1); |
670 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
671 |
< |
for (int i = 0; i < tasks.length; i++) |
672 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
673 |
< |
LONG_DELAY_MS, MILLISECONDS); |
657 |
< |
try { |
670 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
671 |
> |
for (int i = 0; i < tasks.length; i++) |
672 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
673 |
> |
LONG_DELAY_MS, MILLISECONDS); |
674 |
|
int max = tasks.length; |
675 |
|
if (tasks[4].cancel(true)) --max; |
676 |
|
if (tasks[3].cancel(true)) --max; |
682 |
|
long count = p.getTaskCount(); |
683 |
|
if (count == max) |
684 |
|
return; |
685 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
685 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
686 |
|
fail("Purge failed to remove cancelled tasks"); |
671 |
– |
} finally { |
672 |
– |
for (ScheduledFuture task : tasks) |
673 |
– |
task.cancel(true); |
674 |
– |
joinPool(p); |
687 |
|
} |
688 |
|
} |
689 |
|
|
696 |
|
final int count = 5; |
697 |
|
final AtomicInteger ran = new AtomicInteger(0); |
698 |
|
final CustomExecutor p = new CustomExecutor(poolSize); |
699 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
699 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
700 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
701 |
|
threadsStarted.countDown(); |
702 |
|
try { |