为什么java的inputstream.close()会阻塞?

发布于 2024-07-15 06:22:53 字数 524 浏览 3 评论 0原文

我的 Java 程序使用 ProcessBuilder(redirectErrorStream 设置为 true),并有一个循环运行进程的输入流的 read 方法,该方法是阻塞的。 然后我调用的外部程序停止等待输入和标准输入。 我现在想终止该进程。 这不是通过(在单独的线程中)调用进程的 destroy 方法,并调用输入流的 close 方法来阻止 read 方法再阻塞来完成的,以便我的初始线程可以结束其生命吗?

由于某种原因 process.getInputStream().close() 阻塞。 从 JavaDoc 我不明白为什么会发生这种情况。 此外,我不明白为什么 javadoc 说“InputStream 的 close 方法什么也不做”。 (链接到 javadoc )有人可以解释一下吗?

谢谢 :-)

My Java program uses ProcessBuilder (with redirectErrorStream set true) and has a loop that runs the processes's inputstream's read method, which is blocking. The external program I'm calling then comes to a stop waiting for input and stdin. I now want to kill the process. Is this not done by (in a seperate thread) calling the process's destroy method, and calling the inputstream's close method to stop the read method from blocking anymore, so that my initial thread can end its life?

For some reason process.getInputStream().close() blocks. From the JavaDoc I don't see why this can happen. Furthermore, I don't understand why the javadoc says "The close method of InputStream does nothing." (link to javadoc) Could someone explain this?

Thanks :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

つ低調成傷 2024-07-22 06:22:53

关于阻塞行为,Java 中有一个已知问题,在与另一个进程通信时可能会导致死锁。 我不知道这是否是您所看到的,但值得研究。 java.lang.Process 说:

因为仅限某些本机平台
提供有限的缓冲区大小
标准输入和输出流,
未能及时写入输入
流或读取输出流
子进程可能会导致
子进程阻塞,甚至
陷入僵局。

Regarding the blocking behavior, there is a known issue in Java that can cause deadlock when communicating with another process. I can't tell if this is what you're seeing but it's worth looking into. The document for java.lang.Process says:

Because some native platforms only
provide limited buffer size for
standard input and output streams,
failure to promptly write the input
stream or read the output stream of
the subprocess may cause the
subprocess to block, and even
deadlock.

苏辞 2024-07-22 06:22:53

由于某种原因
process.getInputStream().close()
块。 从 JavaDoc 我没有看到
为什么会发生这种情况。 此外,我
不明白为什么javadoc说
“InputStream 的 close 方法确实
什么都没有。”(链接到 javadoc)可以
有人解释一下吗?

如果您查看 Javadoc,您会发现 InputStream 是一个抽象类。 扩展 InputStream 的子类应重写 close() 方法(如果需要)。 显然,您正在使用的 InputStream 子类在 close 方法中执行了某些操作。

For some reason
process.getInputStream().close()
blocks. From the JavaDoc I don't see
why this can happen. Furthermore, I
don't understand why the javadoc says
"The close method of InputStream does
nothing." (link to javadoc) Could
someone explain this?

If you look at the Javadoc, you'll see that InputStream an abstract class. Subclasses that extend InputStream are expected to override the close() method (should it be needed). Clearly the InputStream subclass that you're using does something in the close method.

锦爱 2024-07-22 06:22:53

添加 jdigital 编写的内容,请检查此 文章。 它处理 Runtime.exec() 方法,并且 ProcessBuilder 是在 Java 5 中引入的,但在我看来,讨论通常可以推断到系统进程。

Adding onto what jdigital wrote, check this article. It deals with Runtime.exec() method, and ProcessBuilder was introduced in Java 5, but it seems to me the discussion can be extrapolated to system processes in general.

总攻大人 2024-07-22 06:22:53

我想我已经明白了这一点。 显然,在 process.getInputStream().close() 和 process.getErrorStream().close() 之前调用 process.getOutputStream().close() 很重要。

I think I figured this out. Obviously it is important to call process.getOutputStream().close() before process.getInputStream().close() and process.getErrorStream().close().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文