要求 FutureTask 在取消之前启动
在我的可调用代码中,我使用信号通知向另一个线程通知多个结束行为。 Callable 对象与 Executor 中的 FutureTasks 一起排队。排队后也可能被取消。
现在,我的问题是,我至少依赖于启动的任务来使我的信号正常工作,但如果任务在有机会运行之前被标记为已取消,则执行器看起来会跳过该任务。
那么,有没有一种方法可以保证任务在运行时始终启动并始终取消(通过 InterruptedException)。
另外,您可以检查任务是否尚未开始但失败了?
In my Callable code I use signaling to notify multiple ending behaviours to another thread. The Callable objects are queued up with FutureTasks in an Executor. They may also be cancelled after being queued up.
Now, my problem is that I rely on the tasks atleast being started for my signaling to work, but it looks like the Executor just skips a task if it's been marked as canceled before it got a chance to run it.
So, is there a way to garantee that a task is always started, and always cancelled (by InterruptedException) while running.
Also, can you check if a task has not started but failed?
您可以子类化 FutureTask 类并覆盖其 done() 方法来执行信号。根据文档,即使任务已被取消,也应该调用此方法。
You can probably subclass FutureTask class and override its done() method to perform the signalling. According to the documentation, this method should be called even if the task has been cancelled.