105 |
|
final Runnable task = new CheckedRunnable() { |
106 |
|
public void realRun() { done.countDown(); }}; |
107 |
|
p.execute(task); |
108 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
108 |
> |
await(done); |
109 |
|
} |
110 |
|
} |
111 |
|
|
113 |
|
* delayed schedule of callable successfully executes after delay |
114 |
|
*/ |
115 |
|
public void testSchedule1() throws Exception { |
116 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
117 |
|
final CustomExecutor p = new CustomExecutor(1); |
118 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
118 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
119 |
|
final long startTime = System.nanoTime(); |
119 |
– |
final CountDownLatch done = new CountDownLatch(1); |
120 |
|
Callable task = new CheckedCallable<Boolean>() { |
121 |
|
public Boolean realCall() { |
122 |
|
done.countDown(); |
126 |
|
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
127 |
|
assertSame(Boolean.TRUE, f.get()); |
128 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
129 |
– |
assertTrue(done.await(0L, MILLISECONDS)); |
129 |
|
} |
130 |
|
} |
131 |
|
|
212 |
|
public void realRun() { done.countDown(); }}; |
213 |
|
ScheduledFuture h = |
214 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
215 |
< |
done.await(); |
215 |
> |
await(done); |
216 |
|
h.cancel(true); |
217 |
|
double normalizedTime = |
218 |
|
(double) millisElapsedSince(startTime) / delay; |
238 |
|
public void realRun() { done.countDown(); }}; |
239 |
|
ScheduledFuture h = |
240 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
241 |
< |
done.await(); |
241 |
> |
await(done); |
242 |
|
h.cancel(true); |
243 |
|
double normalizedTime = |
244 |
|
(double) millisElapsedSince(startTime) / delay; |
362 |
|
* thread becomes active |
363 |
|
*/ |
364 |
|
public void testGetActiveCount() throws InterruptedException { |
365 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
366 |
|
final ThreadPoolExecutor p = new CustomExecutor(2); |
367 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
367 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
368 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
369 |
– |
final CountDownLatch done = new CountDownLatch(1); |
369 |
|
assertEquals(0, p.getActiveCount()); |
370 |
|
p.execute(new CheckedRunnable() { |
371 |
|
public void realRun() throws InterruptedException { |
372 |
|
threadStarted.countDown(); |
373 |
|
assertEquals(1, p.getActiveCount()); |
374 |
< |
done.await(); |
374 |
> |
await(done); |
375 |
|
}}); |
376 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
376 |
> |
await(threadStarted); |
377 |
|
assertEquals(1, p.getActiveCount()); |
379 |
– |
done.countDown(); |
378 |
|
} |
379 |
|
} |
380 |
|
|
425 |
|
*/ |
426 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
427 |
|
final int THREADS = 3; |
428 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
429 |
|
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
430 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
430 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
431 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
433 |
– |
final CountDownLatch done = new CountDownLatch(1); |
432 |
|
assertEquals(0, p.getLargestPoolSize()); |
433 |
|
for (int i = 0; i < THREADS; i++) |
434 |
|
p.execute(new CheckedRunnable() { |
435 |
|
public void realRun() throws InterruptedException { |
436 |
|
threadsStarted.countDown(); |
437 |
< |
done.await(); |
437 |
> |
await(done); |
438 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
439 |
|
}}); |
440 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
440 |
> |
await(threadsStarted); |
441 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
444 |
– |
done.countDown(); |
442 |
|
} |
443 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
444 |
|
} |
448 |
|
* become active |
449 |
|
*/ |
450 |
|
public void testGetPoolSize() throws InterruptedException { |
451 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
452 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
453 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
453 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
454 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
457 |
– |
final CountDownLatch done = new CountDownLatch(1); |
455 |
|
assertEquals(0, p.getPoolSize()); |
456 |
|
p.execute(new CheckedRunnable() { |
457 |
|
public void realRun() throws InterruptedException { |
458 |
|
threadStarted.countDown(); |
459 |
|
assertEquals(1, p.getPoolSize()); |
460 |
< |
done.await(); |
460 |
> |
await(done); |
461 |
|
}}); |
462 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
462 |
> |
await(threadStarted); |
463 |
|
assertEquals(1, p.getPoolSize()); |
467 |
– |
done.countDown(); |
464 |
|
} |
465 |
|
} |
466 |
|
|
469 |
|
* submitted |
470 |
|
*/ |
471 |
|
public void testGetTaskCount() throws InterruptedException { |
472 |
+ |
final int TASKS = 3; |
473 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
474 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
475 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
475 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
476 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
479 |
– |
final CountDownLatch done = new CountDownLatch(1); |
480 |
– |
final int TASKS = 5; |
477 |
|
assertEquals(0, p.getTaskCount()); |
478 |
< |
for (int i = 0; i < TASKS; i++) |
478 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
479 |
> |
p.execute(new CheckedRunnable() { |
480 |
> |
public void realRun() throws InterruptedException { |
481 |
> |
threadStarted.countDown(); |
482 |
> |
await(done); |
483 |
> |
}}); |
484 |
> |
await(threadStarted); |
485 |
> |
assertEquals(1, p.getTaskCount()); |
486 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
487 |
> |
for (int i = 0; i < TASKS; i++) { |
488 |
> |
assertEquals(1 + i, p.getTaskCount()); |
489 |
|
p.execute(new CheckedRunnable() { |
490 |
|
public void realRun() throws InterruptedException { |
491 |
|
threadStarted.countDown(); |
492 |
< |
done.await(); |
492 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
493 |
> |
await(done); |
494 |
|
}}); |
495 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
496 |
< |
assertEquals(TASKS, p.getTaskCount()); |
497 |
< |
done.countDown(); |
495 |
> |
} |
496 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
497 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
498 |
|
} |
499 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
500 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
501 |
|
} |
502 |
|
|
503 |
|
/** |
552 |
|
* isTerminated is false before termination, true after |
553 |
|
*/ |
554 |
|
public void testIsTerminated() throws InterruptedException { |
555 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
556 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
557 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
558 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
549 |
– |
final CountDownLatch done = new CountDownLatch(1); |
550 |
– |
assertFalse(p.isTerminated()); |
559 |
|
p.execute(new CheckedRunnable() { |
560 |
|
public void realRun() throws InterruptedException { |
561 |
|
assertFalse(p.isTerminated()); |
562 |
|
threadStarted.countDown(); |
563 |
< |
done.await(); |
563 |
> |
await(done); |
564 |
|
}}); |
565 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
565 |
> |
await(threadStarted); |
566 |
> |
assertFalse(p.isTerminated()); |
567 |
|
assertFalse(p.isTerminating()); |
568 |
|
done.countDown(); |
569 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
576 |
|
* isTerminating is not true when running or when terminated |
577 |
|
*/ |
578 |
|
public void testIsTerminating() throws InterruptedException { |
579 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
580 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
581 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
582 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
573 |
– |
final CountDownLatch done = new CountDownLatch(1); |
583 |
|
assertFalse(p.isTerminating()); |
584 |
|
p.execute(new CheckedRunnable() { |
585 |
|
public void realRun() throws InterruptedException { |
586 |
|
assertFalse(p.isTerminating()); |
587 |
|
threadStarted.countDown(); |
588 |
< |
done.await(); |
588 |
> |
await(done); |
589 |
|
}}); |
590 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
590 |
> |
await(threadStarted); |
591 |
|
assertFalse(p.isTerminating()); |
592 |
|
done.countDown(); |
593 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
601 |
|
* getQueue returns the work queue, which contains queued tasks |
602 |
|
*/ |
603 |
|
public void testGetQueue() throws InterruptedException { |
604 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
605 |
|
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
606 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
606 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
607 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
598 |
– |
final CountDownLatch done = new CountDownLatch(1); |
608 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
609 |
|
for (int i = 0; i < tasks.length; i++) { |
610 |
|
Runnable r = new CheckedRunnable() { |
611 |
|
public void realRun() throws InterruptedException { |
612 |
|
threadStarted.countDown(); |
613 |
< |
done.await(); |
613 |
> |
await(done); |
614 |
|
}}; |
615 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
616 |
|
} |
617 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
617 |
> |
await(threadStarted); |
618 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
619 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
620 |
|
assertFalse(q.contains(tasks[0])); |
612 |
– |
done.countDown(); |
621 |
|
} |
622 |
|
} |
623 |
|
|
625 |
|
* remove(task) removes queued task, and fails to remove active task |
626 |
|
*/ |
627 |
|
public void testRemove() throws InterruptedException { |
628 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
629 |
|
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
630 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
630 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
631 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
632 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
624 |
– |
final CountDownLatch done = new CountDownLatch(1); |
633 |
|
for (int i = 0; i < tasks.length; i++) { |
634 |
|
Runnable r = new CheckedRunnable() { |
635 |
|
public void realRun() throws InterruptedException { |
636 |
|
threadStarted.countDown(); |
637 |
< |
done.await(); |
637 |
> |
await(done); |
638 |
|
}}; |
639 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
640 |
|
} |
641 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
641 |
> |
await(threadStarted); |
642 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
643 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
644 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
649 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
650 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
651 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
644 |
– |
done.countDown(); |
652 |
|
} |
653 |
|
} |
654 |
|
|
656 |
|
* purge removes cancelled tasks from the queue |
657 |
|
*/ |
658 |
|
public void testPurge() throws InterruptedException { |
659 |
+ |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
660 |
+ |
final Runnable releaser = new Runnable() { public void run() { |
661 |
+ |
for (ScheduledFuture task : tasks) |
662 |
+ |
if (task != null) task.cancel(true); }}; |
663 |
|
final CustomExecutor p = new CustomExecutor(1); |
664 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
665 |
< |
for (int i = 0; i < tasks.length; i++) |
666 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
667 |
< |
LONG_DELAY_MS, MILLISECONDS); |
657 |
< |
try { |
664 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
665 |
> |
for (int i = 0; i < tasks.length; i++) |
666 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
667 |
> |
LONG_DELAY_MS, MILLISECONDS); |
668 |
|
int max = tasks.length; |
669 |
|
if (tasks[4].cancel(true)) --max; |
670 |
|
if (tasks[3].cancel(true)) --max; |
676 |
|
long count = p.getTaskCount(); |
677 |
|
if (count == max) |
678 |
|
return; |
679 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
679 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
680 |
|
fail("Purge failed to remove cancelled tasks"); |
671 |
– |
} finally { |
672 |
– |
for (ScheduledFuture task : tasks) |
673 |
– |
task.cancel(true); |
674 |
– |
joinPool(p); |
681 |
|
} |
682 |
|
} |
683 |
|
|
690 |
|
final int count = 5; |
691 |
|
final AtomicInteger ran = new AtomicInteger(0); |
692 |
|
final CustomExecutor p = new CustomExecutor(poolSize); |
693 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
693 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
694 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
695 |
|
threadsStarted.countDown(); |
696 |
|
try { |
700 |
|
}}; |
701 |
|
for (int i = 0; i < count; i++) |
702 |
|
p.execute(waiter); |
703 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
703 |
> |
await(threadsStarted); |
704 |
|
assertEquals(poolSize, p.getActiveCount()); |
705 |
|
assertEquals(0, p.getCompletedTaskCount()); |
706 |
|
final List<Runnable> queuedTasks; |
1218 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1219 |
|
*/ |
1220 |
|
public void testTimedInvokeAll6() throws Exception { |
1221 |
< |
final ExecutorService e = new CustomExecutor(2); |
1222 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1223 |
< |
for (long timeout = timeoutMillis();;) { |
1221 |
> |
for (long timeout = timeoutMillis();;) { |
1222 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1223 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1224 |
> |
public String realCall() { |
1225 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1226 |
> |
catch (InterruptedException ok) {} |
1227 |
> |
return "1"; }}; |
1228 |
> |
final ExecutorService p = new CustomExecutor(2); |
1229 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1230 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1231 |
|
tasks.add(new StringTask("0")); |
1232 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1232 |
> |
tasks.add(waiter); |
1233 |
|
tasks.add(new StringTask("2")); |
1234 |
|
long startTime = System.nanoTime(); |
1235 |
|
List<Future<String>> futures = |
1236 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1236 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1237 |
|
assertEquals(tasks.size(), futures.size()); |
1238 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1239 |
|
for (Future future : futures) |