Class LockResource

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    RWLockResource

    public class LockResource
    extends java.lang.Object
    implements java.io.Closeable
    A resource lock that makes it possible to acquire and release locks using the following idiom:
       try (LockResource r = new LockResource(lock)) {
         ...
       }
     
    The subclasses will be tracked by the leak detector. The subclasses should call super.close() in their close(), otherwise a leak will be reported.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.locks.Lock mLock  
    • Constructor Summary

      Constructors 
      Constructor Description
      LockResource​(java.util.concurrent.locks.Lock lock)
      Creates a new instance of LockResource using the given lock.
      LockResource​(java.util.concurrent.locks.Lock lock, boolean acquireLock, boolean useTryLock)
      Creates a new instance of LockResource using the given lock.
      LockResource​(java.util.concurrent.locks.Lock lock, boolean acquireLock, boolean useTryLock, java.lang.Runnable closeAction)
      Creates a new instance of LockResource using the given lock.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Releases the lock.
      boolean hasSameLock​(LockResource other)
      Returns true if the other LockResource contains the same lock.
      • Methods inherited from class java.lang.Object

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

      • mLock

        protected java.util.concurrent.locks.Lock mLock
    • Constructor Detail

      • LockResource

        public LockResource​(java.util.concurrent.locks.Lock lock)
        Creates a new instance of LockResource using the given lock.
        Parameters:
        lock - the lock to acquire
      • LockResource

        public LockResource​(java.util.concurrent.locks.Lock lock,
                            boolean acquireLock,
                            boolean useTryLock)
        Creates a new instance of LockResource using the given lock. This method may use the Lock.tryLock() method to gain ownership of the locks. The reason one might want to use this is to avoid the fairness heuristics within the ReentrantReadWriteLock's NonFairSync which may block reader threads if a writer is the first in the queue.
        Parameters:
        lock - the lock to acquire
        acquireLock - whether to lock the lock
        useTryLock - whether or not use to Lock.tryLock()
      • LockResource

        public LockResource​(java.util.concurrent.locks.Lock lock,
                            boolean acquireLock,
                            boolean useTryLock,
                            @Nullable
                            java.lang.Runnable closeAction)
        Creates a new instance of LockResource using the given lock. This method may use the Lock.tryLock() method to gain ownership of the locks. The reason one might want to use this is to avoid the fairness heuristics within the ReentrantReadWriteLock's NonFairSync which may block reader threads if a writer is the first in the queue.
        Parameters:
        lock - the lock to acquire
        acquireLock - whether to lock the lock
        useTryLock - whether or not use to Lock.tryLock()
        closeAction - the nullable closeable that will be run before releasing the lock
    • Method Detail

      • hasSameLock

        public boolean hasSameLock​(LockResource other)
        Returns true if the other LockResource contains the same lock.
        Parameters:
        other - other LockResource
        Returns:
        true if the other lockResource has the same lock
      • close

        public void close()
        Releases the lock.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable