Java Non-blocking Read [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
开发者_开发问答 Improve this questionI'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!
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).
having two threads is the only solution.
I think your best strategy would be using classes in java.nio package. Check this example out.
精彩评论