Name: nt126004 Date: 09/20/2001
java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)
Reading through the code to BufferedInputStream, I notice that close is implemented as follows:
public void close() throws IOException {
if (in == null)
return;
in.close();
in = null;
buf = null;
}
There are many problems with this code:
1) Close is accessing/modifying the shared variables in and buf outside of a
synchronized block
2) Two threads could call close simultaneously. One thread can set 'in' to null
right before the second thread reads 'in' to call in.close()
3) Setting in to null could cause other methods to throw a NullPointerException
instead of an IOException.
The answer to this problem is not to make close synchronized. This means that
close could hang indefinitely if some other thread were hanging in read. Probably the best thing to do is to get rid of the in = null and buf = null.
This will require rewriting ensureOpen too.
(Review ID: 132314)
======================================================================
- duplicates
-
JDK-4728096 java.io.BufferedInputStream has no synchronization on close operation
- Resolved