java - 挂在 threadpoolexecutor 中的可运行程序会发生什么?
如果一个可运行对象在线程池执行器中运行时挂起,有没有办法找出它已挂起并杀死该可运行对象? getActiveCount 方法是否会将挂起的可运行对象视为“正在主动执行”?
If a runnable hangs while running in a threadpoolexecutor, is there a way to find out that it has hung and kill the runnable? Will the getActiveCount method consider a runnable that's hanging as "actively executing"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有安全的方法来杀死繁忙的线程(除了在另一个进程中运行它并杀死它)您可以通过超时等待结果来检测线程是否花费了很长时间。您还可以添加一个任务以在超时后取消该任务,但这只会中断线程的任务,而不会杀死它。
您最好确定任务“挂起”的原因并修复代码,以免任务“挂起”。
当您启动任务时,您存储的 Thread.currentThread() 是一个共享变量。然后,您可以定期使用 getStackTrace() 来确定它正在做什么并记录下来。
There is no safe way to kill a thread which is busy (other than running it in another process and killing it) You can detect if a thread is taking to long by waiting for the result with a timeout. You can also add a task to cancel the task after a timeout, however this will only interrupt a thread's task, not kill it.
You are better off determining why the task "hangs" and fixing the code so it doesn't.
When you start a task you store Thread.currentThread() is a share variable. You can then take a getStackTrace() periodically to determine what it is doing and log it.