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

I want to stop the Java thread immediately, but when I try to execute the thread, it takes some time before it stops. Is there a way to forcefully stop a Java thread?

+10
java multithreading
source share
5 answers

There is no good way to stop the flow instantly.

  • There is Thread.stop() , but it is dangerous, deprecated and removed in Java 11 . Do not use it if:

    1. You fully understand the problems

    2. You have carefully analyzed your code and determined that the problems are not applicable and / or the risks are acceptable, and

    3. you don’t care that your application gets stuck in Java 8 or earlier.

  • There is Thread.interrupt() , but there is no guarantee that the thread will stop quickly or even stop. The behavior will depend on whether application flow code has been developed to notice and properly respond to interrupts.

  • There is an approach to writing a thread to periodically check the flag, but if the flag is not checked often (accidentally or intentionally), then the stream will not stop quickly.


FWIW, the flag, and interrupt() approaches are largely the same. In both cases, a thread that is expecting an interrupt should be checked regularly; for example, calling interrupted() or isInterrupted() or checking the flag.

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

+17
source share

There is one good way to forcefully (and quickly) forcefully stop a thread:

 System.exit(0) 

Unfortunately, this has a side effect that kills all other threads.

+7
source share

Starting Thread cannot be stopped using Thread.Interrupt , only stopping or blocking threads can be stopped using Thread.Interrupt . But using a shared variable to signal that she should stop what she is doing. The thread should periodically check the variable (for example: use the while loop) and exit in an ordered manner.

 private boolean isExit= false; public void beforeExit() { isExit= true; } public void run() { while (!isExit) { } } 
+6
source share

Do not use Thread.stop under any circumstances - if you ask this question, you don’t understand the Java threading model to use it safely.

Details, for the curious, are here: Java Thread Primitive Deprecation

If you are well versed in Java concurrency, you will understand why you should never use it, and that even if it is completely safe in your own code, it makes your code infected and unusable by anyone else. Instead, you are likely to end up using a boolean variable trick or some other pattern to tell the thread when it should complete and then exit.

Do not use Thread.stop. Ever.

+2
source share

Your thread can be placed in a while loop and check a boolean variable each time. When you set the variable to false, the loop will end and the thread will also end. To terminate a thread before going through the current loop, you can use the break keyword.

This avoids the use of obsolete methods such as Thread.stop ().

 private boolean run = true; //Call this method to end your thread void stopRunning(){ run = false; } //This loop would go into the overided run() method while(run){ //Put your task here } 

The thread will complete the current task and then stop itself.

If you need to stop the thread before completing the task, you can check the run variable using if / else somewhere in the loop, and end the thread the same way as elsewhere.

 while(run){ //Your code here /*Be sure your not stopping the task at a point that will harm your program.*/ if(!run){break;} } 
+1
source share

All Articles