检测到JVM正在关闭 [英] Detecting that JVM is shutting down

查看:125
本文介绍了检测到JVM正在关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Swing应用程序,它使用addShutdownHook()处理Ctrl + C,并且可以正常工作,直到我执行了其中一个关闭任务,该调用任务在正常情况下会更改一个JLabel文本,此后它会挂起.

我认为问题在于Swing EDT已终止或正在等待某事.

是否有办法确定EDT已终止或已完成"(因此我可以避免调用Swing方法),或防止Ctrl-C出现通常的"windows-all-the-windows-down"行为?


只需要澄清一下:

我在名为stop()的类中有一个方法.在正常情况下,可以调用此方法(及其补语start()),并触发级联的事物,导致JLabel进行更新,以得到视觉反馈,表明已经发生了stop().

当我的关闭挂钩运行时,我需要调用stop()来正常关闭一些资源.

我要问的是如何检测到Swing EDT不存在,因此我可以重写stop(),以便它检测到缺少Swing并避免调用Swing函数.

解决方案

我最终在关闭钩子的开头设置了一个标志,并将其(通过预先设置的对象)与我的方法,以便它可以测试此标志并确定是否调用Swing方法.

如果未设置关闭标志,则调用Swing方法.否则他们不会.

I have a Swing application that handles Ctrl+C using addShutdownHook(), and it works fine until one of the shutdown tasks I have calls a function that under normal circumstances changes a JLabel text, at which point it hangs.

I assume the problem is that the Swing EDT has either terminated or is waiting for something.

Is there a way to either determine that the EDT has terminated or is "done" (so I can avoid calling Swing methods), or to prevent the usual close-all-the-windows-down behavior on Ctrl-C?


Just to clarify:

I have a method in a class called stop(). Under normal circumstances this can get called (along with its complement start()) and it triggers a cascade of things that causes a JLabel to be updated, for visual feedback that a stop() has occurred.

When my shutdown hook runs, I need to call stop() to gracefully shutdown some resources.

What I'm asking is how I can detect that Swing EDT is not there, so I can rewrite stop() so that it detects a lack of Swing and avoids the call to the Swing functions.

解决方案

I ended up setting a flag at the beginning of the shutdown hook, and communicating this (via objects set up ahead of time) to the object with my stop() method, so that it can test this flag and decide whether or not to call the Swing methods.

If the shutdown flag is not set, the Swing methods get called. Otherwise they don't.

这篇关于检测到JVM正在关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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