Class ReentrantLock<T>
- java.lang.Object
-
- de.aristaflow.adept2.util.locking.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.
-
Field Summary
Fields Modifier and Type Field Description protected Logger
logger
My logger.protected LockNotification<T>
notification
The notification to call directly but atomically after locking and unlocking.protected ReentrantLock.Lock
readLock
The lock responsible for synchronising read access.protected ReentrantLock.Lock
writeLock
The lock responsible for synchronising write access.
-
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 theLockCountManager
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 designatedLockCountManager
for counting locks as well as comparing objects.ReentrantLock(LockCountManager<? super T> lockCountManager, Logger logger)
Creates a new exclusive lock using the designatedLockCountManager
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 designatedLockCountManager
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 designatedLockCountManager
for counting locks as well as comparing objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
invalidate()
Invalidates the read and the write lock by letting all waiting threads and all future threads throw anInterruptedException
.boolean
isReadLocked()
Returns whether this read/write-lock is currently read-locked or this exclusive lock is currently locked.boolean
isWriteLocked()
Returns whether this read/write-lock is currently write-locked or this exclusive lock is currently locked.ReentrantLock.Lock
readLock()
Returns the read-lock for this read/write-lock or the exclusive lock in case of an exclusive lock.ReentrantLock.Lock
writeLock()
Returns the write-lock for this read/write-lock or the exclusive lock in case of an exclusive lock.
-
-
-
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 thewriteLock()
.
-
notification
protected final LockNotification<T> notification
The notification to call directly but atomically after locking and unlocking. If this isnull
, 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 designatedLockCountManager
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, aNouSuchMethodException
will be thrown.InstantiationException
- If the designated lock count manager class cannot be instantiated or does not have a parameterless constructor, anInstantiationException
will be thrown.IllegalAccessException
- If the designated lock count manager class or its constructor is not accessible, anIllegalAccessException
will be thrown.InvocationTargetException
- If the constructor of the designated lock count manager throws an exception, this will be wrapped by anInvocationTargetException
.
-
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 designatedLockCountManager
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 isnull
, no notification will be sent.logger
- The logger for log messages.- Throws:
NoSuchMethodException
- If the designated lock count manager has no constructor without parameters, aNouSuchMethodException
will be thrown.InstantiationException
- If the designated lock count manager class cannot be instantiated or does not have a parameterless constructor, anInstantiationException
will be thrown.IllegalAccessException
- If the designated lock count manager class or its constructor is not accessible, anIllegalAccessException
will be thrown.InvocationTargetException
- If the constructor of the designated lock count manager throws an exception, this will be wrapped by anInvocationTargetException
.
-
ReentrantLock
public ReentrantLock(LockCountManager<? super T> lockCountManager, Logger logger)
Creates a new exclusive lock using the designatedLockCountManager
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 designatedLockCountManager
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 isnull
, 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 theLockCountManager
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 isnull
or equals thewriteLockCountManager
, an exclusive lock will be created.notification
- The notification to call directly but atomically after locking and unlocking. If this isnull
, 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 anInterruptedException
.
-
-