运行多个线程的JVM如何处理ctrl-c,w/w和w/o shutdown钩子? [英] How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks?
问题描述
无法在线找到此答案.当按Ctrl + C时:
Could not find this answer online. When Ctrl+C is hit:
- 当我们没有任何关闭钩子时,正在运行的线程会如何处理-它们是否都被InterruptedException击中?
- 当我们有关闭钩子时,我知道关闭钩子会以任意顺序在新线程中运行.但是现有的正在运行的线程会怎样?他们仍然每个人都受到InterruptedException的打击吗?
谢谢!
推荐答案
经典书籍"Java Concurrency in Practice"中有关于JVM关闭的一章(7.4),您应该阅读该书,但是这里有一些相关的引号:
The classic book "Java Concurrency in Practice" has a chapter (7.4) on the JVM shutdown, you should read that, but here are some relevant quotes:
如果在关闭时仍在运行任何应用程序线程(守护程序或非守护程序),它们将继续与关闭进程同时运行.
If any application threads (daemon or nondaemon) are still running at shutdown time, they continue to run concurrently with the shutdown process.
JVM不会尝试停止或中断在关闭时仍在运行的任何应用程序线程;当JVM最终停止时,它们会突然终止.
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.
因此线程不会被中断,但是您可以根据需要从关闭挂钩中显式地中断线程.
So the threads are not interrupted, but you can interrupt them explicitly from the shutdown hook, if you wish.
这篇关于运行多个线程的JVM如何处理ctrl-c,w/w和w/o shutdown钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!