在Java 1.7中保持并检查进程是否存在

时间:2015-08-07 01:10:14

标签: java performance

  • 如何确保Java 1.7中的进程(java.lang.Process)存活。在Java 1.8中,有isAlive()方法。如何在Java 1.7中完成。

谢谢!

3 个答案:

答案 0 :(得分:1)

可能等得太晚,但由于我遇到同样的问题,这是我的解决方案: 只需从Java 8中复制Process.isAlive()方法的实现:

public boolean isAlive() {
    try {
        exitValue();
        return false;
    } catch(IllegalThreadStateException e) {
        return true;
    }
}

答案 1 :(得分:0)

了解过程需要的背景。但总的来说,我们可以使用Threads with Executors框架。 Executors.newCachedThreadPool() 然后向其提交任务......

答案 2 :(得分:0)

我使用以下方法监视通过Swing应用程序启动的多个进程。它遵循@mastah提到的相同逻辑。看看它是否有帮助。

package snippet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ProcessMonitor extends Thread {

    private Process process;
    private int exitCode;

    public ProcessMonitor(Process process) {
        this.process = process;
        start();
    }

    @Override public void run() {
        try {
            exitCode = process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void setOutputStream(final OutputStream s) {
        new Thread(new Runnable() {
            @Override public void run() {
                InputStream is = process.getInputStream();
                int c ;
                try {
                    while((c = is.read()) >= 0) {
                        s.write(c);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void setErrorStream(final OutputStream s) {
        new Thread(new Runnable() {
            @Override public void run() {
                InputStream is = process.getErrorStream();
                int c ;
                try {
                    while((c = is.read()) >= 0) {
                        s.write(c);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public int getExitCode() {
        return exitCode;
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        if(args.length == 1) {
            System.err.println("In child process.. going to sleep for 1 second");
            Thread.sleep(1000);
            System.err.println("In child process.. done sleep exiting...");
            System.exit(-1);
        }
        String[] pbArgs = new String[] {
          "java", "-cp", System.getProperty("java.class.path"), ProcessMonitor.class.getName(), "arg"      
        };
        ProcessBuilder pb = new ProcessBuilder(pbArgs);
        pb.redirectErrorStream(true);
        System.out.println("Starting process: " + pb.command());
        final Process process = pb.start();
        ProcessMonitor pm = new ProcessMonitor(process);
        pm.setOutputStream(System.err);
        while (pm.isAlive()) {
            System.out.println("Process is still alive");
            Thread.sleep(1000);
        }
        System.out.println("Process exited with: " + pm.getExitCode());
    }

}