Class OptimisticObjectLockManager<O,L>
- java.lang.Object
-
- de.aristaflow.adept2.util.locking.ObjectLockManager<O,L>
-
- de.aristaflow.adept2.util.locking.OptimisticObjectLockManager<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 OptimisticObjectLockManager<O,L> extends ObjectLockManager<O,L>
A manager for (optimistically) locking objects with reentrant locks whereas the locks are itself object specific. No synchronisation takes place, it may happen that lock objects are created and not used at all since another lock has been created concurrently.
Optimistic locking is usually better in environments where locks live short and are not very reentrant.- Author:
- Ulrich Kreher
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classOptimisticObjectLockManager.LockRemover<O,L>When unlocking an object for the last time (count is 0), this will remove the corresponding locked object from the map containing all locked objects.
-
Field Summary
Fields Modifier and Type Field Description protected Constructor<? extends ReentrantLock<L>>lockConstrThe constructor for instantiating lock implementations.protected ConcurrentMap<O,ReentrantLock<L>>objectLocksA mapping from the locked object to the corresponding locking object.protected booleanserviceLoggerWhether the constructor of the lock implementation accepts the service logger.-
Fields inherited from class de.aristaflow.adept2.util.locking.ObjectLockManager
lockCountManager, lockType, logger
-
-
Constructor Summary
Constructors Constructor Description OptimisticObjectLockManager(Class<? extends LockCountManager<? super L>> lockCountManager, Class<? extends ReentrantLock<L>> lock, String lockType, Logger logger)Creates a newObjectLockManagerwith the designated lock count manager providing the "equality" for the designated reentrant locks and of the designated type using optimistic locking.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidgetLockForObjectReadWriteDirect(boolean writeLock, L lockingObject, O lockedObject, long timeout)Cf.protected ReentrantLock<L>getObjectLock(O lockedObject)Gets the lock for the designated object.-
Methods inherited from class de.aristaflow.adept2.util.locking.ObjectLockManager
getLockForObject, getLockForObject, hasLock, unlockObject
-
-
-
-
Field Detail
-
objectLocks
protected final ConcurrentMap<O,ReentrantLock<L>> objectLocks
A mapping from the locked object to the corresponding locking object.
-
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
-
OptimisticObjectLockManager
public OptimisticObjectLockManager(Class<? extends LockCountManager<? super L>> lockCountManager, Class<? extends ReentrantLock<L>> lock, String lockType, Logger logger) throws InvocationTargetException
Creates a newObjectLockManagerwith the designated lock count manager providing the "equality" for the designated reentrant locks and of the designated type using optimistic 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>>), abooleanas well as an instance ofLockNotification.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, anInvocationTargetExceptionwill 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:ObjectLockManagerGets the lock for the designated object. This allows subclasses to use their own data structure for lock management.- Specified by:
getObjectLockin 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
nullin case the designated object is not locked.
-
getLockForObjectReadWriteDirect
protected void getLockForObjectReadWriteDirect(boolean writeLock, L lockingObject, O lockedObject, long timeout) throws InterruptedException, TimeoutExceptionDescription copied from class:ObjectLockManagerCf. documentation ofObjectLockManager.getLockForObject(boolean, Object, Object, long). Implementors can rely on the first boolean to be set appropriately depending on present locks.- Specified by:
getLockForObjectReadWriteDirectin 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, anInterruptedExceptionwill be thrown.TimeoutException- If the designated timeout elapsed before the lock can be acquired, aTimeoutExceptionwill be thrown.
-
-