From 53906a2cb4cc06f0817d97be68a405c60edf5efa Mon Sep 17 00:00:00 2001 From: Roger Riggs Date: Wed, 19 Nov 2014 21:22:22 -0500 Subject: [PATCH] 8064932: java/lang/ProcessBuilder/Basic.java: waitFor didn't take long enough Reviewed-by: dholmes, martin --- .../unix/classes/java/lang/UNIXProcess.java | 17 ++++++++++------- .../windows/classes/java/lang/ProcessImpl.java | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/jdk/src/java.base/unix/classes/java/lang/UNIXProcess.java b/jdk/src/java.base/unix/classes/java/lang/UNIXProcess.java index 85f15407c8f..d3adac90cd2 100644 --- a/jdk/src/java.base/unix/classes/java/lang/UNIXProcess.java +++ b/jdk/src/java.base/unix/classes/java/lang/UNIXProcess.java @@ -406,14 +406,17 @@ final class UNIXProcess extends Process { if (hasExited) return true; if (timeout <= 0) return false; - long timeoutAsNanos = unit.toNanos(timeout); - long startTime = System.nanoTime(); - long rem = timeoutAsNanos; + long remainingNanos = unit.toNanos(timeout); + long deadline = System.nanoTime() + remainingNanos; - while (!hasExited && (rem > 0)) { - wait(Math.max(TimeUnit.NANOSECONDS.toMillis(rem), 1)); - rem = timeoutAsNanos - (System.nanoTime() - startTime); - } + do { + // Round up to next millisecond + wait(TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L)); + if (hasExited) { + return true; + } + remainingNanos = deadline - System.nanoTime(); + } while (remainingNanos > 0); return hasExited; } diff --git a/jdk/src/java.base/windows/classes/java/lang/ProcessImpl.java b/jdk/src/java.base/windows/classes/java/lang/ProcessImpl.java index 02e0c7bf346..ec90ea0da3a 100644 --- a/jdk/src/java.base/windows/classes/java/lang/ProcessImpl.java +++ b/jdk/src/java.base/windows/classes/java/lang/ProcessImpl.java @@ -461,11 +461,21 @@ final class ProcessImpl extends Process { if (getExitCodeProcess(handle) != STILL_ACTIVE) return true; if (timeout <= 0) return false; - long msTimeout = unit.toMillis(timeout); + long remainingNanos = unit.toNanos(timeout); + long deadline = System.nanoTime() + remainingNanos ; + + do { + // Round up to next millisecond + long msTimeout = TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L); + waitForTimeoutInterruptibly(handle, msTimeout); + if (Thread.interrupted()) + throw new InterruptedException(); + if (getExitCodeProcess(handle) != STILL_ACTIVE) { + return true; + } + remainingNanos = deadline - System.nanoTime(); + } while (remainingNanos > 0); - waitForTimeoutInterruptibly(handle, msTimeout); - if (Thread.interrupted()) - throw new InterruptedException(); return (getExitCodeProcess(handle) != STILL_ACTIVE); }