0

以下のコードが実行されるたびに、Defunct ゾンビ プロセスに入ります。誰かがこの問題を解決するのを手伝ってくれませんか。

  private static boolean executeCommand(String command)
        throws ClientException, IOException, InterruptedException {

    int exitVal = 1; // 0 is success, so we default to a nonzero.
    Process proc = null;
    try{
    Runtime rt = Runtime.getRuntime();
    proc = rt.exec(command);

    //Below lines are required to flush out the streams. else the process will hang.
    ReadStream s1 = new ReadStream("stdin", proc.getInputStream ());
    ReadStream s2 = new ReadStream("stderr", proc.getErrorStream ());
    s1.start ();
    s2.start ();        

    exitVal = proc.waitFor();

    if (exitVal == 0) {
        return true;
    } else {
        throw new ClientException("103", "" + command + " failed.");
    }
    }finally{
        if(proc  != null){
            proc.destroy();
        }
    }

}

別のスレッドですべてのストリームをクリアしています。

これが私のReadStreamクラスです

 public class ReadStream implements Runnable {

private static Logger logger = Logger.getLogger(ReadStream.class);

String name;
InputStream is;
Thread thread;

public ReadStream(String name, InputStream is) {
    this.name = name;
    this.is = is;
}

public void start() {
    thread = new Thread(this);
    thread.start();
}

public void run() {
    try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        while (true) {
            String s = br.readLine();
            if (s == null)
                break;
            logger.info("[" + name + "] " + s);
        }
        is.close();
    } catch (Exception ex) {
        logger.error("Problem reading stream " + name + "... :" + ex);

    }
}

}

4

1 に答える 1

0

これが問題だとは思いませんが、実行方法を次のように変更してみてください。

try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        do {
            String s = br.readLine();
            if (s != null)
              logger.info("[" + name + "] " + s);
        } while (s != null);
        is.close();
    } catch (Exception ex) {
        logger.error("Problem reading stream " + name + "... :" + ex);

    }
于 2013-02-04T12:25:10.877 に答える