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

青春壹個敷衍的年華 提交于 2019-11-27 03:20:41

问题


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?


回答1:


There is no good way to stop a thread instantly.

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

    1. you fully understand the problems,

    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 will be stuck at Java 8 or earlier.

  • 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.

  • There is the approach of writing the thread to periodically check a flag, but if the flag is not checked frequently (by accident or by design), then the thread won't stop quickly.


FWIW, the 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() 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 an application specific flag mechanism.




回答2:


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

System.exit(0)

Unfortunately, this has the side effect of killing all other threads.




回答3:


Running Thread cannot be stopped using Thread.Interrupt , only waiting or blocking threads can be stopped using Thread.Interrupt.But using a shared variable to signal that it should stop what it is doing. The thread should check the variable periodically,(ex : use a while loop ) and exit in an orderly manner.

private boolean isExit= false;

public void beforeExit() {
    isExit= true;
}

public void run() {
    while (!isExit) {

    }
}



回答4:


Do not under any circumstances use Thread.stop - if you are asking this question then you do not understand the Java threading model enough to safely use it.

The details, for the curious, are here: Java Thread Primitive Deprecation

If you get really good at Java concurrency, then you will understand why you should never use it, and that even if it's entirely safe within your own code, it makes your code contaminated and unusable by anyone else. Instead you will probably end up use the boolean variable trick or some other pattern to tell a thread when it should finish up and then exit.

Do not use Thread.stop. Ever.




回答5:


Your thread could be put in a while loop and check a boolean variable each time through. When you set the variable to false the loop would end and so would the thread. To end the thread before going through the current loop you can use the break keyword.

This avoids using deprecated 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 finish the current task and then stop itself naturally.

If you need the thread to stop before finishing the task, you can check the run variable using an if/else anywhere in the loop and end the thread just like anywhere else.

while(run){
    //Your code here

    /*Be sure your not stopping the task at a point that will harm your program.*/
    if(!run){break;}
}


来源:https://stackoverflow.com/questions/5241822/is-there-a-good-way-to-forcefully-stop-a-java-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!