如何正常关闭/终止Java进程? [英] How to do graceful shutdown/termination of java processes?

查看:975
本文介绍了如何正常关闭/终止Java进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些Java应用程序,因此需要从Windows bat脚本正常关闭/关闭所有应用程序. 所以我的问题是:
如何通过Windows bat脚本调用关机钩子并正常关机Java程序.

I am running some java apps and I need to shutdown/close all apps gracefully from windows bat script. So my question is:
How to invoke shutdown hook by windows bat script and gracefully shutdown java program.

任何建议都值得赞赏. 预先感谢.

Any suggestion is appreciated. Thanks in advance.

推荐答案

通常通过让所有非守护进程线程正常完成其工作来实现Java中的正常"关闭.如果您的应用程序在某个端口上侦听关闭"命令,则可以让脚本将该命令触发到该端口,然后您可以使用该脚本为线程设置适当的标志以使其停止工作,从而使JVM关闭.这可能是我看过的最简单的方法.

A "graceful" shutdown in Java is generally achieved by letting all non-daemon threads complete their work normally. If you have your app listen for a "shutdown" command on some port, then you could have the script trigger the command to that port, which you could then use to set appropriate flags for your threads to stop working, letting the JVM shut down. That's probably the simplest way I've seen it done.

这篇关于如何正常关闭/终止Java进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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