Java进程exitValue错误



在linux中为java进程对象调用exitValue()时,我得到了"0"值,但偶尔会有子线程(错误和输出流读取器)没有完成,并陷入联接。进程exitValue()的"0"值难道不应该保证所有子进程都成功终止吗?

private class ReadStdoutThread extends Thread {
        private Process m_prc;
        private StringBuffer m_sb;
        public ReadStdoutThread(Process prc, StringBuffer sb) {
            m_prc = prc;
            m_sb = sb;
        }
        public void run() {
            BufferedReader stdout =
                new BufferedReader(new InputStreamReader(m_prc.getInputStream()));
            String line = null;
            try {
                while ((line = stdout.readLine()) != null) {
                    System.out.println("Stdout: " + line);
                    m_sb.append(line + "n");
                }
                stdout.close();
            } catch (IOException e) {
                System.out.println(e.toString());
                return;
            }
        }
    }
    private class ReadStderrThread extends Thread {
        private Process m_prc;
        private StringBuffer m_sb;
        public ReadStderrThread(Process prc, StringBuffer sb) {
            m_prc = prc;
            m_sb = sb;
        }
        public void run() {
            BufferedReader stderr =
                new BufferedReader(new InputStreamReader(m_prc.getErrorStream()));
            String line = null;
            try {
                while ((line = stderr.readLine()) != null) {
                    System.out.println("Stderr: " + line);
                    m_sb.append(line + "n");
                }
                stderr.close();
            } catch (IOException e) {
                System.out.println(e.toString());
                return;
            }
        }
    }
    public static String runCmd(String cmd, long timeoutMS) throws IOException,
                                                                   InterruptedException {
        Process prc = Runtime.getRuntime().exec(cmd);
        long startTimeMS = System.currentTimeMillis();
        boolean isRunning = true;
        System.out.println("Command has started.");
        StringBuffer sb = new StringBuffer();
        ReadStdoutThread ot = new HostCommand().new ReadStdoutThread(prc, sb);
        ReadStderrThread et = new HostCommand().new ReadStderrThread(prc, sb);
        ot.start();
        et.start();
        if (timeoutMS == 0) {
            System.out.println("Thread will wait until command is completed.");
            prc.waitFor();
        } else {
            System.out.println("Command timeout (ms): " + timeoutMS);
            synchronized (prc) {
                int n = -1;
                while (isRunning) {
                    prc.wait(1000);
                    try {
                        n = prc.exitValue();
                        System.out.println("Command has completed with value: " +
                                           n);
                        m_processExitValue = n;
                        isRunning = false;
                    } catch (IllegalThreadStateException e) {
                        // command is still running
                        isRunning = true;
                    }
                    if ((System.currentTimeMillis() - startTimeMS >
                         timeoutMS) && isRunning) {
                        System.out.println("Timeout has reached, and command is still running.  Command will be interrupted.");
                        prc.destroy();
                        m_processExitValue = n;
                        isRunning = false;
                    }
                }
            }
        }
        try {
            ot.join(timeoutMS);
        }
        catch(InterruptedException e) {
            throw e;
        }
        try {
            et.join(timeoutMS);
        }
        catch(InterruptedException e) {
            throw e;
        }
        return sb.toString();
    }

根据文档,如果任何子流程尚未终止,该方法应返回IllegalThreadStateException,否则将返回0,表示正常终止。

来源:https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

不能保证ReadStdoutThread和ReadStderrThread已经完成执行。Process的waitFor API和您派生的两个独立线程之间不同步,这两个线程用于从Process读取标准输入和错误流。调用ot.start();et.start();之后需要使用:ot.join();et.join();。基本上,join应该在调用Process.waitFor() 之前调用

相关内容

  • 没有找到相关文章

最新更新