程序在java中退出时调用函数 [英] Calling function when program exits in java

查看:106
本文介绍了程序在java中退出时调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每次用户退出程序时保存程序设置。所以当用户退出程序时,我需要一种方法来调用函数。我如何做?

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?

我正在使用Java 1.5。

I am using Java 1.5.

推荐答案

p>您可以通过执行以下操作为应用程序添加一个关闭钩子:

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
    }
}));

这基本上等同于在整个程序中尝试{} finally {}包含最后的块中的内容。

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

请注意注意事项

这篇关于程序在java中退出时调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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