public int execute(Process process, long timeout, TimeUnit unit) throws TimeoutException,
        InterruptedException {
      long timeRemaining = unit.toMillis(timeout);
      long startTime = System.currentTimeMillis();

      while (timeRemaining > 0) {
        try {
          return process.exitValue();
        } catch (IllegalThreadStateException ex) {
          Thread.sleep(Math.min(timeRemaining, 500));
        }

        long timeElapsed = System.currentTimeMillis() - startTime;
        timeRemaining = unit.toMillis(timeout) - timeElapsed;
      }

      process.destroy();

      throw new TimeoutException();
    }
Executes the specified process within the supplied timeout value. If the
process terminates normally within the timeout period, then the exit code
is returned. If the process did not succeed within the specified timeout
value, then a {@link TimeoutException} is thrown.
<p/>
If the process did not finish within the specified timeout, then the
process will be destroyed via {@link Process#destroy()}.

@param process
         the process to execute (not {@code null}).
@param timeout
         the timeout to apply to the process.
@param unit
         the time units for the timeout
@return the exit code of the process
@throws TimeoutException
          if the process did not finish within the specified time.
ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java