Package gov.usgs.util
Class ObjectLock<T>
java.lang.Object
gov.usgs.util.ObjectLock<T>
- Type Parameters:
T
- The type of object used for locking. This object is used as a key in a HashMap. Objects that are equal, but not necessarily ==, reference the same lock.
Reentrant ReadWrite Locking per object.
This is intended for use when multiple sections of code should allow
concurrent access, but only when operating on independent objects.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
acquireLock
(T object) This is a synonym for acquireWriteLock, which is an exclusive lock for this object.void
acquireReadLock
(T object) Acquire a read lock for an object.void
acquireWriteLock
(T object) Acquire a write lock for an object.boolean
haveWriteLock
(T object) Check if the calling thread currently has a write lock for the object.void
releaseLock
(T object) This is a synonym for releaseWriteLock, which is an exclusive lock for this object.void
releaseReadLock
(T object) Release a held read lock for an object.void
releaseWriteLock
(T object) Release a held write lock for an object.
-
Constructor Details
-
ObjectLock
public ObjectLock()Construct a new ObjectLock object.
-
-
Method Details
-
acquireReadLock
Acquire a read lock for an object. Callers MUST subsequently call releaseReadLock.- Parameters:
object
- the object to lock for reading.- Throws:
InterruptedException
- if thread is interrupted
-
haveWriteLock
Check if the calling thread currently has a write lock for the object.- Parameters:
object
- object to check.- Returns:
- true if the current thread currently holds a write lock, false otherwise.
-
releaseReadLock
Release a held read lock for an object. Callers MUST have previously called acquireReadLock(object).- Parameters:
object
- the object to unlock for reading.
-
acquireWriteLock
Acquire a write lock for an object. Callers MUST also call releaseWriteLock(object).- Parameters:
object
- the object to lock for writing.- Throws:
InterruptedException
- if thread is interrupted
-
releaseWriteLock
Release a held write lock for an object. Callers MUST have previously called acquireWriteLock(object).- Parameters:
object
- the object to unlock for writing.
-
acquireLock
This is a synonym for acquireWriteLock, which is an exclusive lock for this object.- Parameters:
object
- the object to lock.- Throws:
InterruptedException
- if thread is interrupted
-
releaseLock
This is a synonym for releaseWriteLock, which is an exclusive lock for this object.- Parameters:
object
- the object to unlock.
-