程序在java中退出时调用函数

编程入门 行业动态 更新时间:2024-10-25 11:34:36
本文介绍了程序在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中退出时调用函数

本文发布于:2023-11-27 19:40:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639344.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   函数   程序   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!