java 输入ctrl c,CTRL-C如何与Java程序配合使用

当在控制台中按下 Ctrl+C 时,Java 应用程序的关闭过程是不确定的,不会按照特定顺序停止线程。Shutdown Hooks 在 JVM 关闭时按未指定顺序调用,而应用线程可能继续运行直至进程结束。如果希望线程有序关闭,需要在 shutdown hook 中执行如 `Thread.interrupt()` 来通知工作线程停止,并使用 `join()` 确保其已经停止。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值