Class ReentrantLock<T>

  • Type Parameters:
    T - The type of objects which may lock this reentrant lock (lock owning object type).
    Direct Known Subclasses:
    SessionLock, ThreadLock, UUIDLock

    public class ReentrantLock<T>
    extends Object
    Represents a reentrant read/write lock or exclusive lock based on objects instead of threads, for instance for locking based on objects.
    The locks are reentrant, that means, one object can acquire a lock multiple times - but it also has to release it for every acquisition. A read/write lock allows multiple readers to own the lock at the same time but blocks writers meanwhile. Writers also block readers but only one writer is allowed at a time.
    Writers have priority, as soon as one writer wants to lock, all new readers are blocked until the writer has unlocked. If another writer wants to lock, the readers will be blocked further on.
    Neither downgrading nor upgrading a read/write lock is supported by this class and it will either lead to a deadlock or not work correctly - do not even think of it. If you need it, just use only a write lock and live with less concurrency.
    See Also:
    ReentrantReadWriteLock
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  ReentrantLock.Lock
      Represents a reentrant exclusive lock which allows only one object to hold the lock but it may hold it multiple times.
      protected class  ReentrantLock.ReadLock
      Represents a read lock which allows multiple objects to hold the lock simultaneously and multiple times but blocks as soon as one object wants a write lock.
      protected class  ReentrantLock.WriteLock
      Represents a write lock which allows an object to hold the lock multiple times. an object requesting a write lock blocks objects that request a read lock.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReentrantLock​(LockCountManager<? super T> writeLockCountManager, LockCountManager<? super T> readLockCountManager, LockNotification<T> notification, Logger logger)
      Creates either a new exclusive lock or a new read/write-lock and using the LockCountManager for counting locks as well as comparing objects.
      ReentrantLock​(LockCountManager<? super T> lockCountManager, LockNotification<T> notification, Logger logger)
      Creates a new exclusive lock using the designated LockCountManager for counting locks as well as comparing objects.
      ReentrantLock​(LockCountManager<? super T> lockCountManager, Logger logger)
      Creates a new exclusive lock using the designated LockCountManager for counting locks as well as comparing objects.
      ReentrantLock​(Class<? extends LockCountManager<? super T>> lockCountManager, boolean exclusive, LockNotification<T> notification, Logger logger)
      Creates either a new exclusive lock or a new read/write-lock and using the designated LockCountManager for counting locks as well as comparing objects.
      ReentrantLock​(Class<? extends LockCountManager<? super T>> lockCountManager, boolean exclusive, Logger logger)
      Creates either a new exclusive lock or a new read/write-lock and using the designated LockCountManager for counting locks as well as comparing objects.
    • Field Detail

      • logger

        protected final Logger logger
        My logger.
      • writeLock

        protected final ReentrantLock.Lock writeLock
        The lock responsible for synchronising write access.
      • readLock

        protected final ReentrantLock.Lock readLock
        The lock responsible for synchronising read access. In case of an exclusive lock this is the same as the writeLock().
      • notification

        protected final LockNotification<T> notification
        The notification to call directly but atomically after locking and unlocking. If this is null, no notification will be sent.
    • Constructor Detail

      • ReentrantLock

        public ReentrantLock​(Class<? extends LockCountManager<? super T>> lockCountManager,
                             boolean exclusive,
                             Logger logger)
                      throws NoSuchMethodException,
                             InstantiationException,
                             IllegalAccessException,
                             InvocationTargetException
        Creates either a new exclusive lock or a new read/write-lock and using the designated LockCountManager for counting locks as well as comparing objects.
        Parameters:
        lockCountManager - The manager counting locks for objects. The provided class needs an accessible (public) constructor without parameters.
        exclusive - Whether to create an exclusive lock or a read/write-lock.
        logger - The logger for log messages.
        Throws:
        NoSuchMethodException - If the designated lock count manager has no constructor without parameters, a NouSuchMethodException will be thrown.
        InstantiationException - If the designated lock count manager class cannot be instantiated or does not have a parameterless constructor, an InstantiationException will be thrown.
        IllegalAccessException - If the designated lock count manager class or its constructor is not accessible, an IllegalAccessException will be thrown.
        InvocationTargetException - If the constructor of the designated lock count manager throws an exception, this will be wrapped by an InvocationTargetException.
      • ReentrantLock

        public ReentrantLock​(Class<? extends LockCountManager<? super T>> lockCountManager,
                             boolean exclusive,
                             LockNotification<T> notification,
                             Logger logger)
                      throws NoSuchMethodException,
                             InstantiationException,
                             IllegalAccessException,
                             InvocationTargetException
        Creates either a new exclusive lock or a new read/write-lock and using the designated LockCountManager for counting locks as well as comparing objects. The designated notification will be used for notifying a successful lock or unlock while locking and unlocking, that is, the notification will be atomically with respect to locking/unlocking.
        Parameters:
        lockCountManager - The manager counting locks for objects. The provided class needs an accessible (public) constructor without parameters.
        exclusive - Whether to create an exclusive lock or a read/write-lock.
        notification - The notification to call directly but atomically after locking and unlocking. If this is null, no notification will be sent.
        logger - The logger for log messages.
        Throws:
        NoSuchMethodException - If the designated lock count manager has no constructor without parameters, a NouSuchMethodException will be thrown.
        InstantiationException - If the designated lock count manager class cannot be instantiated or does not have a parameterless constructor, an InstantiationException will be thrown.
        IllegalAccessException - If the designated lock count manager class or its constructor is not accessible, an IllegalAccessException will be thrown.
        InvocationTargetException - If the constructor of the designated lock count manager throws an exception, this will be wrapped by an InvocationTargetException.
      • ReentrantLock

        public ReentrantLock​(LockCountManager<? super T> lockCountManager,
                             Logger logger)
        Creates a new exclusive lock using the designated LockCountManager for counting locks as well as comparing objects.
        Parameters:
        lockCountManager - The manager counting locks for objects.
        logger - The logger for log messages.
      • ReentrantLock

        public ReentrantLock​(LockCountManager<? super T> lockCountManager,
                             LockNotification<T> notification,
                             Logger logger)
        Creates a new exclusive lock using the designated LockCountManager for counting locks as well as comparing objects. The designated notification will be used for notifying a successful lock or unlock while locking and unlocking, that is, the notification will be atomically with respect to locking/unlocking.
        Parameters:
        lockCountManager - The manager counting locks for objects.
        notification - The notification to call directly but atomically after locking and unlocking. If this is null, no notification will be sent.
        logger - The logger for log messages.
      • ReentrantLock

        public ReentrantLock​(LockCountManager<? super T> writeLockCountManager,
                             LockCountManager<? super T> readLockCountManager,
                             LockNotification<T> notification,
                             Logger logger)
        Creates either a new exclusive lock or a new read/write-lock and using the LockCountManager for counting locks as well as comparing objects. If there is no lock count manager for the read lock or it equals the lock count manager for the write lock, an exclusive lock will be created, otherwise this will be read/write-lock.
        The designated notification will be used for notifying a successful lock or unlock while locking and unlocking, that is, the notification will be atomically with respect to locking/unlocking.
        Parameters:
        writeLockCountManager - The manager counting write locks for objects.
        readLockCountManager - The manager counting read locks for objects. If this is null or equals the writeLockCountManager, an exclusive lock will be created.
        notification - The notification to call directly but atomically after locking and unlocking. If this is null, no notification will be sent.
        logger - The logger for log messages.
    • Method Detail

      • isReadLocked

        public boolean isReadLocked()
        Returns whether this read/write-lock is currently read-locked or this exclusive lock is currently locked.
        Returns:
        Whether this read/write-lock is currently read-locked or this exclusive lock is currently locked.
      • isWriteLocked

        public boolean isWriteLocked()
        Returns whether this read/write-lock is currently write-locked or this exclusive lock is currently locked.
        Returns:
        Whether this read/write-lock currently write-locked or this exclusive lock is currently locked.
      • readLock

        public ReentrantLock.Lock readLock()
        Returns the read-lock for this read/write-lock or the exclusive lock in case of an exclusive lock.
        Returns:
        The read-lock for this read/write-lock or the exclusive lock in case of an exclusive lock.
      • writeLock

        public ReentrantLock.Lock writeLock()
        Returns the write-lock for this read/write-lock or the exclusive lock in case of an exclusive lock.
        Returns:
        The write-lock for this read/write-lock or the exclusive lock in case of an exclusive lock.
      • invalidate

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