--- old/test/java/rmi/testlibrary/JavaVM.java 2014-02-06 15:56:38.000000000 +0800 +++ new/test/java/rmi/testlibrary/JavaVM.java 2014-02-06 15:56:38.000000000 +0800 @@ -26,6 +26,9 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.StringTokenizer; +import java.util.concurrent.TimeoutException; +import java.util.logging.Level; +import java.util.logging.Logger; /** * RMI regression test utility class that uses Runtime.exec to spawn a @@ -172,6 +175,47 @@ return status; } + /** + * Causes the current thread to wait the subprocess to exit, if necessary, + * until the subprocess represented by this Process object has terminated, + * or the specified waiting time elapses. + * @param timeout the maximum milliseconds to wait + * @return true if the JavaVM has exited and false if the waiting time + * elapsed before the subprocess has exited. + * @throws InterruptedException if the current thread is interrupted + * while waiting. + * @throws TimeoutException if subprocess does not end after timeout + * milliseconds passed + */ + public int waitFor(long timeout) + throws InterruptedException, TimeoutException { + long startTime = System.currentTimeMillis(); + long rem = timeout; + + do { + try { + int status = vm.exitValue(); + outPipe.join(); + errPipe.join(); + return status; + } catch (IllegalThreadStateException ex) { + if (rem > 0) { + Thread.sleep(Math.min(rem, 100)); + } + } + rem = timeout - (System.currentTimeMillis() - startTime); + } while (rem > 0); + new Thread(){ + @Override + public void run() { + try { + JavaVM.this.waitFor(); + } catch (InterruptedException ignore) {} + } + }.start(); + throw new TimeoutException(); + } + /** * Starts the subprocess, waits for it to exit, and returns its exit status. */