How do you kill a java.lang.Thread
in Java?
I want to add several observations, based on the comments that have accumulated.
Thread.stop()
will stop a thread if the security manager allows it.Thread.stop()
is dangerous. Having said that, if you are working in a JEE environment and you have no control over the code being called, it may be necessary; see Why is Thread.stop deprecated?stop()
creates a new ThreadDeathError
error on the calling thread and then throws that error on the target thread. Therefore, the stack trace is generally worthless.stop()
checks with the security manager and then calls stop1()
that calls stop0()
. stop0()
is native code.Thread.stop()
has not been removed (yet), but Thread.stop(Throwable)
was removed in Java 11. (mailing list, JDK-8204243)