如何使用Java编程来检查进程和端口是否存活?
我只是想用Java在Linux中构建一个集群软件。我想控制CPU负载,例如,如果CPU负载高于阈值,那么我减少执行线程大小。我以为我可以通过恶魔线程每秒或几秒钟检查一次CPU负载,如何在Java中实现它?如果我要检查特定进程是否已死亡,以及它打开的端口是否丢失,如何在 Java 中实现?
I just thinking to build a cluster software in Linux in Java. I want to control the CPU load, for example, if CPU load is higher than a threshold, then I reduce execution thread sizes. I thought I could check CPU load once per second or a couple of seconds by a demon thread, how to implement it in Java? and How to implment in Java if I am going to check particular process is dead or not, and the port it opens is lost or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(AFAIK)在纯 Java 中不可能做到这一点。您可以编写一些本机 C 来执行此操作,然后使用 JNA / JNI 进行接口(这将是最强大的解决方案)。
或者,一种快速的 hacky 简单方法(如果您只使用 Linux)将使用
Runtime.exec()
调用 这些然后您可以从 Java 内部解析本机 方法。在检查进程是否死亡方面,您可以使用上述方法,但使用
ps
。编辑:这可能会有所帮助。
There's no way (AFAIK) to do this in pure Java. You could potentially write some native C to do this and then interface using JNA / JNI (which would be the most robust solution.)
Alternatively, a quick hacky easy approach (if you're just using Linux) would be to use
Runtime.exec()
to call one of these native approaches which you could then parse from within Java.In terms of checking whether a process is dead or not, you could use the above approach but with
ps
.EDIT: This may be something that helps.