I have the following code
ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(new Runnable() {
@Override public void run()
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.