< prev index next >

src/java.base/share/classes/java/io/FileInputStream.java

Print this page
rev 15897 : 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
Reviewed-by: alanb, shade, plevart

@@ -24,11 +24,10 @@
  */
 
 package java.io;
 
 import java.nio.channels.FileChannel;
-import java.util.concurrent.atomic.AtomicBoolean;
 import sun.nio.ch.FileChannelImpl;
 
 
 /**
  * A <code>FileInputStream</code> obtains input bytes

@@ -58,11 +57,13 @@
      */
     private final String path;
 
     private volatile FileChannel channel;
 
-    private final AtomicBoolean closed = new AtomicBoolean(false);
+    private final Object closeLock = new Object();
+
+    private volatile boolean closed;
 
     /**
      * Creates a <code>FileInputStream</code> by
      * opening a connection to an actual file,
      * the file named by the path name <code>name</code>

@@ -311,14 +312,19 @@
      *
      * @revised 1.4
      * @spec JSR-51
      */
     public void close() throws IOException {
-        if (!closed.compareAndSet(false, true)) {
-            // if compareAndSet() returns false closed was already true
+        if (closed) {
+            return;
+        }
+        synchronized (closeLock) {
+            if (closed) {
             return;
         }
+            closed = true;
+        }
 
         FileChannel fc = channel;
         if (fc != null) {
            fc.close();
         }

@@ -368,11 +374,11 @@
         if (fc == null) {
             synchronized (this) {
                 fc = this.channel;
                 if (fc == null) {
                     this.channel = fc = FileChannelImpl.open(fd, path, true, false, this);
-                    if (closed.get()) {
+                    if (closed) {
                         try {
                             fc.close();
                         } catch (IOException ioe) {
                             throw new InternalError(ioe); // should not happen
                         }
< prev index next >