Wait for process to terminate with a better way

Refine #1122.
This commit is contained in:
Mygod
2017-03-01 22:28:35 +08:00
parent b6667b9e0f
commit 86773085c3
3 changed files with 35 additions and 19 deletions
@@ -54,14 +54,19 @@ public class JniHelper {
? new ErrnoException("kill", errno) : new Exception("kill failed: " + errno);
}
@Deprecated // Use Process.destroy() since API 24
public static int waitpidCompat(Process process) throws Exception {
@Deprecated // only implemented for before API 24
public static boolean waitForCompat(Process process, long millis) throws Exception {
if (Build.VERSION.SDK_INT >= 24) throw new UnsupportedOperationException("Never call this method in OpenJDK!");
return waitpid(process);
final Object mutex = getExitValueMutex(process);
synchronized (mutex) {
if (getExitValue(process) == null) mutex.wait(millis);
return getExitValue(process) != null;
}
}
private static native int sigterm(Process process);
private static native int waitpid(Process process);
private static native Integer getExitValue(Process process);
private static native Object getExitValueMutex(Process process);
public static native int sendFd(int fd, String path);
public static native void close(int fd);
}