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

add a AutoCloseable to Lock

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 7
    • core-libs
    • x86
    • windows_7

      A DESCRIPTION OF THE REQUEST :
      currently when using a Lock you need to use the try finally pattern (similar to streams):

           Lock l = ...;
           l.lock();
           try {
               // access the resource protected by this lock
           } finally {
               l.unlock();
           }

      with AutoCloseable this can be simplified with (names are debatable)

           Lock l = ...;
           try(AutoCloseable cl = l.lockWithClosable()){
               // access the resource protected by this lock
           }

      JUSTIFICATION :
      this makes using Locks closer in syntax and semantics to using the Object monitors

      while still maintaining the full availability of the explicit lock and unlock methods

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the close of the returned AutoCloseable should call unlock() on the lock the first time and subsequent calls should either throw a IllegalMonitorStateException (similar to calling unlock on a ReentrantLock when not held) or be a no-op

      this is to couple each close() of the AutoCloseable to a lockWithCloseable() as each unlock() is to a lock()

      whether correct nesting should be enforced should be implementation defined
      ACTUAL -
      //not implemented

            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: