Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java native process timeout

Tags:

java

process

At the moment I execute a native process using the following:

java.lang.Process process = Runtime.getRuntime().exec(command);  int returnCode = process.waitFor(); 

Suppose instead of waiting for the program to return I wish to terminate if a certain amount of time has elapsed. How do I do this?

like image 902
deltanovember Avatar asked Aug 07 '09 23:08

deltanovember


People also ask

What is runtime getRuntime () exec?

In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method.

What is process WaitFor ()?

WaitFor() Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. WaitFor(Int64, TimeUnit) Causes the current thread to wait, if necessary, until the subprocess represented by this Process object has terminated, or the specified waiting time elapses.

How do I get PID in Java?

To get the current thread's PID, just call Affinity. getAffinityImpl(). getProcessId() .

What is the Java process?

Process provides control of native processes started by ProcessBuilder. start and Runtime. exec. The class provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.


2 Answers

All other responses are correct but it can be made more robust and efficient using FutureTask.

For example,

private static final ExecutorService THREAD_POOL      = Executors.newCachedThreadPool();  private static <T> T timedCall(Callable<T> c, long timeout, TimeUnit timeUnit)     throws InterruptedException, ExecutionException, TimeoutException {     FutureTask<T> task = new FutureTask<T>(c);     THREAD_POOL.execute(task);     return task.get(timeout, timeUnit); }  final java.lang.Process[] process = new Process[1]; try {     int returnCode = timedCall(new Callable<Integer>() {         public Integer call() throws Exception {             process[0] = Runtime.getRuntime().exec(command);              return process[0].waitFor();         }     }, timeout, TimeUnit.SECONDS); } catch (TimeoutException e) {     process[0].destroy();     // Handle timeout here } 

If you do this repeatedly, the thread pool is more efficient because it caches the threads.

like image 82
ZZ Coder Avatar answered Oct 03 '22 19:10

ZZ Coder


If you're using Java 8 or later (API 26 or later for Android) you could simply use the waitFor with timeout:

Process p = ... if(!p.waitFor(1, TimeUnit.MINUTE)) {     //timeout - kill the process.      p.destroy(); // consider using destroyForcibly instead } 
like image 28
Aleksander Blomskøld Avatar answered Oct 03 '22 20:10

Aleksander Blomskøld