java native Process timeout

前端 未结 6 1455
不思量自难忘°
不思量自难忘° 2020-11-27 14:31

At the moment I execute a native process using the following:

java.lang.Process process = Runtime.getRuntime().exec(command); 
int returnCode = process.waitF         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 15:22

    You'd need a 2. thread that interrupts the thread that calls .waitFor(); Some non trivial synchronization will be needed to make it robust, but the basics are:

    TimeoutThread:

     Thread.sleep(timeout);
     processThread.interrupt();
    

    ProcessThread:

      try {
          proc.waitFor(); 
        } catch (InterruptedException e) {
           proc.destroy();
        }
    

提交回复
热议问题