Java中的线程可以监视它启动的进程吗? [英] Can a thread in java monitor processes that it starts?

查看:87
本文介绍了Java中的线程可以监视它启动的进程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有10个线程池,并且这些线程退出并启动了不同的进程(perl脚本),那么有没有办法让这些线程检查"这些脚本以查看它们的运行状况?

For example if i have a pool of 10 threads and these threads go out and start different processes (perl scripts), is there a way that those threads can "check up" on those scripts to see how they're doing?

有时某些脚本冻结了,我无法得知.因此,我一直在考虑一种使线程每隔一段时间检查一次脚本的方法,以便在脚本挂起时得到通知,以便我可以弄清它们为什么挂断并解决问题.

Sometimes some of the scripts freeze up and I have no way of knowing. So i've been thinking of a way to have the threads check on the scripts every once in a while so I can be notified when a script is hung up so that i can start figuring out why they are hanging up and fix the problem.

推荐答案

例如,如果我有10个线程池,并且这些线程退出并启动了不同的进程(perl脚本),那么有没有办法让这些线程检查"这些脚本以查看它们的运行状况?

For example if i have a pool of 10 threads and these threads go out and start different processes (perl scripts), is there a way that those threads can "check up" on those scripts to see how they're doing?

我知道的唯一方法是,脚本本身每隔X秒输出某种状态消息到标准输出或错误,并且产生脚本的线程正在积极读取,等待输出.然后,它可以更新有关该过程的状态信息.

The only way that I know of to do this is if the script itself output some sort of status message every X seconds to standard-out or error and the thread that spawned the script was actively reading, waiting for the output. It then could update status information about the process.

如果使用ProcessBuilder并调用start()以获取Process,则可以将BufferedReader附加到process.getOutputStream()以监视脚本.当然,您也可以调用process.exitValue()来查看该过程是否已完成,但这不会告诉您是否已挂起.

If you use the ProcessBuilder and call start() to get a Process, then you can attach a BufferedReader to the process.getOutputStream() to monitor the script. You certainly can also call process.exitValue() to see if the process has finished but that won't tell you if it is hung.

或者,脚本可以通过套接字或其他IPC以某种方式回调监视过程,但我认为仅监视输出流是最好的方法.

Alternatively would be for the script to somehow call back to the monitoring process via a socket or some other IPC but I think just monitoring the output-stream is the best approach.

这篇关于Java中的线程可以监视它启动的进程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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