如果JVM崩溃了,线程还存在吗?
面试的时候被问到这个问题,有人知道吗?
I was asked this question during an interview, does anybody know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
面试的时候被问到这个问题,有人知道吗?
I was asked this question during an interview, does anybody know?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
如果 JVM 由于崩溃而退出,则该线程将不再存在。如果崩溃是某种内存不足或类似错误,则根据具体情况,它可能存在也可能不存在。
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.
如果线程正在运行,则 JVM 尚未崩溃。
然而,有时人们错误地将抛出的任何异常描述为崩溃。如果这就是面试官的意思,我会说线程仍在运行,因为异常被捕获并且没有重新抛出。
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.
如果包含 JVM 的进程关闭,则其所有线程也会关闭。当操作系统清理分配给给定进程的所有内存时,这是在操作系统级别处理的。
我提到进程级闭包是为了将其与 JVM 可能发生的其他奇怪的、未知的状态区分开来,使其保持运行,但处于未知状态(例如,也许它实际上没有执行任何操作)。
同样,如果您终止进程,所有子线程都将关闭,无论其类型如何(守护程序/非守护程序)。
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).
想象一下您是 JVM。您走上马路,没有真正注意要去哪里,结果发生了致命事故。在不幸发生的前一刻,你脑子里浮现了 30 件事。将你脑海中的这些事情想象成“线”
当你被从路上卷起来时,你认为你仍然有意识并且脑子里还有所有这些事情吗?
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?
JVM 的执行实例是一个进程。
线程是进程的一部分。
如果进程死了,那么它的一部分就死了。
因此,当您的 JVM 崩溃或被强制终止时,它创建的所有线程也会停止执行。
此外,当 JVM 中运行的唯一线程是守护线程时,它会自动终止。
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.