Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6631427

File{In,Out}putStream should replace use of ThreadLocal with AtomicInteger

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7
    • core-libs
    • None
    • generic
    • generic

      FileInputStream and FileOutputStream use ThreadLocal to communicate
      information from finalize() to close(). This is slow and ugly.
      Generally, use of ThreadLocal is considered a last resource.
      Here's a code sketch of a cleaner solution:

      Instead of
       
          private final Object closeLock = new Object();
          private volatile boolean closed = false;
          private static final ThreadLocal<Boolean> runningFinalize =

      use

      private final static int OPEN = 0;
      private final static int RUNNING_FINALIZE = 1;
      private final static int CLOSED = 2;
      AtomicInteger state = new AtomicInteger(OPEN);

      and then CAS state in an infinite loop in the state-changing methods.

      e.g.

      void close() {
        for (;;) {
           int st = state.get();
           if (st == CLOSED) return;
           boolean runningFinalize = (st == RUNNING_FINALIZE);
           ...
           if (state.compareAndSetState(st, CLOSED)).... return;
        }
      }

      This does not require locking to manage state transitions.

            Unassigned Unassigned
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: