views:

780

answers:

7

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.

A: 

What is the point of your test? Java offers no guarantees about what happens on an exit, apart from attempting to run shutdown hooks if the exit is a "clean" one

It sounds akin to trying to test your program's behaviour in the case it goes OutOfMemory; there's nothing you can do about it and no way of telling deterministically what will happen

oxbow_lakes
The point is to check transaction-handling. If the computer dies mid-thread, the transaction gets rolled back, and another computer (= service) should eventually pick up the jobs in the transaction.
ripper234
Have you written the transaction layer? Or are you using someone else's
oxbow_lakes
This sounds like it needs to be tested in an "integration" test - not a unit test. i.e. you really do exit the program at a point in the transaction
oxbow_lakes
It is an integration test, but that's just a different name. Effectively, it runs under the same suite, same Continuous Integration process, etc...
ripper234
Using someone else's transactional layer.
ripper234
A: 

Is there any reason you can't use Thread.suspend()? It will stop the thread so you can examine the state when the thread is interrupted.

You could also use Thread.stop() although that risks throwing multiple ThreadDeathExceptions. You can wrap it int try/catch/finally blocks, but there are no guarantees.

patros
+6  A: 

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?

gojomo
Not if I want to do it from an integration test.
ripper234
Well, if you use `stop()`, it could foul the process/heap so subsequent tests aren't clean and independent. You could have an integration test outside the JVM that kills and restarts the Java process. If half-done IO is a concern, you could possibly have an external process unmount drives or perform other nastiness. I think those may be the closest you can get to a reproduceable simulation of system death.
gojomo
So what if it's done in an integration test? Just start a new JVM for that test! (What's the issue?)
Arafangion
If a major fault that kills the machine/JVM is what you want to test, then a forcefull kill (kill -9 on *nix) is definitely what you want to use for the test. As @Arafangion mentioned that's easily done if you start a JVM for that specific test.
Joachim Sauer
I think starting a new JVM is overkill, but maybe that's just an irrational fear because I haven't done it yet.
ripper234
A: 

Thread stop() throws an error in the thread. ThreadDeath

The only way to simulate an application dying mid thread is to call System.exit().

However, this is pretty random so you have to perform the test many times to have any confidence you application behaves correctly no matter where it dies.

Peter Lawrey
+2  A: 

There is no portable method. You might try to call "kill -9" (or your local equivalent) on the whole java process, if you want to suppress the running of finalizers and shutdown hooks.

You won't get any kind of repeatable results out of such a test, but it might be interesting to perform such tests a few thousand times if your program is writing to the file system or a database and might leave inconsistent data structures when being killed.

mfx
+1  A: 

Or you could... kill the process. (ie, if this is Linux, send a kill -9 signal to the process).

Beware the race issues if you're trying to test something - if you hoping to crash badly - it might only do it once a month if you're particularly unlucky.

Arafangion
A: 

You may please refer to the following link, and do let me know if it really helped http://freejavaclass.com/detailpost.jsp?postid=49

athi