public static ProcessTimer createTimer(TimerListener listener, double timeInSeconds) { ProcessTimer timer = new ProcessTimer(listener, timeInSeconds); timers.add(timer); timer.start(); logAliveTimersCount(); return timer; }
public static void removeTimer(ProcessTimer timer) { if (timer != null) { timer.close(); timers.remove(timer); } logAliveTimersCount(); }
/** * Waits until the process is terminated or the timeout is elapsed. * * @return The exit code of the process */ public int waitFor() { int exitCode = -1; if (process != null) { try { exitCode = process.waitFor(); logger.debug("Closing errorStream..."); errorStream.close(); logger.debug("Closing outputStream..."); outputStream.close(); } catch (InterruptedException e) { e.printStackTrace(); } ProcessTimer.removeTimer(timer); logger.debug("TProcess ended: exitCode = " + exitCode); } else { ErrorsAndWarnings.addError("Cannot call waitFor() on a null TProcess"); } return exitCode; }
/** Destroys the process */ public void stop() { ProcessTimer.removeTimer(timer); try { Thread.sleep(StreamListener.PRINTING_PERIOD_IN_MILLIS); } catch (InterruptedException e) { e.printStackTrace(); } if (errorStream != null) { logger.debug("Closing errorStream..."); errorStream.close(); } if (outputStream != null) { logger.debug("Closing outputStream..."); outputStream.close(); } if (process != null) { logger.debug("destroying TProcess..."); process.destroy(); logger.debug("TProcess destroyed"); } }
/** * Creates a TProcess, composed by the Process and one timer. Call waitFor() to wait until the * process returns. * * @param processToManage The process to destroy after timeout * @param timeoutInSeconds The timeout in seconds (precision is millisecond) */ public TProcess(Process processToManage, double timeoutInSeconds) { process = processToManage; timer = ProcessTimer.createTimer(this, timeoutInSeconds); }