CTRL-C如何与Java程序配合使用 [英] How does CTRL-C work with Java program
问题描述
当我按照控制台中的 ctrl - c 按应用程序线程停止和关闭挂钩调用的顺序?
应用程序线程没有以任何明确定义的方式停止。事实上,他们可以继续跑直到过程退出。
如果你想要以有序的方式关闭你的线程,你需要在关闭挂钩中做一些事情,以引起这种情况发生。例如,一个关闭挂钩可以调用 Thread.interrupt()
告诉工作线程停止他们正在做什么...并调用 join()
,以确保它已发生。
When I press ctrl-c in console in what sequence are application threads stopped and shutdown hooks called?
According to the javadocs, the registered shutdown hooks are called in an unspecified order when the JVM starts shutting down; e.g. in response to a CTRL-C.
Application threads are not "stopped" in any well defined way. Indeed, they could continue running up right until the process exits.
If you want your threads to be shut down in an orderly fashion, you need to do something in a shutdown hook to cause this to happen. For example, a shutdown hook could call Thread.interrupt()
to tell worker threads to stop what they are doing ... and call join()
to make sure that it has happened.
这篇关于CTRL-C如何与Java程序配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!