简体   繁体   English

如何停止Swing EDT

[英]How to stop the Swing 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. 典型的Swing应用程序在开始时启动EDT,当最后一个窗口关闭时,Application基本上使用隐式或显式的System.exit停止。

But my little application is actually a plugin for a framework which knows nothing about Swing. 但我的小应用程序实际上是一个对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 . 所以我不能调用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 ... 但如果我不这样做,EDT将继续运行,一旦框架完成,EDT仍将运行并运行并运行...

So I'd like to kill the EDT without killing the application. 所以我想在不杀死应用程序的情况下杀死EDT。 How do I do that? 我怎么做?

The following document from Oracle/Sun shed some light on the issue: AWT Threading Issues Oracle / Sun的以下文档阐述了该问题: AWT线程问题

[...] [...]

Prior to 1.4, the helper threads were never terminated. 在1.4之前,辅助线程从未终止。

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: 从1.4开始,由于4030718的修复,行为已经改变。使用当前实现,AWT终止其所有帮助程序线程,允许应用程序在满足以下三个条件时干净地退出:

  • There are no displayable AWT or Swing components. 没有可显示的AWT或Swing组件。
  • There are no native events in the native event queue. 本机事件队列中没有本机事件。
  • There are no AWT events in java EventQueues. java EventQueues中没有AWT事件。

Therefore, a stand-alone AWT application that wishes to exit cleanly without calling System.exit must: 因此,希望在不调用System.exit的情况下干净地退出的独立AWT应用程序必须:

  • Make sure that all AWT or Swing components are made undisplayable when the application finishes. 确保在应用程序完成时使所有AWT或Swing组件无法显示。 This can be done by calling Window.dispose on all top-level Windows. 这可以通过在所有顶级Windows上调用Window.dispose来完成。 See Frame.getFrames. 请参见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. 确保应用程序使用任何AWT或Swing组件注册的AWT事件侦听器的任何方法都无法运行到无限循环或无限期挂起。 For example, an AWT listener method triggered by some AWT event can post a new AWT event of the same type to the EventQueue. 例如,由某些AWT事件触发的AWT侦听器方法可以将相同类型的新AWT事件发布到EventQueue。 The argument is that methods of AWT event listeners are typically executed on helper threads. 参数是AWT事件侦听器的方法通常在辅助线程上执行。

[...] [...]

There may be some hidden windows (for example, dialogs displayed using JOptionPane.showMessageDialog(…) which are already closed) preventing Swing from exiting. 可能有一些隐藏的窗口(例如,使用JOptionPane.showMessageDialog(…)显示的对话框已经关闭)阻止Swing退出。 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. 然后应该停止Event Dispatch Thread。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM