Class ObjectEqualityLockManager<O>
- java.lang.Object
-
- de.aristaflow.adept2.util.locking.ObjectEqualityLockManager<O>
-
- Type Parameters:
O
- The type of the objects to be locked being compared withObject.equals(Object)
.
public class ObjectEqualityLockManager<O> extends Object
Class with convenience methods for anObjectLockManager
using the current thread for locking and using object equality for the locked objects.- Author:
- Ulrich Kreher
-
-
Field Summary
Fields Modifier and Type Field Description protected ObjectLockManager<O,Thread>
olm
The object lock manager to which all calls will be forwarded.
-
Constructor Summary
Constructors Constructor Description ObjectEqualityLockManager(String lockType, boolean optimistic, Logger logger)
Creates a newObjectLockManager
using the current thread for locking and using object equality for the locked objects.ObjectEqualityLockManager(String lockType, Logger logger)
Creates a newObjectLockManager
with pessimistic locking using the current thread for locking and using object equality for the locked objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
getLockForObject(boolean writeLock, O lockedObject)
Locks the designated object by the current thread.void
getLockForObject(boolean writeLock, O lockedObject, long timeout)
Locks the designated object by the current thread.boolean
hasLock(boolean writeLock, O lockedObject)
Checks whether the designated object is locked for reading or writing by the current thread.void
unlockObject(boolean writeLock, O lockedObject)
Unlocks the designated object by the current thread.
-
-
-
Field Detail
-
olm
protected final ObjectLockManager<O,Thread> olm
The object lock manager to which all calls will be forwarded.
-
-
Constructor Detail
-
ObjectEqualityLockManager
public ObjectEqualityLockManager(String lockType, Logger logger) throws InvocationTargetException
Creates a newObjectLockManager
with pessimistic locking using the current thread for locking and using object equality for the locked objects.- Parameters:
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 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).
-
ObjectEqualityLockManager
public ObjectEqualityLockManager(String lockType, boolean optimistic, Logger logger) throws InvocationTargetException
Creates a newObjectLockManager
using the current thread for locking and using object equality for the locked objects.- Parameters:
lockType
- The type of locks managed by the created object. This is for user information only.optimistic
- Whether to use optimistic locking which prevents synchronisation but may create unneeded objects. This is suggested with not much lock contention.logger
- The logger for log messages.- Throws:
InvocationTargetException
- If there are problems instantiating the 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
-
getLockForObject
public void getLockForObject(boolean writeLock, O lockedObject) throws InterruptedException
Locks the designated object by the current thread.- Parameters:
writeLock
- Whether to acquire a write lock.lockedObject
- The object to lock.- Throws:
InterruptedException
- If the thread is interrupted while waiting for the lock, anInterruptedException
will be thrown.RuntimeException
- If the provided lock count manager can not be instantiated, aRuntimeException
will be thrown.
-
getLockForObject
public void getLockForObject(boolean writeLock, O lockedObject, long timeout) throws InterruptedException, TimeoutException
Locks the designated object by the current thread. Since the locks are expected to be short-term this method will wait until the lock can be successfully acquired. The locks are reentrant, that is, it needs to be unlocked for every call to this method and for the right lock type (read or write lock).- Parameters:
writeLock
- Whether to acquire a write lock.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.RuntimeException
- If the provided lock or the lock count manager can not be instantiated, aRuntimeException
will be thrown.
-
hasLock
public boolean hasLock(boolean writeLock, O lockedObject)
Checks whether the designated object is locked for reading or writing by the current thread.- Parameters:
writeLock
- Whether the locking object is to be checked for having a write lock on the designated locked 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 void unlockObject(boolean writeLock, O lockedObject) throws LockException
Unlocks the designated object by the current thread. The lock will not be released if it is held several times. If an exception occurs while trying to unlock, the lock will still be held when returning.- Parameters:
writeLock
- Whether to release the write lock.lockedObject
- The object to unlock.- Throws:
LockException
- If the locked object has not locked at all or not by the designated locking object, aLockException
will be thrown.
-
-