简体   繁体   English

java.io.InputStreamReader.ready()阻止执行

[英]java.io.InputStreamReader.ready() blocks execution

i have one unexpected problem. 我有一个意外的问题。 in my project i use object of type java.io.bufferedReader to read some data. 在我的项目中,我使用java.io.bufferedReader类型的对象读取一些数据。 it contains readLine() method which reads next line of text from the source. 它包含readLine()方法,该方法从源代码中读取下一行文本。

but the problem with this method is that if source is not ready it blocks execution of the thread which called this method until source has something to read. 但是此方法的问题在于,如果源未准备好,它将阻止调用该方法的线程执行,直到源需要读取某些内容为止。

fortunately, bufferedReader has ready() method which tells me weather source is ready or not, so i can call it once and a while to see weather there is something to read from the source, and if not to continue doing other jobs. 幸运的是,bufferedReader具有ready()方法,该方法可以告诉我天气源是否准备就绪,因此我可以不时地调用它以查看天气是否有需要从源中读取的内容,以及是否不继续进行其他工作。 it mostly works fine, but today i noticed something strange. 它通常可以正常工作,但是今天我注意到了一些奇怪的事情。

in some rare cases when i call ready() method ready method itself blocks the execution. 在极少数情况下,当我调用ready()方法时,ready方法本身会阻止执行。

so i am surprised since ready() method is designed to return true only if it is absolutely sure read* method would not block. 所以我很惊讶,因为ready()方法设计为仅在绝对确定read *方法不会阻塞的情况下才返回true。

as pointed out by "erwin" below, the real problem is not in java.io.BufferedReader but in underlying reader. 如下面的“ erwin”所指出的,真正的问题不在java.io.BufferedReader中,而在底层阅读器中。

to construct buffered reader i use java.io.InputStreamReader. 构造缓冲的阅读器,我使用java.io.InputStreamReader。

even if i do not create BufferedReader, but just use InputStreamReader directly, if i call ready() method, call to that method blocks. 即使我不创建BufferedReader,而是直接使用InputStreamReader,如果我调用ready()方法,则对该方法块的调用也是如此。

so, how is possible that ready() method blocks, and how to avoid it? 那么,ready()方法怎么可能阻塞,以及如何避免呢?

thank you. 谢谢。

The ready() method in BufferedReader never blocks by itself - you can easily check that in the source code of BufferedReader. BufferedReaderready()方法永远不会自己阻塞-您可以在BufferedReader的源代码中轻松检查一下。

The only case in which the ready() method of BufferedReader blocks is, if the ready() method in the underlying Reader blocks (the one that you passed into the constructor of BufferedReader). 如果底层Reader块中的ready()方法(传递给BufferedReader的构造函数的方法ready() ,则BufferedReader块的ready()方法是唯一的情况。 So your question should be changed to "[my real Reader class] read() method blocks execution". 因此,您的问题应更改为“ [我的实际Reader类] read()方法阻止执行”。

For instance, if your underlying reader is a PipedReader , there could be an issue because the ready() and the read() methods are both synchronized . 例如,如果您的基础阅读器是PipedReader ,则可能存在问题,因为ready()read()方法都已synchronized If one Thread is blocked for reading from a PipedReader and another thread uses PipedReader.ready() to check if it can read, then the second thread also blocks until the read() on the first thread completes. 如果一个线程被阻止从PipedReader读取,而另一个线程使用PipedReader.ready()检查是否可以读取,则第二个线程也将阻塞,直到第一个线程的read()完成。

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

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