Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java Process.waitfor is a blocking call

I want to be able to kick off an external process from within the JVM and reach on its completion.

I could use the ProcessBuilder to create a Process and then do Process.waitFor() to wait for its completion. However, this a blocking call and simply wastes thread resources.

It would make better sense to do this via an event handler and reactive programming. One would think that the JVM could register some sort of a listener with the OS to listen for process completion events, and relay that back to the program.

Does such a mechanism/ API exist? Any alternatives or libraries that achieve this?

like image 612
vishr Avatar asked Dec 07 '12 16:12

vishr


1 Answers

Use the NuProcess library if you only need to support Windows, MacOS X, and Linux. It provides non-blocking access to external processes using a callback model -- including I/O and program termination. Disclosure: I am the author of said library.

like image 160
brettw Avatar answered Sep 21 '22 17:09

brettw