Class ReentrantLock.Lock

java.lang.Object
de.aristaflow.adept2.util.locking.ReentrantLock.Lock
Direct Known Subclasses:
ReentrantLock.ReadLock, ReentrantLock.WriteLock
Enclosing class:
ReentrantLock<T>

public class ReentrantLock.Lock extends Object
Represents a reentrant exclusive lock which allows only one object to hold the lock but it may hold it multiple times.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
    Whether this lock has been invalidated and no thread should wait any more.
    protected final LockCountManager<? super T>
    The manager for counting the lock acquisitions of an object and encapsulating whether objects are equal.
    protected final AtomicInteger
    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

    Modifier and Type
    Method
    Description
    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
    Invalidates the lock by letting all waiting threads and all future threads throw an InterruptedException.
    boolean
    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
    Gets whether a notification of waiting threads is required.
    int
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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 Details

    • 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 Details

    • 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.