Does the Thread still exist if the JVM crashes?
I was asked this question during an interview, does开发者_如何学JAVA anybody know?
If JVM exits as a result of crash the thread will not exist anymore. If crash is some kind of out of memory or similar error, it may or may not exist depending on specific circumstances.
If a thread is running the JVM has not crashed.
However, sometimes people incorrectly describe any exception being throw as a crash. If this is what the interviewer meant, I would say the thread is still running because the exception was caught and not rethrown.
If the process that contains the JVM is closed, all its threads are closed as well. This is taken care of at the OS level, when the OS cleans up all the memory allocated to a given process.
I mention the process-level closure to distinguish it from other weird, unknown states that the JVM might happen into that leaves it running, but in an unknown state (maybe it's not actually doing anything, for example).
Similarly, if you kill the process, all the child threads will close, regardless of their type (daemon / non-daemon).
Imagine you're the JVM. You walk out on to the road, not really looking where you are going and you have a fatal accident. An instant before the unfortunate happens, you had 30 things on your mind. Imagine those things on your mind as "threads"
When you're being swept up from the road, do you think you'll still be conscious and have all of those things on your mind?
An execution instance of the JVM is a process.
Threads are a part of a process.
If the process is dead, everything that was a part of it is dead.
So, when your JVM crashes or has been terminated forcefully, all the threads created by it stop executing as well.
Moreover, the JVM terminates automatically when the only threads running inside it are daemon threads.
精彩评论