gpt4 book ai didi

java - 如何停止 Swing EDT

转载 作者:搜寻专家 更新时间:2023-10-30 21:07:53 26 4
gpt4 key购买 nike

典型的 Swing 应用程序在开始时启动 EDT,当最后一个窗口关闭时,应用程序基本上以隐式或显式的 System.exit 停止。

但我的小应用程序实际上是一个对 Swing 一无所知的框架的插件。我的插件将在调用时显示一个对话框,以便从用户那里获得一些输入并随后退出,但框架将/必须继续运行。所以我不能调用 System.exit

但如果我不这样做,EDT 将继续运行,一旦框架完成,EDT 仍将运行并运行......

所以我想在不终止应用程序的情况下终止 EDT。我该怎么做?

最佳答案

来自 Oracle/Sun 的以下文档阐明了这个问题: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.

[...]

关于java - 如何停止 Swing EDT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com