我认为问题在于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正在关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 15:23