`

ExecutorService 的shutdown 与 shutdownNow

 
阅读更多

shutdown 对执行中的任务无影响,会执行等待队列中的所有任务。

shutdownNow 对执行中的任务进行Thread.interrupt(),不会执行等待队列中的任务,这些队列中的任务其实shutdownNow的返回参数。

 

shutdown和shutDownNow的共同之处在于立刻返回,不等待池中的任务。要等待池中的任务完成,要调用awaitTermination。

 

shutdown其实也可以做到与shutDownNow一样的效果。

我们往ExecutorService中submit任务,会得到一个Future。我们把这些future收集起来,放到一个List,那么以下代码

excutorService.shutdown();
for(Future f : futureList){
  f.cacel(true)
}

 与

excutorService.shutDownNow()

 等效。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics