开发者

Cleanup before exiting Java application

开发者 https://www.devze.com 2023-02-26 01:04 出处:网络
I\'m writing a small Java application using Swing. The application also has a networking component. I want to run some code before exiting(close channels, keys,开发者_开发知识库 etc).Is there a way t

I'm writing a small Java application using Swing. The application also has a networking component.

I want to run some code before exiting(close channels, keys,开发者_开发知识库 etc).Is there a way to do this, without adding an Exit button in my interface, in which to do my cleanup, then do System.exit()?


You can use the Runtime#addShutdownHook function to add a function to run when the JVM shuts down.


You should be able to do what you want in windowClosing(), shown here.


'Close keys': SelectionKeys? You don't need to do anything about those. Closing the channel cancels all its SelectionKeys automatically. And unless you have pending data you need to flush you don't really need to close Channels either, the OS will do that for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号