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>
| Modifier and Type | Field and Description |
|---|---|
protected java.lang.reflect.Constructor<? extends ReentrantLock<L>> |
lockConstr
The constructor for instantiating lock implementations.
|
protected java.util.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.
|
lockCountManager, lockType, logger| Constructor and Description |
|---|
PessimisticObjectLockManager(java.lang.Class<? extends LockCountManager<? super L>> lockCountManager,
java.lang.Class<? extends ReentrantLock<L>> lock,
java.lang.String lockType,
java.util.logging.Logger logger)
Creates a new
ObjectLockManager with the designated lock count
manager providing the "equality" for the designated reentrant
locks and of the designated type using pessimistic locking. |
| Modifier and Type | Method and Description |
|---|---|
protected void |
getLockForObjectReadWriteDirect(boolean writeLock,
L lockingObject,
O lockedObject,
long timeout)
Cf. documentation of
ObjectLockManager.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.
|
getLockForObject, getLockForObjectprotected final java.util.Map<O,ReentrantLock<L>> objectLocks
protected final ObjectCounting<O> waitingLockCount
objectLocks in
case threads are waiting for this lock.protected final java.lang.reflect.Constructor<? extends ReentrantLock<L>> lockConstr
protected final boolean serviceLogger
public PessimisticObjectLockManager(java.lang.Class<? extends LockCountManager<? super L>> lockCountManager, java.lang.Class<? extends ReentrantLock<L>> lock, java.lang.String lockType, java.util.logging.Logger logger) throws java.lang.reflect.InvocationTargetException
ObjectLockManager with the designated lock count
manager providing the "equality" for the designated reentrant
locks and of the designated type using pessimistic locking. 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
designated lockCountManager (of type
Class<? extends LockCountManager<? super L>>) and a
boolean.lockType - The type of locks managed by the created object. This is
for user information only.logger - The logger for log messages.java.lang.reflect.InvocationTargetException - If there are problems instantiating the
designated lock count manager or the reentrant lock, an
InvocationTargetException will be thrown. This
prevents problems at runtime when actually retrieving a lock
(which requires creating the corresponding objects).protected ReentrantLock<L> getObjectLock(O lockedObject)
ObjectLockManagergetObjectLock in class ObjectLockManager<O,L>lockedObject - The locked object for which to retrieve the
corresponding reentrant lock.null in case the designated object is not locked.protected void getLockForObjectReadWriteDirect(boolean writeLock,
L lockingObject,
O lockedObject,
long timeout)
throws java.lang.InterruptedException,
java.util.concurrent.TimeoutException
ObjectLockManagerObjectLockManager.getLockForObject(boolean, Object, Object, long). Implementors can
rely on the first boolean to be set appropriately depending on present
locks.getLockForObjectReadWriteDirect in class ObjectLockManager<O,L>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).java.lang.InterruptedException - If the thread is interrupted while waiting for
the lock, an InterruptedException will be thrown.java.util.concurrent.TimeoutException - If the designated timeout elapsed before the lock
can be acquired, a TimeoutException will be thrown.public boolean hasLock(boolean writeLock,
L lockingObject,
O lockedObject)
ObjectLockManagerhasLock in class ObjectLockManager<O,L>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.public boolean unlockObject(boolean writeLock,
L lockingObject,
O lockedObject)
throws LockException
ObjectLockManagerLockException occurs while trying to unlock, the lock will
still be held when returning.unlockObject in class ObjectLockManager<O,L>writeLock - Whether to release the write lock.lockingObject - The object unlocking the designated object.lockedObject - The object to unlock.ReentrantLock.Lock.unlock(Object) and usually only
required by subclasses.LockException - If the locked object has not locked at all or not by
the designated locking object, a LockException will
be thrown.