Class PessimisticObjectLockManager<O,L>
- java.lang.Object
-
- de.aristaflow.adept2.util.locking.ObjectLockManager<O,L>
-
- de.aristaflow.adept2.util.locking.PessimisticObjectLockManager<O,L>
-
- Type Parameters:
O
- The type of the objects to be locked (locked object type).L
- The type of the objects that lock (lock owning type).
public class PessimisticObjectLockManager<O,L> extends ObjectLockManager<O,L>
A manager for (pessimistically) locking objects with reentrant locks whereas the locks are itself object specific. By synchronising appropriately on this class, concurrent modifications of the data structures are prevented. This also requires to track the number of waiting lockers.
Pessimistic locking is usually better in environments where locks live long and are held concurrently (for reading).- Author:
- Ulrich Kreher
-
-
Field Summary
Fields Modifier and Type Field Description protected Constructor<? extends ReentrantLock<L>>
lockConstr
The constructor for instantiating lock implementations.protected Map<O,ReentrantLock<L>>
objectLocks
A mapping from the locked object to the corresponding locking object.protected boolean
serviceLogger
Whether the constructor of the lock implementation accepts the service logger.protected ObjectCounting<O>
waitingLockCount
Counts the amount of threads that are waiting to lock an object.-
Fields inherited from class de.aristaflow.adept2.util.locking.ObjectLockManager
lockCountManager, lockType, logger
-
-
Constructor Summary
Constructors Constructor Description PessimisticObjectLockManager(Class<? extends LockCountManager<? super L>> lockCountManager, Class<? extends ReentrantLock<L>> lock, String lockType, Logger logger)
Creates a newObjectLockManager
with the designated lock count manager providing the "equality" for the designated reentrant locks and of the designated type using pessimistic locking.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
getLockForObjectReadWriteDirect(boolean writeLock, L lockingObject, O lockedObject, long timeout)
Cf. documentation ofObjectLockManager.getLockForObject(boolean, Object, Object, long)
.protected ReentrantLock<L>
getObjectLock(O lockedObject)
Gets the lock for the designated object.boolean
hasLock(boolean writeLock, L lockingObject, O lockedObject)
Checks whether the designated object is locked for reading or writing by the designated locking object.boolean
unlockObject(boolean writeLock, L lockingObject, O lockedObject)
Unlocks the designated object by the designated locking object.-
Methods inherited from class de.aristaflow.adept2.util.locking.ObjectLockManager
getLockForObject, getLockForObject
-
-
-
-
Field Detail
-
objectLocks
protected final Map<O,ReentrantLock<L>> objectLocks
A mapping from the locked object to the corresponding locking object.
-
waitingLockCount
protected final ObjectCounting<O> waitingLockCount
Counts the amount of threads that are waiting to lock an object. This is needed for to prevent removal of a lock fromobjectLocks
in case threads are waiting for this lock.
-
lockConstr
protected final Constructor<? extends ReentrantLock<L>> lockConstr
The constructor for instantiating lock implementations.
-
serviceLogger
protected final boolean serviceLogger
Whether the constructor of the lock implementation accepts the service logger. Otherwise the usual class logger will be used for all locks.
-
-
Constructor Detail
-
PessimisticObjectLockManager
public PessimisticObjectLockManager(Class<? extends LockCountManager<? super L>> lockCountManager, Class<? extends ReentrantLock<L>> lock, String lockType, Logger logger) throws InvocationTargetException
Creates a newObjectLockManager
with the designated lock count manager providing the "equality" for the designated reentrant locks and of the designated type using pessimistic locking.
This constructor checks whether the designated lock class and the lock count manager class can be instantiated with the appropriate constructors.- Parameters:
lockCountManager
- The lock count manager that encapsulates what locking objects are treated as equal when acquiring a lock. The provided class needs an accessible (public) constructor without parameters.lock
- The reentrant lock to be used for locking. The provided class needs an accessible (public) constructor with accepting the designatedlockCountManager
(of typeClass<? extends LockCountManager<? super L>>
) and aboolean
.lockType
- The type of locks managed by the created object. This is for user information only.logger
- The logger for log messages.- Throws:
InvocationTargetException
- If there are problems instantiating the designated lock count manager or the reentrant lock, anInvocationTargetException
will be thrown. This prevents problems at runtime when actually retrieving a lock (which requires creating the corresponding objects).
-
-
Method Detail
-
getObjectLock
protected ReentrantLock<L> getObjectLock(O lockedObject)
Description copied from class:ObjectLockManager
Gets the lock for the designated object. This allows subclasses to use their own data structure for lock management.- Specified by:
getObjectLock
in classObjectLockManager<O,L>
- Parameters:
lockedObject
- The locked object for which to retrieve the corresponding reentrant lock.- Returns:
- The reentrant lock for the designated locked object or
null
in case the designated object is not locked.
-
getLockForObjectReadWriteDirect
protected void getLockForObjectReadWriteDirect(boolean writeLock, L lockingObject, O lockedObject, long timeout) throws InterruptedException, TimeoutException
Description copied from class:ObjectLockManager
Cf. documentation ofObjectLockManager.getLockForObject(boolean, Object, Object, long)
. Implementors can rely on the first boolean to be set appropriately depending on present locks.- Specified by:
getLockForObjectReadWriteDirect
in classObjectLockManager<O,L>
- Parameters:
writeLock
- Whether to acquire a write lock. This will already be adapted to a write lock if required, that is, requesting a read lock while having the write lock already.lockingObject
- The object locking the designated object.lockedObject
- The object to 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, anInterruptedException
will be thrown.TimeoutException
- If the designated timeout elapsed before the lock can be acquired, aTimeoutException
will be thrown.
-
hasLock
public boolean hasLock(boolean writeLock, L lockingObject, O lockedObject)
Description copied from class:ObjectLockManager
Checks whether the designated object is locked for reading or writing by the designated locking object.- Overrides:
hasLock
in classObjectLockManager<O,L>
- Parameters:
writeLock
- Whether the locking object is to be checked for having a write lock on the designated locked object.lockingObject
- The object to check for having the lock on the designated object.lockedObject
- The object to check for the lock lock.- Returns:
- Whether the designated locking object has the designated lock on the designated locked object.
-
unlockObject
public boolean unlockObject(boolean writeLock, L lockingObject, O lockedObject) throws LockException
Description copied from class:ObjectLockManager
Unlocks the designated object by the designated locking object. The lock will not be released if it is held several times. If theLockException
occurs while trying to unlock, the lock will still be held when returning.- Overrides:
unlockObject
in classObjectLockManager<O,L>
- Parameters:
writeLock
- Whether to release the write lock.lockingObject
- The object unlocking the designated object.lockedObject
- The object to unlock.- Returns:
- Whether a notification has been sent when unlocking. This is the
same as
ReentrantLock.Lock.unlock(Object)
and usually only required by subclasses. - Throws:
LockException
- If the locked object has not locked at all or not by the designated locking object, aLockException
will be thrown.
-
-