john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

concurrency timeout kill process

public static int executeCommandLine(final String commandLine,
                                     final boolean printOutput,
                                     final boolean printError,
                                     final long timeout)
  throws IOException, InterruptedException, TimeoutException
{
  Runtime runtime = Runtime.getRuntime();
  Process process = runtime.exec(commandLine);
  /* Set up process I/O. */
  ...
  Worker worker = new Worker(process);
  worker.start();
  try {
    worker.join(timeout);
    if (worker.exit != null)
      return worker.exit;
    else
      throw new TimeoutException();
  } catch(InterruptedException ex) {
    worker.interrupt();
    Thread.currentThread().interrupt();
    throw ex;
  } finally {
    process.destroy();
  }
}

private static class Worker extends Thread {
  private final Process process;
  private Integer exit;
  private Worker(Process process) {
    this.process = process;
  }
  public void run() {
    try {
      exit = process.waitFor();
    } catch (InterruptedException ignore) {
      return;
    }
  }
}

  • « Xen virtualization ubuntu
  • command line process runtime system cmd »

Published

Jan 1, 2012

Category

java

~81 words

Tags

  • concurrency 10
  • java 252
  • kill 4
  • process 6
  • timeout 2