115 |
|
assertEquals(1, p.getActiveCount()); |
116 |
|
done.await(); |
117 |
|
}}); |
118 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
118 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
119 |
|
assertEquals(1, p.getActiveCount()); |
120 |
|
done.countDown(); |
121 |
|
} |
344 |
|
done.await(); |
345 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
346 |
|
}}); |
347 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
347 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
348 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
|
done.countDown(); // release pool |
350 |
|
} |
388 |
|
assertEquals(1, p.getPoolSize()); |
389 |
|
done.await(); |
390 |
|
}}); |
391 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
391 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
392 |
|
assertEquals(1, p.getPoolSize()); |
393 |
|
done.countDown(); // release pool |
394 |
|
} |
398 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
399 |
|
*/ |
400 |
|
public void testGetTaskCount() throws InterruptedException { |
401 |
+ |
final int TASKS = 3; |
402 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
403 |
|
final ThreadPoolExecutor p = |
404 |
|
new ThreadPoolExecutor(1, 1, |
405 |
|
LONG_DELAY_MS, MILLISECONDS, |
406 |
|
new ArrayBlockingQueue<Runnable>(10)); |
407 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
407 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
408 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
407 |
– |
final CountDownLatch done = new CountDownLatch(1); |
409 |
|
assertEquals(0, p.getTaskCount()); |
410 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
411 |
|
p.execute(new CheckedRunnable() { |
412 |
|
public void realRun() throws InterruptedException { |
413 |
|
threadStarted.countDown(); |
412 |
– |
assertEquals(1, p.getTaskCount()); |
414 |
|
done.await(); |
415 |
|
}}); |
416 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
416 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
417 |
|
assertEquals(1, p.getTaskCount()); |
418 |
< |
done.countDown(); |
418 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
419 |
> |
for (int i = 0; i < TASKS; i++) { |
420 |
> |
assertEquals(1 + i, p.getTaskCount()); |
421 |
> |
p.execute(new CheckedRunnable() { |
422 |
> |
public void realRun() throws InterruptedException { |
423 |
> |
threadStarted.countDown(); |
424 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
425 |
> |
done.await(); |
426 |
> |
}}); |
427 |
> |
} |
428 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
429 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
430 |
|
} |
431 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
432 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
433 |
|
} |
434 |
|
|
435 |
|
/** |
497 |
|
threadStarted.countDown(); |
498 |
|
done.await(); |
499 |
|
}}); |
500 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
500 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
501 |
|
assertFalse(p.isTerminating()); |
502 |
|
done.countDown(); |
503 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
525 |
|
threadStarted.countDown(); |
526 |
|
done.await(); |
527 |
|
}}); |
528 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
528 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
529 |
|
assertFalse(p.isTerminating()); |
530 |
|
done.countDown(); |
531 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
559 |
|
tasks[i] = new FutureTask(task); |
560 |
|
p.execute(tasks[i]); |
561 |
|
} |
562 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
562 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
563 |
|
assertSame(q, p.getQueue()); |
564 |
|
assertFalse(q.contains(tasks[0])); |
565 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
589 |
|
}}; |
590 |
|
p.execute(tasks[i]); |
591 |
|
} |
592 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
592 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
593 |
|
assertFalse(p.remove(tasks[0])); |
594 |
|
assertTrue(q.contains(tasks[4])); |
595 |
|
assertTrue(q.contains(tasks[3])); |
614 |
|
new ThreadPoolExecutor(1, 1, |
615 |
|
LONG_DELAY_MS, MILLISECONDS, |
616 |
|
q); |
617 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
617 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
618 |
|
FutureTask[] tasks = new FutureTask[5]; |
619 |
|
for (int i = 0; i < tasks.length; i++) { |
620 |
|
Callable task = new CheckedCallable<Boolean>() { |
626 |
|
tasks[i] = new FutureTask(task); |
627 |
|
p.execute(tasks[i]); |
628 |
|
} |
629 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
629 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
630 |
|
assertEquals(tasks.length, p.getTaskCount()); |
631 |
|
assertEquals(tasks.length - 1, q.size()); |
632 |
|
assertEquals(1L, p.getActiveCount()); |
639 |
|
p.purge(); // Nothing to do |
640 |
|
assertEquals(tasks.length - 3, q.size()); |
641 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
628 |
– |
done.countDown(); |
642 |
|
} |
643 |
|
} |
644 |
|
|
1045 |
|
p.submit(task).get(); |
1046 |
|
}}); |
1047 |
|
|
1048 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
1048 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1049 |
|
t.interrupt(); |
1050 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1051 |
|
done.countDown(); |