美文网首页
Future 超时后是否会杀死线程?

Future 超时后是否会杀死线程?

作者: 第十人_孔 | 来源:发表于2018-02-27 19:16 被阅读0次

问题:

在使用ExecutorService 和 Future 对象时,如果设置为Future设置了一个超时时间,那么当Future超时时,对应的Thread是否会被删除?
boolean cancel(boolean mayInterruptIfRunning)

答案:

不会删除,甚至不会尝试终端Task。

该方法的解释如下:

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. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

解释:

如果Task已经开始,那么mayInterruptIfRunning 参数会在尝试停止任务时,决定该执行该任务的线程是否应该被中断。

参考:
Stack Overflow:Does a Future timeout kill the Thread execution
Java doc:Future

相关文章

网友评论

      本文标题:Future 超时后是否会杀死线程?

      本文链接:https://www.haomeiwen.com/subject/uhgmxftx.html