Sometimes we need to forcefully stop the thread as the best effort before completely shutting down the entire JVM. Typically, Thread#stop quoted as true, even if ham-handed and deprecated, a way to unconditionally stop a thread. However, this is not the case: the entire rogue thread that must be executed in order to continue to work is catch ThreadDeath or a superclass:
public static void main(String[] args) throws InterruptedException { final Thread t = new Thread() { public void run() { for (;;) try { Thread.sleep(Long.MAX_VALUE); } catch (Throwable t) { System.out.println(t.getClass().getSimpleName() + ". Still going on..."); } }}; t.start(); Thread.sleep(200); t.interrupt(); Thread.sleep(200); t.interrupt(); Thread.sleep(200); t.stop(); Thread.sleep(200); t.stop(); }
Will open
InterruptedException. Still going on... InterruptedException. Still going on... ThreadDeath. Still going on... ThreadDeath. Still going on...
Is there anything else I could do to really stop the thread without killing the entire JVM?
java
Marko topolnik
source share