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).
发布评论
评论(3)
您的方法很好,但如果您尝试合并两个流,您可能会遇到输出/错误混杂在一起的问题。
也许看看 Apache commons-exec 这是一个不错的库,用于处理进程调用和输出/错误捕获。在幕后,它完全按照您的建议进行,分叉读取 InputStream 的线程。
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.
我认为最好的策略是使用 java.nio 包中的类。查看此示例。
I think your best strategy would be using classes in java.nio package. Check this example out.