ExecutorService 对单个线程的时间限制
我有一个 ExecutorService 管理许多 Callables。 Callables 运行的任务主要是黑盒转换和数字运算。在某些情况下,正在转换的数据会振荡,线程将需要一个多小时才能完成。相比之下,大多数线程在一分钟内完成。
已确定来自长时间运行的线程的数据不相关。我想中断任何运行时间超过一定时间的线程。最好的方法是什么?
I have an ExecutorService managing a number of Callables. The tasks that the Callables run are mostly black box transformations and number crunching. Under certain conditions, the data being transformed will oscillate and the thread will take over an hour to finish. For comparison, most threads are completed within a minute.
It's been deteremined that the data from the long-running threads is not relevent. I would like to interrupt any thread that runs longer than a certain amount of time. What would the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 ScheduleExecutorService 在达到超时时将任务安排到
taskFuture.cancel(true)
长时间运行的任务。如果任务在此之前完成,则不会被取消。Use a ScheduleExecutorService to schedule a task to
taskFuture.cancel(true)
the long running task when the timeout is reached. If the task finishes before then it won't be cancelled.您可以像其他答案中那样取消未来等,但您需要确保“数字运算”的线程可以处理中断并优雅地终止。你说这是一个黑匣子操作 - 你有多确定线程的中断状态正在黑匣子内被主动检查?如果不是,则无法通过中断取消它。编写黑匣子时需要考虑到中断。
You could cancel the future etc as in the other answers, but you need to make sure that your threads which are "number crunching" can handle the interrupt and terminate gracefully. You say that this a black box operation - how certain are you that the interrupted status of the thread is being checked actively within the black box? If it isn't, you can't cancel it with an interrupt. The black box needs to be written with interruption in mind.
做到这一点的最佳方法是再引入一个 Executor。您可以使用 ScheduledExecutorService 取消所有长时间运行的任务,例如:
The best way for you to do this would be to introduce one more Executor. You can use a ScheduledExecutorService to cancel all long running tasks for example:
您可以获得相应 Futures 的列表(在您提交 Callable 时创建)及其启动时间。
然后,另一个任务可以每分钟检查是否有某个任务运行超过定义的时间,如果是,则在 Future 中调用 cancel(true) 。已完成的期货将从清单中删除。
You could get a list of your corresponding Futures (that are created when you submit a Callable) together with its startup time.
Another task could then check every minute if there are some task running for more than a defined time and if so, invoke cancel(true) in the Future. Done futures would be removed off the list.
您可以使用此方法
并将最大超时设置为一分钟。如果你的线程花费的时间超过这个时间,它就会被中止。
You can use this method
and set the maximum timeout to one minute. If your thread takes more than that it is just aborted.
问题是你不能杀死/停止/挂起java线程。
全部已弃用。
future.cancel
只会在工作线程之前考虑,因此如果线程被任何代码执行卡住并且没有考虑线程中断的逻辑,那么执行仍然会继续。
对于 executorService.shutdown(); 来说更是如此。
文档说。
因此,唯一的解决方案是记录这种情况并解决固有问题。
The problem is you can not kill/stop/suspend the java thread.
All are deprecated.
future.cancel
will only considered before worker threadSo if the thread is stuck with any code execution and doesn't have the logic to consider the thread interruption, then execution will still continue.
This is even true for
executorService.shutdown();
.The documentation says.
So the only solution is to log such a situation and fix the inherent issue.