Class LockPool<K>

  • Type Parameters:
    K - key for the locks
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class LockPool<K>
    extends java.lang.Object
    implements java.io.Closeable
    A resource pool specifically designed to contain locks and will NOT evict any entries that are in use. The pool size is unlimited, when the pool size is larger than the configured max size, a background thread will try to evict locks that are no longer locked, but if all the locks are locked, none of them will be evicted. In the worst case (e.g. deadlock), the pool size might keep growing until exhausting system memories.
    • Constructor Summary

      Constructors 
      Constructor Description
      LockPool​(java.util.function.Function<? super K,​? extends java.util.concurrent.locks.ReentrantReadWriteLock> defaultLoader, int initialSize, int lowWatermark, int highWatermark, int concurrencyLevel)
      Constructor for a lock pool.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      boolean containsKey​(K key)
      Returns whether the pool contains a particular key.
      LockResource get​(K key, LockMode mode)
      Locks the specified key in the specified mode.
      RWLockResource get​(K key, LockMode mode, boolean useTryLock)
      Locks the specified key in the specified mode.
      java.util.Map<K,​java.util.concurrent.locks.ReentrantReadWriteLock> getEntryMap()  
      java.util.concurrent.locks.ReentrantReadWriteLock getRawReadWriteLock​(K key)
      Get the raw readwrite lock from the pool.
      int size()  
      java.lang.String toString()  
      java.util.Optional<RWLockResource> tryGet​(K key, LockMode mode)
      Attempts to take a lock on the given key.
      • Methods inherited from class java.lang.Object

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

      • LockPool

        public LockPool​(java.util.function.Function<? super K,​? extends java.util.concurrent.locks.ReentrantReadWriteLock> defaultLoader,
                        int initialSize,
                        int lowWatermark,
                        int highWatermark,
                        int concurrencyLevel)
        Constructor for a lock pool.
        Parameters:
        defaultLoader - specify a function to generate a value based on a key
        initialSize - initial size of the pool
        lowWatermark - low watermark of the pool size
        highWatermark - high watermark of the pool size
        concurrencyLevel - concurrency level of the pool
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • get

        public LockResource get​(K key,
                                LockMode mode)
        Locks the specified key in the specified mode.
        Parameters:
        key - the key to lock
        mode - the mode to lock in
        Returns:
        a lock resource which must be closed to unlock the key
      • get

        public RWLockResource get​(K key,
                                  LockMode mode,
                                  boolean useTryLock)
        Locks the specified key in the specified mode.
        Parameters:
        key - the key to lock
        mode - the mode to lock in
        useTryLock - Determines whether or not to use Lock.tryLock() or Lock.lock() to acquire the lock. Differs from tryGet(Object, LockMode) in that it will block until the lock has been acquired.
        Returns:
        a lock resource which must be closed to unlock the key
      • tryGet

        public java.util.Optional<RWLockResource> tryGet​(K key,
                                                         LockMode mode)
        Attempts to take a lock on the given key.
        Parameters:
        key - the key to lock
        mode - lockMode to acquire
        Returns:
        either empty or a lock resource which must be closed to unlock the key
      • getRawReadWriteLock

        public java.util.concurrent.locks.ReentrantReadWriteLock getRawReadWriteLock​(K key)
        Get the raw readwrite lock from the pool.
        Parameters:
        key - key to look up the value
        Returns:
        the lock associated with the key
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • containsKey

        public boolean containsKey​(K key)
        Returns whether the pool contains a particular key.
        Parameters:
        key - the key to look up in the pool
        Returns:
        true if the key is contained in the pool
      • size

        public int size()
        Returns:
        the size of the pool
      • getEntryMap

        public java.util.Map<K,​java.util.concurrent.locks.ReentrantReadWriteLock> getEntryMap()
        Returns:
        all entries in the pool, for debugging purposes