java 获取应用程序正常运行时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6431607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get application uptime
提问by aleroot
I've written an application in java and I want to add a feature to report the uptime of the application. Is there a Class/method in the JVM that can do this?
我已经用 java 编写了一个应用程序,我想添加一个功能来报告应用程序的正常运行时间。JVM 中是否有可以执行此操作的类/方法?
Should I save the timestamp when application starts, then calculate the difference with the current uptime in the moment of the request?
我应该在应用程序启动时保存时间戳,然后在请求的那一刻计算与当前正常运行时间的差异吗?
What's a better way to retrieve application uptime in Java?
在 Java 中检索应用程序正常运行时间的更好方法是什么?
回答by Rob Harrop
You can use RuntimeMXBean.getUptime()
您可以使用RuntimeMXBean.getUptime()
RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
long uptime = rb.getUptime();
回答by Tomasz Nurkiewicz
The result is in milliseconds:
结果以毫秒为单位:
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
System.out.println(mxBean.getUptime());
See: ManagementFactory.getRuntimeMXBean()javadoc.
请参阅:ManagementFactory.getRuntimeMXBean()javadoc。
回答by Kamahire
It is just like Heartbeat application. IN which You have write small application which communicate to your java application. Whenever You application start you have to store the time with date in file/database. Monitor application will check the application running or not. The monitor application also take difference of end time and start time sand display the application time.
它就像心跳应用程序。您在其中编写了与 Java 应用程序通信的小应用程序。每当您的应用程序启动时,您都必须将时间和日期存储在文件/数据库中。监控应用程序将检查应用程序是否正在运行。监控应用程序还通过结束时间和开始时间的差异来显示应用程序时间。