374 |
|
assertEquals(1, p.getActiveCount()); |
375 |
|
done.await(); |
376 |
|
}}); |
377 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
377 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
378 |
|
assertEquals(1, p.getActiveCount()); |
379 |
|
done.countDown(); |
380 |
|
} |
439 |
|
done.await(); |
440 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
441 |
|
}}); |
442 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
442 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
443 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
444 |
|
done.countDown(); |
445 |
|
} |
462 |
|
assertEquals(1, p.getPoolSize()); |
463 |
|
done.await(); |
464 |
|
}}); |
465 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
465 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
466 |
|
assertEquals(1, p.getPoolSize()); |
467 |
|
done.countDown(); |
468 |
|
} |
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 |
|
/** |
567 |
|
threadStarted.countDown(); |
568 |
|
done.await(); |
569 |
|
}}); |
570 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
570 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
571 |
|
assertFalse(p.isTerminating()); |
572 |
|
done.countDown(); |
573 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
591 |
|
threadStarted.countDown(); |
592 |
|
done.await(); |
593 |
|
}}); |
594 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
594 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
595 |
|
assertFalse(p.isTerminating()); |
596 |
|
done.countDown(); |
597 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
618 |
|
}}; |
619 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
620 |
|
} |
621 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
621 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
622 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
623 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
624 |
|
assertFalse(q.contains(tasks[0])); |
643 |
|
}}; |
644 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
645 |
|
} |
646 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
646 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
647 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
648 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
649 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
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 { |