java怎么让进程停止_JAVA:如何在进程停止时调用一个方法(甚至有可能吗?)

Here is a pretty good guide to signal handling in Java。它还涵盖关闭和终止情况,包括Ctrl-C。

您想要的特定呼叫位于运行时间addShutdownHook中。

还有一些你不能处理的具体情况......操作系统通常会以某种方式直接杀死应用程序而不给它一个自我保存的机会。你无法通过这种机制来覆盖每一个案例。引用JavaDoc

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值