如何停止 Swing EDT

发布于 2024-10-15 08:40:54 字数 285 浏览 2 评论 0原文

典型的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无语# 2024-10-22 08:40:54

Oracle/Sun 的以下文档阐明了该问题: AWT 线程问题

[...]

在 1.4 之前,辅助线程永远不会终止。

从 1.4 开始,由于修复了 4030718,该行为已发生变化。在当前实现中,AWT 终止其所有辅助线程,允许应用程序在满足以下三个条件时干净退出:

  • 没有可显示的 AWT 或 Swing 组件。
  • 本机事件队列中没有本机事件。
  • java EventQueue 中没有 AWT 事件。

因此,希望在不调用 System.exit 的情况下干净退出的独立 AWT 应用程序必须:

  • 确保应用程序完成时所有 AWT 或 Swing 组件都变得不可显示。这可以通过在所有顶级 Windows 上调用 Window.dispose 来完成。请参阅 Frame.getFrames。 ...
  • 确保应用程序向任何 AWT 或 Swing 组件注册的 AWT 事件侦听器的方法都不会陷入无限循环或无限期挂起。例如,由某个 AWT 事件触发的 AWT 侦听器方法可以将相同类型的新 AWT 事件发布到 EventQueue。争论的焦点是 AWT 事件侦听器的方法通常在辅助线程上执行。

[...]

The following document from Oracle/Sun shed some light on the issue: AWT Threading Issues

[...]

Prior to 1.4, the helper threads were never terminated.

Starting with 1.4, the behavior has changed as a result of the fix for 4030718. With the current implementation, AWT terminates all its helper threads allowing the application to exit cleanly when the following three conditions are true:

  • There are no displayable AWT or Swing components.
  • There are no native events in the native event queue.
  • There are no AWT events in java EventQueues.

Therefore, a stand-alone AWT application that wishes to exit cleanly without calling System.exit must:

  • Make sure that all AWT or Swing components are made undisplayable when the application finishes. This can be done by calling Window.dispose on all top-level Windows. See Frame.getFrames. ...
  • Make sure that no method of AWT event listeners registered by the application with any AWT or Swing component can run into an infinite loop or hang indefinitely. For example, an AWT listener method triggered by some AWT event can post a new AWT event of the same type to the EventQueue. The argument is that methods of AWT event listeners are typically executed on helper threads.

[...]

大海や 2024-10-22 08:40:54

可能有一些隐藏窗口(例如,使用 JOptionPane.showMessageDialog(…) 显示且已关闭的对话框)阻止 Swing 退出。您可以使用以下命令进行检查:

Stream.of(Window.getWindows()).forEach(System.out::println);

如果您不再需要它们,则可以轻松删除它们:

Stream.of(Window.getWindows()).forEach(Window::dispose);

然后事件调度线程应该停止。

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 using

Stream.of(Window.getWindows()).forEach(System.out::println);

If you don’t need them anymore, you can get rid of them easily:

Stream.of(Window.getWindows()).forEach(Window::dispose);

The Event Dispatch Thread should then stop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文