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

后端 未结 5 1265
清歌不尽
清歌不尽 2020-12-03 11:40

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?

5条回答
  •  感动是毒
    2020-12-03 11:59

    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.

提交回复
热议问题