Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unconditionally stop a thread

Tags:

java

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?

like image 415
Marko Topolnik Avatar asked Jun 22 '13 14:06

Marko Topolnik


People also ask

How can we stop a thread in Java?

How can we stop a thread in Java? How can we stop a thread in Java? Whenever we want to stop a thread from running state by calling stop () method of Thread class in Java.This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected.

What happens when a thread reaches the end of its method?

A thread will also move to the dead state automatically when it reaches the end of its method. The stop () method is deprecated in Java due to thread-safety issues.

How to terminate a thread in C#?

At times, while working with a thread in C#, you might come upon a situation where you will need to terminate a thread. C# does provide methods for you to do that successfully, and this article aims to explain the process of terminating a thread in C#. For our goal of terminating a thread, we’ll utilize the Abort () method.

What is threading in Linux?

Threads are executed in their own system-level thread (e.g., a POSIX thread or Windows threads) that is fully managed by the host operating system. Once started, threads run independently until the target function returns.


2 Answers

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.

like image 106
Benjamin Gruenbaum Avatar answered Oct 13 '22 09:10

Benjamin Gruenbaum


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.

like image 43
wobblycogs Avatar answered Oct 13 '22 08:10

wobblycogs