ProcessBuilder调用的Java进程永远睡眠

ProcessBuilder调用的Java进程永远睡眠

本文介绍了ProcessBuilder调用的Java进程永远睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个大系统上工作,我完全用Java编写。但是我也在某种程度上通过运行它作为一个进程,通过文件系统传递一些输入,然后等待它完成从中读取结果与C程序。现在,我不能这样做。我没有时间通过​​API或类似的东西链接他们。

I'm working on a big system, which I have written entirely in Java. But I'm also at some point communicating with a C program by running it as a process, communicating some input to it through the file system and then waiting for it to finish to read the results from it. For now, I can't do anything but this. There is no time for me to link them via an API or anything like that.

这样工作得很好,直到我需要调用这个过程两次才能得到结果。一个我做第一次调用,它工作很好。然而,对于第二次调用,进程只是挂起!我不会,如果它是睡觉和等待信号,但我不明白为什么它应该这样做。

This worked well until I needed to invoke this process twice to get the results. One I make the first invocation, it works just fine. However, for the second invocation the process just hangs! I don't if it is sleeping and waiting for a signal but I don't see why it should do so.

这是调用方法:

public synchronized boolean processCommand(List command) {
    try {
        ProcessBuilder pb = new ProcessBuilder(command);
        Process p = pb.start();
        p.waitFor();
        p.destroy();
    } catch(Exception ex) { return false; }
    return true;
}



我真的不需要与stdout或stdin通信。我只需要这个过程来运行并完成它的工作。但它只是挂起,当调用进程正在等待它只有第二次我调用它!
我的调用代码只是简单地做出命令列表,并从另一个java对象调用此方法。

I really do not need to communicate to the stdout or stdin. I just need the process to run and finish its job. But it just hangs when the calling process is waiting for it ONLY the second time I call it!My calling code is just simply making the command list and calling this method from another java object.

当对C程序的输入较小时,对processCommand(List命令)方法的调用都正常工作。这将是stdin或stdout的一些问题吗?

Both calls to the method processCommand(List command) work fine when the input to the C program is smaller. Would that be some issue with the stdin or stdout?

这只是驱使我疯了!任何人都有洞察这一点?非常感谢您的赞扬:)

It's just driving me crazy! Anybody has an insight into this? I appreciate your commend :)

更新:

关于什么@Gray提到:

Here is the solution based on what @Gray mentioned:

我只需要排除InputStream和可能的ErrorStream:

I just need to drain the InputStream and possibly the ErrorStream:

public synchronized boolean processCommand(List command) {
try {
    ProcessBuilder pb = new ProcessBuilder(command);
    Process p = pb.start();
    handleStream(p.getInputStream);
    handleStream(p.getErrorStream);
    p.waitFor();
    p.destroy();
} catch(Exception ex) { return false; }
return true;

}

public void handleStream(InputStream input) {
    try {
        int c;
        while( (c=input.read())!= -1) { //Anything }
    } catch(Exception ex) { }
}


推荐答案

尝试按照建议的解决方案

Try following the solution suggested here

[...]

没有及时读取子进程的输出流可能导致子进程阻塞,甚至死锁。

Java 6 API clearly states that failure to promptly "read the output stream of the subprocess may cause the subprocess to block, and even deadlock."

[...]

今天处理这个问题的安全赌注是通过调用关闭在Process.getOutputSteam,Process.getInputStream和Process.getErrorStream提供的每个流上显式清除每个Process的实例,然后即使进程已终止,仍调用Process.destroy。

The safe bet for handling this problem today is to explicitly clean up every instance of Process by calling close on each stream made available through Process.getOutputSteam, Process.getInputStream, and Process.getErrorStream, and then call Process.destroy even if the process is already terminated.

这篇关于ProcessBuilder调用的Java进程永远睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:07