48 |
|
final Runnable task = new CheckedRunnable() { |
49 |
|
public void realRun() { done.countDown(); }}; |
50 |
|
p.execute(task); |
51 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
51 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
52 |
|
} |
53 |
|
} |
54 |
|
|
317 |
|
assertEquals(1, p.getActiveCount()); |
318 |
|
done.await(); |
319 |
|
}}); |
320 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
320 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
321 |
|
assertEquals(1, p.getActiveCount()); |
322 |
|
done.countDown(); |
323 |
|
} |
382 |
|
done.await(); |
383 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
384 |
|
}}); |
385 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
385 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
386 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
387 |
|
done.countDown(); |
388 |
|
} |
405 |
|
assertEquals(1, p.getPoolSize()); |
406 |
|
done.await(); |
407 |
|
}}); |
408 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
408 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
409 |
|
assertEquals(1, p.getPoolSize()); |
410 |
|
done.countDown(); |
411 |
|
} |
416 |
|
* submitted |
417 |
|
*/ |
418 |
|
public void testGetTaskCount() throws InterruptedException { |
419 |
+ |
final int TASKS = 3; |
420 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
421 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
422 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
422 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
423 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
422 |
– |
final CountDownLatch done = new CountDownLatch(1); |
423 |
– |
final int TASKS = 5; |
424 |
|
assertEquals(0, p.getTaskCount()); |
425 |
< |
for (int i = 0; i < TASKS; i++) |
425 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
426 |
> |
p.execute(new CheckedRunnable() { |
427 |
> |
public void realRun() throws InterruptedException { |
428 |
> |
threadStarted.countDown(); |
429 |
> |
done.await(); |
430 |
> |
}}); |
431 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
432 |
> |
assertEquals(1, p.getTaskCount()); |
433 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
434 |
> |
for (int i = 0; i < TASKS; i++) { |
435 |
> |
assertEquals(1 + i, p.getTaskCount()); |
436 |
|
p.execute(new CheckedRunnable() { |
437 |
|
public void realRun() throws InterruptedException { |
438 |
|
threadStarted.countDown(); |
439 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
440 |
|
done.await(); |
441 |
|
}}); |
442 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
443 |
< |
assertEquals(TASKS, p.getTaskCount()); |
444 |
< |
done.countDown(); |
442 |
> |
} |
443 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
444 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
445 |
|
} |
446 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
447 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
|
* getThreadFactory returns factory in constructor if not set |
452 |
|
*/ |
453 |
|
public void testGetThreadFactory() throws InterruptedException { |
454 |
< |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
455 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, threadFactory); |
456 |
< |
assertSame(threadFactory, p.getThreadFactory()); |
457 |
< |
joinPool(p); |
454 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
455 |
> |
final ScheduledThreadPoolExecutor p = |
456 |
> |
new ScheduledThreadPoolExecutor(1, threadFactory); |
457 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
458 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
459 |
> |
} |
460 |
|
} |
461 |
|
|
462 |
|
/** |
514 |
|
threadStarted.countDown(); |
515 |
|
done.await(); |
516 |
|
}}); |
517 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
517 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
518 |
|
assertFalse(p.isTerminating()); |
519 |
|
done.countDown(); |
520 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
538 |
|
threadStarted.countDown(); |
539 |
|
done.await(); |
540 |
|
}}); |
541 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
541 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
542 |
|
assertFalse(p.isTerminating()); |
543 |
|
done.countDown(); |
544 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
565 |
|
}}; |
566 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
567 |
|
} |
568 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
568 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
569 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
570 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
571 |
|
assertFalse(q.contains(tasks[0])); |
590 |
|
}}; |
591 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
592 |
|
} |
593 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
593 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
594 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
595 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
596 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
609 |
|
* purge eventually removes cancelled tasks from the queue |
610 |
|
*/ |
611 |
|
public void testPurge() throws InterruptedException { |
612 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
613 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
614 |
< |
for (int i = 0; i < tasks.length; i++) |
615 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
616 |
< |
LONG_DELAY_MS, MILLISECONDS); |
617 |
< |
try { |
612 |
> |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
613 |
> |
final Runnable releaser = new Runnable() { public void run() { |
614 |
> |
for (ScheduledFuture task : tasks) |
615 |
> |
if (task != null) task.cancel(true); }}; |
616 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
617 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
618 |
> |
for (int i = 0; i < tasks.length; i++) |
619 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
620 |
> |
LONG_DELAY_MS, MILLISECONDS); |
621 |
|
int max = tasks.length; |
622 |
|
if (tasks[4].cancel(true)) --max; |
623 |
|
if (tasks[3].cancel(true)) --max; |
629 |
|
long count = p.getTaskCount(); |
630 |
|
if (count == max) |
631 |
|
return; |
632 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
632 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
633 |
|
fail("Purge failed to remove cancelled tasks"); |
616 |
– |
} finally { |
617 |
– |
for (ScheduledFuture task : tasks) |
618 |
– |
task.cancel(true); |
619 |
– |
joinPool(p); |
634 |
|
} |
635 |
|
} |
636 |
|
|
644 |
|
final AtomicInteger ran = new AtomicInteger(0); |
645 |
|
final ScheduledThreadPoolExecutor p = |
646 |
|
new ScheduledThreadPoolExecutor(poolSize); |
647 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
647 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
648 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
649 |
|
threadsStarted.countDown(); |
650 |
|
try { |