Search code examples
javalockingreentrantlock

Java Lock - Unlock


   java.util.concurrent.locks.ReentrantLock;                             

   Public boolean ABCD(final AbcModel abcModel) {
    final Lock lock = (Lock)this.xyz.get((Object)abcModel);
    lock.lock();
    try {
        return super.ABCD(abcModel);
    }
    finally {
        lock.unlock();
    }
}

Does this method make threads and resources remain lock?


Solution

  • Does this method make threads and resources remain lock?

    No. The lock will be unlocked (released) in the finally block. This will happen after the super.ABCD(abcModel) call completes, and before the result of that call is returned.

    This follows directly from the specified behavior of finally; e.g. see JLS 14.20.2 or Exceptions > The finally block on the Java Oracle Tutorial which states:

    "The runtime system always executes the statements within the finally block regardless of what happens within the try block."


    ... though it mentions JVM exit as an exception to "always" a few sentences earlier.