为什么这个shutdownhook不会工作? [英] Why won't this shutdownhook work?

查看:445
本文介绍了为什么这个shutdownhook不会工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主要方法,它包含一个shutdownhook:

This is my main method and it contains a shutdownhook:

public static void main(String args[]) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            JOptionPane.showMessageDialog(null, "Shutdown hook");
        }
        });

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(
            new Runnable() {
                @Override
                public void run() {
                    Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
                }
            });
}

问题是 JOptionPane 根本不显示。相反,框架关闭,但应用程序本身仍然运行。
PS。我不能使用 WindowClosing 事件,因为它不在 Cmd + Q 命令在 Mac OS X

The problem is the JOptionPane doesn't show up at all. Instead, the frame closes but the app itself still runs. PS. I can't use the WindowClosing event because it doesn't fire on the Cmd+Q command on Mac OS X.

推荐答案

发送线程已经关闭,或者可能在之前关闭您的提交到队列是执行。

The dispatch thread is already shut down, or is likely to be shut down before your submission to the queue is executed.

您需要找到另一种捕获退出事件的方法,关闭挂钩在那里,以便在VM退出时可以整理,所以你真的不想在调用UI时绑定更多的资源。

You need to find another way of trapping the quit events, shutdown hook is there so you can tidy up when the VM is exiting, so you really don't want to by tying up more resources in the UI when it is called.

这篇关于为什么这个shutdownhook不会工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆