Class ReentrantLock.Lock

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean invalidated
      Whether this lock has been invalidated and no thread should wait any more.
      protected LockCountManager<? super T> lockCountManager
      The manager for counting the lock acquisitions of an object and encapsulating whether objects are equal.
      protected AtomicInteger waitersForLock
      Counts how many threads are waiting to get this lock.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Lock​(LockCountManager<? super T> lockCountManager)
      Creates a new exclusive lock using the designated LockCountManager for comparing objects.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      String currentLockingObject()
      Retrieves a string representation of the object that is currently holding the lock.
      boolean hasLock​(T object)
      Determines if the designated object holds this lock.
      void invalidate()
      Invalidates the lock by letting all waiting threads and all future threads throw an InterruptedException.
      boolean isLocked()
      Returns whether this lock is currently locked by at least one object.
      void lock​(T object)
      Provides the designated object with an exclusive lock.
      void lock​(T object, long timeout)
      Provides the designated object with an exclusive lock.
      protected boolean notificationRequired()
      Gets whether a notification of waiting threads is required.
      int objectsWaitingToLock()
      Returns the number of objects waiting to acquire this lock.
      boolean unlock​(T object)
      Releases this lock for the designated object if it holds the lock.
    • Field Detail

      • lockCountManager

        protected final LockCountManager<? super T> lockCountManager
        The manager for counting the lock acquisitions of an object and encapsulating whether objects are equal.
      • waitersForLock

        protected final AtomicInteger waitersForLock
        Counts how many threads are waiting to get this lock.
      • invalidated

        protected boolean invalidated
        Whether this lock has been invalidated and no thread should wait any more.
    • Constructor Detail

      • Lock

        protected Lock​(LockCountManager<? super T> lockCountManager)
        Creates a new exclusive lock using the designated LockCountManager for comparing objects.
        Parameters:
        lockCountManager - The manager counting locks for objects.
    • Method Detail

      • lock

        public void lock​(T object)
                  throws InterruptedException
        Provides the designated object with an exclusive lock. The current thread will be blocked until the lock can be acquired.

        Calls Lock.lock(object, 0).

        Parameters:
        object - The object that requests an exclusive lock.
        Throws:
        InterruptedException - If the thread is interrupted while waiting for the lock, an InterruptedException will be thrown.
      • lock

        public void lock​(T object,
                         long timeout)
                  throws InterruptedException,
                         TimeoutException
        Provides the designated object with an exclusive lock. The current thread will be blocked until the lock can be acquired or the timeout elapsed.

        Since the lock is reentrant, an object may acquire a lock multiple times. But it also has to release the lock per acquisition afterwards.

        Parameters:
        object - The object that requests an exclusive lock.
        timeout - The time to wait for the lock. A timeout <= 0 means to wait forever (or until the lock is granted).
        Throws:
        InterruptedException - If the thread is interrupted while waiting for the lock, an InterruptedException will be thrown.
        TimeoutException - If the designated timeout elapsed before the lock can be acquired, a TimeoutException will be thrown.
      • hasLock

        public boolean hasLock​(T object)
        Determines if the designated object holds this lock.
        Parameters:
        object - The object that should be checked to hold this lock.
        Returns:
        Whether the designated object holds this lock.
      • isLocked

        public boolean isLocked()
        Returns whether this lock is currently locked by at least one object.
        Returns:
        Whether this lock is currently locked by at least one object.
      • objectsWaitingToLock

        public int objectsWaitingToLock()
        Returns the number of objects waiting to acquire this lock.
        Returns:
        The number of objects waiting to acquire this lock
      • invalidate

        public void invalidate()
        Invalidates the lock by letting all waiting threads and all future threads throw an InterruptedException.
      • unlock

        public boolean unlock​(T object)
        Releases this lock for the designated object if it holds the lock. If there are other objects waiting for this lock, they will be notified.
        Parameters:
        object - The object for which the lock should be released.
        Returns:
        Whether a notification is sent. This will be done only if the lock is available now and there are already other objects waiting for this lock. Otherwise (the lock remains locked by another object or it is available but there are no waiters for it) false will be returned.
        Throws:
        IllegalArgumentException - If the designated object does not hold this lock, an IllegalArgumentException will be thrown.
      • notificationRequired

        protected boolean notificationRequired()
        Gets whether a notification of waiting threads is required. This depends on whether there are no more locks and whether there are threads waiting for the lock.
        Returns:
        Whether a notification of waiting threads is required (no more locks and there are waiters for the lock).
      • currentLockingObject

        public String currentLockingObject()
        Retrieves a string representation of the object that is currently holding the lock. Due to security reasons the object may not be returned since this would allow to unlock.
        Returns:
        A string representation of the current (or one in case of several) locking objects; the empty string will be returned, if the lock is not locked.