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

Discussion in 'Java' started by Robert Mark Bram, Jul 15, 2005.

  1. 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
    :)
     
    Robert Mark Bram, Jul 15, 2005
    #1
    1. Advertising

  2. On Thu, 14 Jul 2005 23:41:03 -0700, Robert Mark Bram wrote:

    > 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?


    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 15, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zsolt Koppany
    Replies:
    2
    Views:
    7,299
    SaintMagoo
    Mar 5, 2011
  2. MARTIN Herve \(EXT\)
    Replies:
    1
    Views:
    1,513
    Anthony Borla
    Nov 27, 2003
  3. paul brown
    Replies:
    3
    Views:
    5,644
    deepa gr
    Feb 17, 2009
  4. Dave Neuendorf
    Replies:
    5
    Views:
    6,786
    Anthony Borla
    Feb 25, 2005
  5. Lionel
    Replies:
    36
    Views:
    2,234
    Nigel Wade
    Dec 5, 2007
Loading...

Share This Page