ExecutorService没有关闭 [英] ExecutorService is not shutting down

查看:176
本文介绍了ExecutorService没有关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

    ExecutorService es = Executors.newSingleThreadExecutor();
    es.submit(new Runnable() {
           @Override public void run() 
           {
                  while(true);

            }
   });

es.shutdownNow();

问题是我调用shutdownNow后ExecutorService没有关闭。文档说它试图阻止所有正在执行的任务。

The problem is that the ExecutorService doesn't shutdown after I call shutdownNow. Documentation says that it Attempts to stop all actively executing tasks.

那么为什么ES无法关闭?

So why is the ES failing to shutdown?

推荐答案

我做到了这一点并且有效:

I did this and it worked:

    ExecutorService es = Executors.newSingleThreadExecutor();
    es.submit(new Runnable() {
           @Override public void run() 
           {
                  while(!Thread.currentThread().isInterrupted());

            }
   });

   es.shutdownNow();

原因是 shutdownNow 没有终止线程。它只会中断所有正在运行的线程。

the reason is that shutdownNow doesn't terminate the thread. it just interrupts all running threads.

这篇关于ExecutorService没有关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆