Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good way to forcefully stop a Java thread?

I want to stop a Java thread immediately but when I try the thread always takes some time before stopping. Is there a good way to force a Java thread to stop instantly?

like image 299
Puru Avatar asked Mar 09 '11 05:03

Puru


People also ask

What will be the best way to stop a java thread?

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. A thread will also move to the dead state automatically when it reaches the end of its method.

Is it possible to kill a thread in java?

A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle.

How do you stop an infinite thread in java?

The only way to stop a thread asynchronously is the stop() method.

How do you gracefully terminate a thread?

To stop the thread, set stop to true . I think you must do it manually this way. After all, only the code running in the thread has any idea what is and isn't graceful. Note that you either need to use locking or make the field volatile to make sure the reading thread sees changes from the writing thread.


1 Answers

There is no good way to stop a thread instantly.

  • There is Thread.stop(), but it is dangerous and deprecated. Don't use it, unless:

    1. you fully understand the problems that make it dangerous,

    2. you have thoroughly analyzed your code and determined that the problems do not apply and / or the risks are acceptable, and

    3. you don't care that your application may end up stuck at a some version of Java if / when the method is removed1.

  • There was Thread.stop(Throwable) but it was equally dangerous2, deprecated and was removed3 in Java 11.

  • There is Thread.interrupt(), but there is no guarantee that the thread will stop quickly, or even stop at all. The behavior will depend on whether the application thread code has been designed to notice and correctly respond to interrupts. (See below.)

  • There is the approach of writing the thread to periodically check a flag; see this answer. (See below.)


The "check a flag" and interrupt() approaches are largely the same. In both cases, the thread that expects to be interrupted needs to regularly check; e.g. by calling interrupted() or isInterrupted() or by checking a flag.

The difference between the approaches is that that the interrupt() mechanism will work when the code is waiting for a notify(), join(), etcetera, or blocked in an I/O operation. Because of this, and because interrupt is an accepted application independent way of doing this, it is usually preferable to implementing an application specific flag mechanism.


1 - In the Java 18 prerelease javadocs, Thread.stop() is now shown as "flagged for removal".
2 - Possibly more so. If you did a Thread.stop(new IOException()) for example, some code further up the call stack could easily catch the exception and subvert your "stop".
3 - You can still use the old "stop with an exception" functionality by calling the private Thread.stop0(Throwable) method reflectively; see https://stackoverflow.com/a/67077495/139985. It would be better to avoid having to do that.

like image 74
Stephen C Avatar answered Oct 23 '22 21:10

Stephen C