正确使用ProcessBuilder [英] Correct Usage of ProcessBuilder

查看:158
本文介绍了正确使用ProcessBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究之后我注意到使用java的ProcessBuilder的正确方法是生成另外两个线程来管理新创建的进程的stdout / stderr,这样它就不会挂起,如下所示:
javaworld文章

After researching I have noticed that the "correct" way to use java's ProcessBuilder is to spawn two other threads to manage gobbling up the stdout/stderr of the newly created process so that it doesn't hang as is shown here : javaworld article

但是这让我想知道2个问题 -
1.)为什么需要单独的进程而不是让父进程吞噬stdout和那么顺序stderr?

But this has left me wondering about 2 questions- 1.) Why exactly are seperate processes needed instead of having the parent process gobble up the stdout and then sequentially the stderr?

2。)另外,如果你要将流重定向到两者都转到stdout只是让父进程吞下了stdout流,然后不必担心死锁?

2.) In addition, if you were to redirect the streams to both go to stdout would it be acceptable to just have the parent process swallow the stdout stream, and then not have to worry about deadlocks?

推荐答案

请注意你的条款。 主题不是流程


  1. 因为孩子可以写入两者并且当 stderr的缓冲区已满(孩子等待父母阅读 stderr ,父等待孩子关闭 stdout )。

  1. Because the child could write to both and you would get a deadlock when the buffer for stderr is full (child waits for parent to read stderr, parent waits for child to close stdout).

不。如果子进程还需要 stdin ,那么你必须在主线程中处理 stdin 并通过以下方式读取合并的输出流一个额外的线程,或者你可能再次遇到死锁(孩子等待父母读取输出流,父母等待孩子读取 stdin 上的数据)。

No. If the child process also needs stdin, then you must handle stdin in your main thread and read the merged output streams via an extra thread or you could have deadlocks again (child waits for parent to read the output stream and the parent waits for the child to read the data on stdin).

这篇关于正确使用ProcessBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆