开发者

Java Non-blocking Read [closed]

开发者 https://www.devze.com 2023-01-09 04:04 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing
Closed. This question is opinion-based. It is not currently accepting answers.

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 question

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消