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.

public class ObjectLock<T> extends Object
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
    Constructor
    Description
    Construct a new ObjectLock object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    acquireLock(T object)
    This is a synonym for acquireWriteLock, which is an exclusive lock for this object.
    void
    Acquire a read lock for an object.
    void
    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
    Release a held read lock for an object.
    void
    Release a held write lock for an object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ObjectLock

      public ObjectLock()
      Construct a new ObjectLock object.
  • Method Details

    • acquireReadLock

      public void acquireReadLock(T object) throws InterruptedException
      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

      public boolean haveWriteLock(T object)
      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

      public void releaseReadLock(T object)
      Release a held read lock for an object. Callers MUST have previously called acquireReadLock(object).
      Parameters:
      object - the object to unlock for reading.
    • acquireWriteLock

      public void acquireWriteLock(T object) throws InterruptedException
      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

      public void releaseWriteLock(T object)
      Release a held write lock for an object. Callers MUST have previously called acquireWriteLock(object).
      Parameters:
      object - the object to unlock for writing.
    • acquireLock

      public void acquireLock(T object) throws InterruptedException
      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

      public void releaseLock(T object)
      This is a synonym for releaseWriteLock, which is an exclusive lock for this object.
      Parameters:
      object - the object to unlock.