ScheduledExecutorService:如何等待取消的任务完成?

ScheduledExecutorService: How can I wait for cancelled tasks to finish?

我在 ScheduledExecutorService 中安排了一些任务。关闭时,我想取消它们,并在它们全部完成后释放数据库锁。

如何等待取消的任务完成处理?

尝试过的解决方案

How can I wait for the cancelled tasks to finish processing?

问题是取消是在尽力而为的基础上完成的。 javadoc 状态

Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.

通常,取消是通过中断实现的,中断是一种惯例。没有什么能保证它会被正确实施。因此,即使您确实发送了 cancel,也无法保证底层任务(如果已经 运行)将完成取消。

没有可靠的方法来实现您的用例。