• Post Reply Bookmark Topic Watch Topic
  • New Topic

ProcessBuilder API in java  RSS feed

 
Prasad Katti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi All,

When using the ProcessBuilder API in java to execute shell scripts, I have the following queries:

1. Is the start() API a blocking call?
2. how long will waitFor() wait?
3. Is there a way to specify a time out value to the waitFor() call [inherently avaliable]?

regards
Prasad
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which waitFor()? I think you mean the Process class instead. It tells you in the Process#waitFor() documentation what that method waits for. I had a look at the start() documentation and it doesn't say anything about blocking.

Before going anywhere near Process, I presume you will have read the classic article about it, which you should find by Googling: when Runtime.exec() won't.
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Prasad Katti wrote:
1. Is the start() API a blocking call?

Write a simple example and after start(), call Thread.sleep(100000);
During the threads sleeping period, press:
  • CTRL + break (WIN)
  • CTRL + \ (UNIX)

  • Now check which thread was put to sleep.
    In case it is a main, then you're dealing with a "blocking" code.
    Prasad Katti wrote: 2. how long will waitFor() wait?

    http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor%28%29
    Prasad Katti wrote: 3. Is there a way to specify a time out value to the waitFor() call [inherently avaliable]?

    Hm, I'd say no.


    Regards,
    Rok
     
    Rob Spoor
    Sheriff
    Posts: 21131
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is with a workaround: put the waitFor into a new thread which you then join with a timeout:
    The Runnable may need to be tweaked to allow the finished status to be passed back, and perhaps have better exception handling, but this is the basic idea.
    In fact, with Java 5.0 it may be easier with a combination of Callable<Integer> and Future<Integer>; the latter has a timed get() method. You just need an ExecutorService or CompletionService to turn the Callable into a Future.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!