Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Non-blocking Read [closed]

I'm trying to read in from two different input streams (stdin and stderr of a child application) and print them in one combine frame. I've never done this before, and am wondering about best practices.

I can spawn two threads and let them both block on the input stream. Is there a better way?

Thanks!

like image 595
Jim Avatar asked Dec 03 '25 17:12

Jim


1 Answers

Your approach is fine, though if you are trying to combine two streams you may get issues with the output/error getting jumbled together.

Perhaps check out Apache commons-exec which is a decent library for handling process invocation and output/error capture. Under the hood it is doing exactly as you suggest, forking threads which read InputStream(s).

like image 60
Mike Q Avatar answered Dec 06 '25 10:12

Mike Q



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!