I have two threads in Java.
First thread is closing a bufferedreader (br.close()
)
When the second thread does a read on the same reader I get an IOException (Stream Closed)
I get this exception even if I use br.ready()
Is there a way to know if the stream is already closed?
The method ready()
will throw an exception if closed. But even if you added a closed check method, so long as the lock is released between calls (which it is for BufferedReader
), the reader might be closed by the time you read it.
I see three options:
BufferedReader
that extends close()
to set your own variable that
can be used to check to see if the reader is closed. This also requires overriding a lot of
methods to do whatever the behavior you want with a closed reader if you want it do something beside throw the IOException
.ready()
and read()
calls into the same synchronized block.