加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 停止所有Awt / Swing线程和监视器和东西,以便只剩下主线

发布时间:2020-12-15 08:45:57 所属栏目:Java 来源:网络整理
导读:我有以下内容 public static void main(String[] args) { boolean running = true; boolean foo= false; while(running) { doSomeTask(); // might set foo true if(foo) { //This call waits/blocks until gui is done working. fireUpSwingGui(); //does w
我有以下内容

public static void main(String[] args) {
    boolean running = true;
    boolean foo= false;

    while(running)
    {
        doSomeTask(); // might set foo true
        if(foo) {
            //This call waits/blocks until gui is done working.
            fireUpSwingGui(); //does work...
            foo=false;
            godModeReleaseGUIandALLResourcesOnlyWantMainThreadLeft();
        }
    }
}

希望godModeReleaseGUIandALLResourcesOnlyWantMainThreadLeft()说明一切.

请记住,当foo在doSomeTask()内部再次变为真时,我们可能会在稍后阶段再次启动gui.

解决方法

看看 AWT Threading Issues,它解释了退出AWT应用程序的标准.您要关注的部分如下:

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 07001 on all top-level Windows. See 07002.
  • 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.

一个快速示例应用程序来演示……

import java.awt.Frame;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class CloseAWT
{
    private static boolean running = true;
    private static int response = -1;

    public static void main(String[] args)
    {
        boolean showSwing = true;
        boolean checkFrames = true;
        while (running)
        {
            if (showSwing)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        response = JOptionPane.showConfirmDialog(null,"Hello World?");
                    }
                });
                showSwing = false;
            }
            else
            {
                if (response >= 0 && checkFrames)
                {
                    SwingUtilities.invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            // topFrame.dispose();
                            Frame[] frames = Frame.getFrames();
                            System.out.printf("frames.length=%dn",frames.length);
                        }
                    });
                    checkFrames = false;
                }
            }
        }
    }
}

为了确认行为符合预期,我在JProfiler中运行了这个.单击“是”以关闭确认对话框后,’AWT-EventQueue-0’线程被标记为已死.在此之后唯一活着的线程是’main’和侦听Ctrl-Break的线程.

我强烈建议使用像JProfiler,YourKit,JProbe或其中一个免费的分析器,以确保您已正确释放所有组件并删除所有听众.

最后一个想法……您可能想要考虑将GUI作为一个单独的进程生成,并使用某种IPC在您的守护进程和GUI之间传递信息.虽然这会产生额外进程和IPC的额外开销,但它可以让您更好地确保在不再需要GUI时完全清理它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读