如何在关闭挂钩中使用摆动? [英] How do I use swing in a shutdown hook?

查看:39
本文介绍了如何在关闭挂钩中使用摆动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将swing添加到关闭钩子中(即在VM关闭时显示一个弹出窗口)?

Is there any possible way to add swing into a shutdown hook (that is, display a popup upon the VM shutting down)?

我意识到如果我尝试创建一个新的 JFrame,它会给我一个错误,因为它试图注册一个关闭挂钩,但由于 VM 已经关闭而失败.我只是想知道实际上是否有任何解决方法

I realize that if I try to make a new JFrame, it will give me an error, as it tries to register a shutdown hook, which fails as the VM is already shutting down. I'm just wondering if there is in fact any way around this

推荐答案

你真的不应该这样做.来自 Runtime.addShutdownHook 规范:

You really shouldn't be doing this. From the Runtime.addShutdownHook specification:

Java 虚拟机关闭以响应两种事件:

The Java virtual machine shuts down in response to two kinds of events:

  • 程序正常退出,当最后一个非守护线程退出时或者当exit(相当于,System.exit)方法是调用,或
  • 虚拟机在响应用户中断(例如键入 ^C)或系统范围事件(例如用户注销或系统关闭)时终止.
  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
  • The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

...

关闭钩子在虚拟机生命周期的一个微妙时刻运行,因此应该进行防御性编码.特别是,它们应该被编写为线程安全的,并尽可能避免死锁.他们也不应该盲目依赖可能已经注册了自己的关闭钩子的服务,因此他们自己可能会在关闭过程中.例如,尝试使用其他基于线程的服务(例如 AWT 事件分派线程)可能会导致死锁.

Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.

关闭钩子也应该快速完成它们的工作.当程序调用 exit 时,期望虚拟机将立即关闭并退出.当虚拟机由于用户注销或系统关闭而终止时,底层操作系统可能只允许关闭和退出的固定时间量.因此,不建议尝试任何用户交互或在关闭挂钩中执行长时间运行的计算.

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.

...

在极少数情况下,虚拟机可能会中止,即在没有彻底关闭的情况下停止运行.当虚拟机从外部终止时会发生这种情况,例如在 Unix 上使用 SIGKILL 信号或在 Microsoft Windows 上使用 TerminateProcess 调用.如果本地方法出错,例如破坏内部数据结构或尝试访问不存在的内存,虚拟机也可能中止.如果虚拟机中止,则无法保证是否会运行任何关闭挂钩.

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.

此处建议您不要这样做的具体警告:

Specific warnings here that suggest you not do this:

  1. Shutdown hooks 也应该快速完成它们的工作."

依赖任何可能需要一段时间来完成其工作的东西,或者像JOptionPane对话框那样无限期地阻塞用户输入,都是不是您应该在关闭挂钩中做什么.

Relying on anything that might take a while to do its work, or blocking indefinitely on user-input like JOptionPane dialogs, is not what you should be doing in your shutdown hook.

例如,尝试使用其他基于线程的服务(例如 AWT 事件分派线程)可能会导致死锁"

Swing 在 AWT 之上运行,其底层的事件调度线程也可能正在关闭.在关闭时尝试使用 Swing 或 AWT 不仅会导致死锁,而且可能根本无法正常工作.

Swing runs on-top of AWT, whose underlying event-dispatch thread may be in the process of shutting down, too. Trying to use Swing or AWT while shutting down can lead not only to dead locks but also may just not work at all, anyways.

如果虚拟机中止,则无法保证是否会运行任何关闭挂钩"

无法保证您的用户甚至可能收到您的消息,因为只有在正常退出或终止时才能保证关闭挂钩运行 - 不会在暂停或中止时运行.

There are no guarantees your user could even possibly get your message, since shutdown hooks are only guaranteed to run when it exits normally or terminated -- not when halted or aborted.

这篇关于如何在关闭挂钩中使用摆动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆