开发者

Cleanup before exiting Java application

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜