How to make a ScheduledExecutorService terminate automatically when its scheduled task is cancelled

帅比萌擦擦* 提交于 2019-12-05 14:24:20

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!