如何处理Java中的关闭应用程序事件? [英] How to handle a closing application event in Java?

查看:101
本文介绍了如何处理Java中的关闭应用程序事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有控制台应用程序,服务器接受来自客户端的多个连接,是否可以在关闭应用程序上设置侦听器或事件?在这种情况下,我想要告诉所有连接的客户端在应用程序真正关闭之前轻轻地断开连接。



任何解决方案?谢谢!

解决方案

您想使用关机挂钩:

 调用Runtime.getRuntime()addShutdownHook(theHookThread)。 

所以线程将在JVM关闭时运行,请参阅这里的详细信息。 p>

Having a console application, a server accepting several connections from clients, is it possible to have a listener or an event on a closing application? I want, in this event, tell all connected clients to gently disconnect before the application really closes itself.

Any solution? Thank you!

解决方案

You want to use a shutdown hook:

Runtime.getRuntime().addShutdownHook(theHookThread);

So the thread will be run when the JVM shuts down, see here for details.

这篇关于如何处理Java中的关闭应用程序事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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