在 Java 应用程序中,使用关闭挂钩自动更新应用程序是个好主意吗?

In a Java application, is it a good idea to use a shutdown hook to auto-update the app?

想法是在应用程序 运行 时检查更新。该更新包含一个主 jar 和几个第三方 jar。如果有可用更新:

这是个好主意吗?

如果你想要一个围绕 shutDown 挂钩的可靠系统,那么它不是一个好主意,因为遵循以下准则:-
a) Shutdown Hooks在某些情况下可能不会执行。
b) 一旦启动,shutdown hooks 可以在完成前强行停止。
c) 如果定义了多个shutdown hooks,它们的执行顺序不保证。
d) 不能 register/unregister 关闭挂钩与 in 关闭挂钩。

有关它的更多信息,请访问 Javadoc ref

没有。来自 Javadoc:

Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

因此您不应在关闭挂钩中进行长运行或阻塞活动。