Package gov.usgs.util

Class 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 java.lang.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
      ObjectLock()
      Construct a new ObjectLock object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void 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.
      • Methods inherited from class java.lang.Object

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

      • ObjectLock

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

      • acquireReadLock

        public void acquireReadLock​(T object)
                             throws java.lang.InterruptedException
        Acquire a read lock for an object. Callers MUST subsequently call releaseReadLock.
        Parameters:
        object - the object to lock for reading.
        Throws:
        java.lang.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 java.lang.InterruptedException
        Acquire a write lock for an object. Callers MUST also call releaseWriteLock(object).
        Parameters:
        object - the object to lock for writing.
        Throws:
        java.lang.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 java.lang.InterruptedException
        This is a synonym for acquireWriteLock, which is an exclusive lock for this object.
        Parameters:
        object - the object to lock.
        Throws:
        java.lang.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.