java java程序退出时调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/63687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:54:37  来源:igfitidea点击:

Calling function when program exits in java

javaevents

提问by Mat Mannion

I would like to save the programs settings every time the user exits the program. So I need a way to call a function when the user quits the program. How do I do that?

每次用户退出程序时,我都想保存程序设置。所以我需要一种在用户退出程序时调用函数的方法。我怎么做?

I am using Java 1.5.

我正在使用 Java 1.5。

回答by Mat Mannion

You can add a shutdown hook to your application by doing the following:

您可以通过执行以下操作向应用程序添加关闭挂钩:

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    public void run() {
        // what you want to do
    }
}));

This is basically equivalent to having a try {} finally {} block around your entire program, and basically encompasses what's in the finally block.

这基本上相当于在整个程序中使用 try {} finally {} 块,并且基本上包含 finally 块中的内容。

Please note the caveatsthough!

不过请注意注意事项

回答by mfx

Adding a shutdown hook addShutdownHook(java.lang.Thread)is probably what you look for. There are problems with that approach, though:

添加关闭挂钩addShutdownHook(java.lang.Thread)可能是您要寻找的。但是,这种方法存在问题:

  • you will lose the changes if the program aborts in an uncontrolled way (i.e. if it is killed)
  • you will lose the changes if there are errors (permission denied, disk full, network errors)
  • 如果程序以不受控制的方式中止(即如果它被终止),您将丢失更改
  • 如果出现错误(权限被拒绝、磁盘已满、网络错误),您将丢失更改

So it might be better to save settings immediately (possibly in an extra thread, to avoid waiting times).

所以最好立即保存设置(可能在一个额外的线程中,以避免等待时间)。

回答by Avrom

Are you creating a stand alone GUI app (i.e. Swing)?

您是否正在创建一个独立的 GUI 应用程序(即 Swing)?

If so, you should consider how you are providing options to your users how to exit the application. Namely, if there is going to be a File menu, I would expect that there will be an "Exit" menu item. Also, if the user closes the last window in the app, I would also expect it to exit the application. In both cases, it should call code that handles saving the user's preferences.

如果是这样,您应该考虑如何向用户提供退出应用程序的选项。也就是说,如果有一个文件菜单,我希望会有一个“退出”菜单项。此外,如果用户关闭应用程序中的最后一个窗口,我也希望它退出应用程序。在这两种情况下,它都应该调用处理保存用户首选项的代码。

回答by Kevin Day

Using Runtime.getRuntime().addShutdownHook() is certainly a way to do this - but if you are writing Swing applications, I strongly recommend that you take a look at JSR 296 (Swing Application Framework)

使用 Runtime.getRuntime().addShutdownHook() 当然是一种方法 - 但是如果您正在编写 Swing 应用程序,我强烈建议您查看 JSR 296(Swing 应用程序框架)

Here's a good article on the basics: http://java.sun.com/developer/technicalArticles/javase/swingappfr/.

这是一篇关于基础知识的好文章:http: //java.sun.com/developer/technicalArticles/javase/swingappfr/

The JSR reference implementation provides the kind of features that you are looking for at a higher level of abstraction than adding shutdown hooks.

JSR 参考实现提供了您在比添加关闭挂钩更高的抽象级别上寻找的功能类型。

Here is the reference implementation: https://appframework.dev.java.net/

这是参考实现:https: //appframework.dev.java.net/