我需要根据我的申请结果修改JVM返回码。
但是显式调用 System.exit(code) 是有风险的,因为应用程序很复杂并且很难识别运行线程的结束。
所以我想到了使用 shutdown hook 在 JVM 退出之前修改返回码。
但是有一个问题,我怎样才能得到JVM的原始返回码,因为它可能是一个非0的错误码。
最佳答案
你不应该在 shutdown hook 中调用 exit 方法,System.exit(status)
内部调用 Runtime.getRuntime().exit(status);
这会导致你无限期阻止的应用程序。
根据 JavaDoc
If this method is invoked after the virtual machine has begun its shutdown sequence then if shutdown hooks are being run this method will block indefinitely.
您无权访问status
,因为它可能会在所有关闭 Hook 被调用后发生变化。
关于java - 如何在关闭 Hook 中获取返回码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42127345/