timing out after Runtime.getRuntime().exec(command)

R

Robert Mark Bram

Hi All,

I have some code like this:

Process pprocess = Runtime.getRuntime().exec(ftfCommand);
process.waitFor();

How do I make a timeout occur after x milliseconds?

Rob
:)
 
S

Stefan Schulz

Hi All,

I have some code like this:

Process pprocess = Runtime.getRuntime().exec(ftfCommand);
process.waitFor();

How do I make a timeout occur after x milliseconds?

Warning: untested from the top of my head

void foo(String ftfCommand){
class Waker implements Runnable {
private Thread t;
Waker(Thread toWake){
t = toWake;
}
public void run(){
synchronized(this){
this.wait(TIMEOUT);
}

t.interrupt();
}
}

new Thread(new Waker(Thread.currentThread())).start();
Process pprocess = Runtime.getRuntime().exec(ftfCommand);

try {
pprocess.waitFor();
} catch (InterruptedException e){
// timed out
}

How do I make a timeout occur after x milliseconds?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top