JVM 运行 多线程如何处理 ctrl-c、w/ 和 w/o 关闭挂钩?
How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks?
无法在线找到此答案。当按下 Ctrl+C 时:
- 当我们没有任何关闭挂钩时,运行ning 线程会发生什么情况 - 它们是否都遇到 InterruptedException?
- 当我们有关闭挂钩时,我知道关闭挂钩会以任意顺序在新线程中获得 运行。但是现有的 运行ning 线程会怎样?他们仍然每个人都遇到 InterruptedException 吗?
谢谢!
经典书籍“Java Concurrency in Practice”有一章(7.4)是关于JVM关闭的,你应该读一下,但这里有一些相关的引用:
If any application threads (daemon or nondaemon) are still running at shutdown time, they continue to run concurrently with the shutdown process.
The JVM makes no attempt to stop or interrupt any application threads that are still running at shutdown time; they are abruptly terminated when the JVM eventually halts.
因此线程不会被中断,但如果您愿意,您可以从关闭挂钩中显式中断它们。
IMO,守护进程线程将在关闭过程中继续 运行 并且 JVM 将在稍后退出应用程序时杀死所有 运行ning 线程。我不认为 运行ning 线程会得到 InterruptedException
,因为 JVM 不会做任何额外的努力来停止 运行ning 线程。
http://www.tutorialspoint.com/java/lang/runtime_addshutdownhook.htm
无法在线找到此答案。当按下 Ctrl+C 时:
- 当我们没有任何关闭挂钩时,运行ning 线程会发生什么情况 - 它们是否都遇到 InterruptedException?
- 当我们有关闭挂钩时,我知道关闭挂钩会以任意顺序在新线程中获得 运行。但是现有的 运行ning 线程会怎样?他们仍然每个人都遇到 InterruptedException 吗?
谢谢!
经典书籍“Java Concurrency in Practice”有一章(7.4)是关于JVM关闭的,你应该读一下,但这里有一些相关的引用:
If any application threads (daemon or nondaemon) are still running at shutdown time, they continue to run concurrently with the shutdown process.
The JVM makes no attempt to stop or interrupt any application threads that are still running at shutdown time; they are abruptly terminated when the JVM eventually halts.
因此线程不会被中断,但如果您愿意,您可以从关闭挂钩中显式中断它们。
IMO,守护进程线程将在关闭过程中继续 运行 并且 JVM 将在稍后退出应用程序时杀死所有 运行ning 线程。我不认为 运行ning 线程会得到 InterruptedException
,因为 JVM 不会做任何额外的努力来停止 运行ning 线程。
http://www.tutorialspoint.com/java/lang/runtime_addshutdownhook.htm