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"); |
631 |
– |
} finally { |
632 |
– |
for (ScheduledFuture task : tasks) |
633 |
– |
task.cancel(true); |
634 |
– |
joinPool(p); |
634 |
|
} |
635 |
|
} |
636 |
|
|