Gee*_*eek 64 java java.util.concurrent
我想知道的基本区别shutdown()
,并shutdownNow()
为关闭Executor Service
?据我了解shutdown()
应该用于优美关机,这意味着人乳宁,并排队等待处理,但尚未开始应该可以完成所有任务,并shutdownNow()
做一个突然关机意味着一些未完成的任务被取消,未启动的任务也被取消.我还缺少哪些隐含/明确的东西?
PS:我发现了另一个问题,关于SO与此相关的,但不是我想知道到底是什么.
ass*_*ias 116
总之,你可以这样想:
shutdown()
只会告诉执行程序服务它不能接受新任务,但已经提交的任务继续运行shutdownNow()
将执行相同操作并将尝试通过中断相关线程来取消已提交的任务.请注意,如果您的任务忽略了中断,其shutdownNow
行为方式将完全相同shutdown
.你可以试试下面的例子中,更换shutdown
被shutdownNow
更好地理解执行的不同的路径:
shutdown
,输出是Still waiting after 100ms: calling System.exit(0)...
因为正在运行的任务没有中断并继续运行.shutdownNow
,输出interrupted
和Exiting normally...
由于正在运行的任务被中断,捕捉中断,然后停止它在做什么(打破while循环).shutdownNow
,如果在while循环中注释掉线,你会得到Still waiting after 100ms: calling System.exit(0)...
,因为引起中断不是由正在运行的任务不再处理.public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(new Runnable() {
@Override
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("interrupted");
break;
}
}
}
});
executor.shutdown();
if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) {
System.out.println("Still waiting after 100ms: calling System.exit(0)...");
System.exit(0);
}
System.out.println("Exiting normally...");
}
Run Code Online (Sandbox Code Playgroud)
shutdown()
:要终止ExecutorService内部的线程,请调用其shutdown()
方法。ExecutorService不会立即关闭,但是将不再接受新任务,并且一旦所有线程都完成了当前任务,ExecutorService就会关闭。调用shutdown()之前提交给ExecutorService的所有任务都将执行。
shutdownNow()
:如果要立即关闭ExecutorService,可以调用该shutdownNow()
方法。这将尝试立即停止所有正在执行的任务,并跳过所有已提交但未处理的任务。不能保证执行任务。也许他们停下来,也许执行到最后。这是尽力而为的尝试。
归档时间: |
|
查看次数: |
44570 次 |
最近记录: |