java native Process timeout

前端 未结 6 1462
不思量自难忘°
不思量自难忘° 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:04

    If you're using Java 8 or later (API 26 or later for Android) you could simply use the waitFor with timeout:

    Process p = ...
    if(!p.waitFor(1, TimeUnit.MINUTE)) {
        //timeout - kill the process. 
        p.destroy(); // consider using destroyForcibly instead
    }
    

提交回复
热议问题