ExecutorService is not shutting down

前端 未结 5 500
独厮守ぢ
独厮守ぢ 2021-01-13 06:17

I have the following code

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


        
5条回答
  •  自闭症患者
    2021-01-13 06:49

    You can't shut down a thread that just refuses to terminate. It's just like you cannot force a thread to terminate, when it is running an infinite loop.

    From the Javadoc for shutdownNow(): "There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate."

    So unless your thread responds to an interrupt (see http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html), there's not much else shutdownNow() can do to terminate a thread.

提交回复
热议问题