如何停止 Swing EDT
典型的 Swing 应用程序在开始时启动 EDT,当最后一个窗口关闭时,应用程序基本上会停止并出现隐式或显式的 System.exit。
但我的小应用程序实际上是一个框架的插件,它对 Swing 一无所知。我的插件将在调用时显示一个对话框,以便从用户获取一些输入并随后退出,但框架将/必须继续运行。所以我无法调用System.exit
。
但如果我不这样做,EDT 将继续运行,一旦框架完成,EDT 仍将运行、运行、运行……
所以我想在不终止应用程序的情况下终止 EDT。我该怎么做?
The typical Swing application starts the EDT at the beginning and when the last window is closed the Application stops basically with a System.exit either implicit or explicit.
But my little application is actually a plugin for a framework which knows nothing about Swing. My plugin will when called display a dialog, in order to get some input from the user and exit afterwards but the framework will/must keep running. So I can't call System.exit
.
But if I don't do that the EDT will continue to run and once the framework is finished the EDT will still run and run and run ...
So I'd like to kill the EDT without killing the application. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Oracle/Sun 的以下文档阐明了该问题: AWT 线程问题
The following document from Oracle/Sun shed some light on the issue: AWT Threading Issues
可能有一些隐藏窗口(例如,使用 JOptionPane.showMessageDialog(…) 显示且已关闭的对话框)阻止 Swing 退出。您可以使用以下命令进行检查:
如果您不再需要它们,则可以轻松删除它们:
然后事件调度线程应该停止。
There may be some hidden windows (for example, dialogs displayed using
JOptionPane.showMessageDialog(…)
which are already closed) preventing Swing from exiting. You can check this usingIf you don’t need them anymore, you can get rid of them easily:
The Event Dispatch Thread should then stop.