Class NonReentrantLock

java.lang.Object
com.github.benmanes.caffeine.cache.NonReentrantLock
All Implemented Interfaces:
Serializable, Lock

final class NonReentrantLock extends Object implements Lock, Serializable
A non-reentrant mutual exclusion Lock. This type of lock does not allow recursive locks held by the same thread and will deadlock if used recursively. This type of lock is useful when reentrancy is not required and a slim lock is desired.

A NonReentrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. This can be checked using methods isHeldByCurrentThread().

It is recommended practice to always immediately follow a call to lock with a try block, most typically in a before/after construction such as:

 
 class X {
   private final ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() {
     lock.lock();  // block until condition holds
     try {
       // ... method body
     } finally {
       lock.unlock()
     }
   }
 }
 

In addition to implementing the Lock interface, this class defines a number of public and protected methods for inspecting the state of the lock. Some of these methods are only useful for instrumentation and monitoring.

Serialization of this class behaves in the same way as built-in locks: a deserialized lock is in the unlocked state, regardless of its state when serialized.

  • Field Details

  • Constructor Details

    • NonReentrantLock

      public NonReentrantLock()
  • Method Details

    • lock

      public void lock()
      Specified by:
      lock in interface Lock
    • lockInterruptibly

      public void lockInterruptibly() throws InterruptedException
      Specified by:
      lockInterruptibly in interface Lock
      Throws:
      InterruptedException
    • tryLock

      public boolean tryLock()
      Specified by:
      tryLock in interface Lock
    • tryLock

      public boolean tryLock(long time, TimeUnit unit) throws InterruptedException
      Specified by:
      tryLock in interface Lock
      Throws:
      InterruptedException
    • unlock

      public void unlock()
      Specified by:
      unlock in interface Lock
    • newCondition

      public Condition newCondition()
      Specified by:
      newCondition in interface Lock
    • isHeldByCurrentThread

      public boolean isHeldByCurrentThread()
      Queries if this lock is held by the current thread.

      Analogous to the Thread.holdsLock(Object) method for built-in monitor locks, this method is typically used for debugging and testing. For example, a method that should only be called while a lock is held can assert that this is the case:

       {
         @code
         class X {
           ReentrantLock lock = new ReentrantLock();
      
           // ...
      
           public void m() {
             assert lock.isHeldByCurrentThread();
             // ... method body
           }
         }
       }
       

      It can also be used to ensure that a reentrant lock is used in a non-reentrant manner, for example:

       {
         @code
         class X {
           ReentrantLock lock = new ReentrantLock();
      
           // ...
      
           public void m() {
             assert !lock.isHeldByCurrentThread();
             lock.lock();
             try {
               // ... method body
             } finally {
               lock.unlock();
             }
           }
         }
       }
       
      Returns:
      true if current thread holds this lock and false otherwise
    • isLocked

      public boolean isLocked()
      Queries if this lock is held by any thread. This method is designed for use in monitoring of the system state, not for synchronization control.
      Returns:
      true if any thread holds this lock and false otherwise
    • getOwner

      public Thread getOwner()
      Returns the thread that currently owns this lock, or null if not owned. When this method is called by a thread that is not the owner, the return value reflects a best-effort approximation of current lock status. For example, the owner may be momentarily null even if there are threads trying to acquire the lock but have not yet done so. This method is designed to facilitate construction of subclasses that provide more extensive lock monitoring facilities.
      Returns:
      the owner, or null if not owned
    • hasQueuedThreads

      public boolean hasQueuedThreads()
      Queries whether any threads are waiting to acquire this lock. Note that because cancellations may occur at any time, a true return does not guarantee that any other thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.
      Returns:
      true if there may be other threads waiting to acquire the lock
    • hasQueuedThread

      public boolean hasQueuedThread(Thread thread)
      Queries whether the given thread is waiting to acquire this lock. Note that because cancellations may occur at any time, a true return does not guarantee that this thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.
      Parameters:
      thread - the thread
      Returns:
      true if the given thread is queued waiting for this lock
      Throws:
      NullPointerException - if the thread is null
    • getQueueLength

      public int getQueueLength()
      Returns an estimate of the number of threads waiting to acquire this lock. The value is only an estimate because the number of threads may change dynamically while this method traverses internal data structures. This method is designed for use in monitoring of the system state, not for synchronization control.
      Returns:
      the estimated number of threads waiting for this lock
    • getQueuedThreads

      public Collection<Thread> getQueuedThreads()
      Returns a collection containing threads that may be waiting to acquire this lock. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive monitoring facilities.
      Returns:
      the collection of threads
    • hasWaiters

      public boolean hasWaiters(Condition condition)
      Queries whether any threads are waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, a true return does not guarantee that a future signal will awaken any threads. This method is designed primarily for use in monitoring of the system state.
      Parameters:
      condition - the condition
      Returns:
      true if there are any waiting threads
      Throws:
      IllegalMonitorStateException - if this lock is not held
      IllegalArgumentException - if the given condition is not associated with this lock
      NullPointerException - if the condition is null
    • getWaitQueueLength

      public int getWaitQueueLength(Condition condition)
      Returns an estimate of the number of threads waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring of the system state, not for synchronization control.
      Parameters:
      condition - the condition
      Returns:
      the estimated number of waiting threads
      Throws:
      IllegalMonitorStateException - if this lock is not held
      IllegalArgumentException - if the given condition is not associated with this lock
      NullPointerException - if the condition is null
    • getWaitingThreads

      public Collection<Thread> getWaitingThreads(Condition condition)
      Returns a collection containing those threads that may be waiting on the given condition associated with this lock. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive condition monitoring facilities.
      Parameters:
      condition - the condition
      Returns:
      the collection of threads
      Throws:
      IllegalMonitorStateException - if this lock is not held
      IllegalArgumentException - if the given condition is not associated with this lock
      NullPointerException - if the condition is null
    • toString

      public String toString()
      Returns a string identifying this lock, as well as its lock state. The state, in brackets, includes either the String "Unlocked" or the String "Locked by" followed by the name of the owning thread.
      Overrides:
      toString in class Object
      Returns:
      a string identifying this lock, as well as its lock state