简体   繁体   English

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

[英]why does java's inputstream.close() block?

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. 我的Java程序使用ProcessBuilder(redirectErrorStream设置为true)并且有一个循环来运行进程的inputstream的read方法,该方法是阻塞的。 The external program I'm calling then comes to a stop waiting for input and stdin. 我正在调用的外部程序停止等待输入和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? 这是不是由(在一个单独的线程中)调用进程的destroy方法,并调用inputstream的close方法来阻止read方法阻塞,以便我的初始线程可以结束它的生命?

For some reason process.getInputStream().close() blocks. 由于某种原因, process.getInputStream().close()阻止。 From the JavaDoc I don't see why this can happen. 从JavaDoc我不明白为什么会发生这种情况。 Furthermore, I don't understand why the javadoc says "The close method of InputStream does nothing." 此外,我不明白为什么javadoc说“InputStream的close方法什么都不做”。 ( link to javadoc ) Could someone explain this? 链接到javadoc )有人可以解释一下吗?

Thanks :-) 谢谢 :-)

Regarding the blocking behavior, there is a known issue in Java that can cause deadlock when communicating with another process. 关于阻塞行为,Java中存在一个已知问题,当与另一个进程通信时会导致死锁。 I can't tell if this is what you're seeing but it's worth looking into. 我不知道这是否是你所看到的,但值得研究。 The document for java.lang.Process says: java.lang.Process的文档说:

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. 由于某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,因此无法及时写入输入流或读取子进程的输出流可能导致子进程阻塞甚至死锁。

For some reason process.getInputStream().close() blocks. 由于某种原因,process.getInputStream()。close()阻止。 From the JavaDoc I don't see why this can happen. 从JavaDoc我不明白为什么会发生这种情况。 Furthermore, I don't understand why the javadoc says "The close method of InputStream does nothing." 此外,我不明白为什么javadoc说“InputStream的close方法什么都不做”。 (link to javadoc) Could someone explain this? (链接到javadoc)有人可以解释一下吗?

If you look at the Javadoc, you'll see that InputStream an abstract class. 如果你看一下Javadoc,你会发现InputStream是一个抽象类。 Subclasses that extend InputStream are expected to override the close() method (should it be needed). 扩展InputStream的子类应该覆盖close()方法(如果需要)。 Clearly the InputStream subclass that you're using does something in the close method. 显然,您正在使用的InputStream子类在close方法中执行了某些操作。

Adding onto what jdigital wrote, check this article . 添加到jdigital写的内容,请查看此文章 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. 它处理Runtime.exec()方法,并且在Java 5中引入了ProcessBuilder ,但在我看来,讨论可以推广到系统进程。

I think I figured this out. 我想我想出来了。 Obviously it is important to call process.getOutputStream().close() before process.getInputStream().close() and process.getErrorStream().close(). 显然,在process.getInputStream()。close()和process.getErrorStream()。close()之前调用process.getOutputStream()。close()是很重要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM