title: How to unconditionally stop a thread
body: <p>Occasionally we must forcibly stop a thread as a best effort before entirely shutting down the whole JVM. Usually <code>Thread#stop</code> is cited as a surefire, even if ham-handed and deprecated, way to unconditionally stop a thread. This is not so, however: all the rogue thread has to do to keep itself running is catch <code>ThreadDeath</code> or a superclass:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>This will print</p>
  
  <pre><code>InterruptedException. Still going on...
  InterruptedException. Still going on...
  ThreadDeath. Still going on...
  ThreadDeath. Still going on...
  </code></pre>
  
  <p>Is there anything else that I could do to really, <em>really</em> stop a thread without killing the whole JVM?</p>