Marko Topolnik
Marko Topolnik

Reputation: 200196

How to unconditionally stop a thread

Occasionally we must forcibly stop a thread as a best effort before entirely shutting down the whole JVM. Usually Thread#stop 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 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();
}

This will print

InterruptedException. Still going on...
InterruptedException. Still going on...
ThreadDeath. Still going on...
ThreadDeath. Still going on...

Is there anything else that I could do to really, really stop a thread without killing the whole JVM?

Upvotes: 9

Views: 1586

Answers (2)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276396

No. There is no built in simple way to really stop a thread.

Such a method, destroy, was planned but not implemented:

Deprecated. This method was originally designed to destroy this thread without any cleanup. Any monitors it held would have remained locked. However, the method was never implemented. If if were to be implemented, it would be deadlock-prone in much the manner of suspend(). If the target thread held a lock protecting a critical system resource when it was destroyed, no thread could ever access this resource again. If another thread ever attempted to lock this resource, deadlock would result. Such deadlocks typically manifest themselves as "frozen" processes.

Threads are not meant for that. They don't provide security. The other thread could just as well terminate the JVM itself - or spawn other problematic threads.

For more information, see Why are Thread.stop, Thread.suspend and Thread.resume are deprecated. You can read why here.

Upvotes: 6

wobblycogs
wobblycogs

Reputation: 4093

There is no way to guarantee that that thread can be stopped in Java. The most forceful way is Thread.stop but that's an accident waiting to happen. The alternatives are to use Thread.interrupt and having the thread check a flag but both of these rely on the thread being coded correctly and, in the case of the flag, checking it on a regular basis.

Personally, I would make sure I wasn't catching ThreadDeath. Stop is a poor way to stop a thread but at least you should get a notification as long as you aren't catching ThreadDeath.

Upvotes: 1

Related Questions