Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing thread instantly in Java

Is it possible to kill a Java thread without raising an exception in it?

This is just for testing purposes - I want to simulate a case when the entire computer dies mid-thread.

Note - I saw a deprecated Thread.destroy() method, but the documentation says it never got implemented in the first place.

like image 532
ripper234 Avatar asked Aug 06 '26 16:08

ripper234


1 Answers

No. There is the deprecated, 'inherently unsafe' Thread.stop() method, but as its comment emphasizes, things could be left in an deeply corrupted state, and the ThreadDeath Error is still thrown inside the thread.

Sun's explanation of the problems with stop(), which can manifest long after it appears to work, is at:

http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

Wouldn't killing the JVM process (or yanking the plug) be a better simulation of computer death?

like image 105
gojomo Avatar answered Aug 08 '26 12:08

gojomo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!