changeset 10488:7357c0e9088d

8064932: java/lang/ProcessBuilder/Basic.java: waitFor didn't take long enough Reviewed-by: dholmes, martin
author rriggs
date Wed, 19 Nov 2014 21:22:22 -0500
parents 562ef02d0146
children bed86c103348 aafe81a686a2
files src/solaris/classes/java/lang/UNIXProcess.java src/windows/classes/java/lang/ProcessImpl.java
diffstat 2 files changed, 24 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/classes/java/lang/UNIXProcess.java	Wed Nov 19 21:28:26 2014 -0500
+++ b/src/solaris/classes/java/lang/UNIXProcess.java	Wed Nov 19 21:22:22 2014 -0500
@@ -405,14 +405,17 @@
         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;
     }
 
--- a/src/windows/classes/java/lang/ProcessImpl.java	Wed Nov 19 21:28:26 2014 -0500
+++ b/src/windows/classes/java/lang/ProcessImpl.java	Wed Nov 19 21:22:22 2014 -0500
@@ -461,11 +461,21 @@
         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 ;
 
-        waitForTimeoutInterruptibly(handle, msTimeout);
-        if (Thread.interrupted())
-            throw new InterruptedException();
+        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);
+
         return (getExitCodeProcess(handle) != STILL_ACTIVE);
     }