How to monitor external process ran by ProcessBuilder?

前端 未结 3 1742
眼角桃花
眼角桃花 2020-12-19 08:47

This should be rather simple, but I don\'t see anything helpful in JavaDocs.

What I need is to run some external process from my Java code and then be able to monito

3条回答
  •  轮回少年
    2020-12-19 09:15

    Start a new Thread which calls Process.waitFor() and sets a flag when that call returns. then, you can check that flag whenever you want to see if the process has completed.

    public class ProcMon implements Runnable {
    
      private final Process _proc;
      private volatile boolean _complete;
    
      public boolean isComplete() { return _complete; }
    
      public void run() {
        _proc.waitFor();
        _complete = true;
      }
    
      public static ProcMon create(Process proc) {
        ProcMon procMon = new ProcMon(proc);
        Thread t = new Thread(procMon);
        t.start();
        return procMon;
      }
    }
    

    (some boilerplate omitted).

提交回复
热议问题