有什么方法可以识别/检查外部进程正在运行的线程吗?
我正在使用 Runtime.exec() 在我的 Jave Web 应用程序中启动外部 16 位 DOS 程序(出于旧应用程序兼容性原因)。我有流读取器来捕获进程的输出,我有一个计时器,如果需要太长时间就会中断,并且我使用 Process.waitFor() 等待进程完成,然后再分析捕获的输出。大多数时候,一切都运转良好。
问题是,当天第一次请求应用程序(我们尚未上线)时,它会超时。我真的很想做某种监控来告诉到底发生了什么 - 生成的进程是否正在等待操作系统许可运行?各位读者还在等吗?操作系统是否卸载了我昨天运行该应用程序时内存中的 DOS 程序,并且只需要一段时间才能再次加载它?
这不是一个非常一致和可重复的事情,但似乎通常与整体计算机负载和自应用程序上次运行以来的时间长度有关。
不管怎样,我希望如果我能识别进程正在运行的线程(这是一台 Windows32 机器,顺便说一句),也许我可以告诉一些事情,或者也许我可以调整该线程的优先级。如果没有,是否有任何类型的监控应用程序可以附加到 tomcat 或 netbeans 上,可能会有用?
谢谢,
丽贝卡
I am using Runtime.exec() to launch an external 16-bit DOS program (for legacy app compatibility reasons) within my Jave web application. I have stream readers to capture the process's output, I have a timer to interrupt if it takes too long, and I use Process.waitFor() to wait until the process is done before I analyze the captured output. It all works well, MOST of the time.
The problem is, the first time the app is requested for the day (we're not live yet), it times out. I would really like to do some kind of monitoring to tell what the heck is going on - is the spawned process sitting around waiting for OS permission to run? Are the stream readers waiting? Did the OS unload the DOS program that had been in memory when I was running the app yesterday, and it just takes a while to load it again?
It's not a tremendously consistent and reproducible thing, but seems to be generally related to overall computer load and the length of time since the app was last run.
Anyway, I was hoping if I could identify the thread the process was running in (this is a Windows32 machine, BTW), maybe I could tell something, or maybe I could tweak the priority of that thread. If not, is there any sort of monitoring app I can attach to tomcat or to netbeans that might be of any use?
Thanks,
Rebeccah
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在纯 Java 中无法做到这一点,但您可能可以通过使用反射来探索实际
Process
子类的私有字段来获得一些牵引力。 (在UNIXProcess
的情况下,UNIX pid 存储在私有字段中...)如果您想获取 Java 中的 pid(或 Windows 等效项)以外的内容,或者您想更改进程优先级,您需要通过 JNI 来完成。 (我记得看到过一个 Java + 本机代码库用于在 UNIX 上执行此类操作。)
但说实话,我认为您最好以不同的方式解决此问题。例如,如果这是“当天第一次”,请尝试使用更长的超时。
There is no way to do this in pure Java, but you might be able to get some traction by using reflection to poke around in the private fields of the actual
Process
subclass. (In the case ofUNIXProcess
, the UNIX pid is stored in a private field ...)If you wanted to get more than the pid (or Windows equivalent) in Java, or if you wanted to change process priority, you'd need to do it via JNI. (I recall seeing a Java + native code library for doing this kind of stuff on UNIX.)
But to be honest, I think you'd be better off solving this a different way. For instance, try using a longer timeout if this is the "first time for the day".
此时,外部工具如 Process Monitor将是您最好的选择,以检查该进程保留了哪些资源。
其中之一(如文件句柄)可以解释超时。
At this point, an external tool like Process Monitor would be your best option, in order to check what kind of resources that process keep.
One of those (like a file handle) could explain the timeout.