From the ScheduledThreadPoolExecutor
(the actual type returned by Executors.newScheduledThreadPool
) documentation:
When a submitted task is cancelled before it is run, execution is suppressed. By default, such a cancelled task is not automatically removed from the work queue until its delay elapses. While this enables further inspection and monitoring, it may also cause unbounded retention of cancelled tasks. To avoid this, set setRemoveOnCancelPolicy(boolean) to true, which causes tasks to be immediately removed from the work queue at time of cancellation.
This is a ScheduledThreadPoolExecutor
method though. I don't believe it's possible to solve this purely using the ScheduledExecutorService
interface.