Class StripedLock


  • @ThreadSafe
    public class StripedLock
    extends java.lang.Object
    A simple implementation of lock striping, using cache entry keys to lock on, primarily used to help make CacheLoader implemtations thread safe.

    Backed by a set of ReentrantReadWriteLock instances, and using the key hashcodes to determine buckets.

    Since buckets are used, it doesn't matter that the key in question is not removed from the lock map when no longer in use, since the key is not referenced in this class. Rather, the hash code is used.

    Since:
    4.0
    Author:
    Manik Surtani, Mircea.Markus@jboss.com
    • Constructor Summary

      Constructors 
      Constructor Description
      StripedLock()
      This constructor just calls StripedLock(int) with a default concurrency value of 20.
      StripedLock​(int concurrency)
      Creates a new StripedLock which uses a certain number of shared locks across all elements that need to be locked.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void acquireAllLocks​(java.util.List<java.lang.Object> keys, boolean exclusive)
      Acquires locks on keys passed in.
      boolean acquireGlobalLock​(boolean exclusive, long timeout)
      Acquires RL on all locks aggregated by this StripedLock, in the given timeout.
      void acquireLock​(java.lang.Object key, boolean exclusive)
      Blocks until a lock is acquired.
      boolean acquireLock​(java.lang.Object key, boolean exclusive, long millis)  
      void downgradeLock​(java.lang.Object key)  
      int getSharedLockCount()  
      int getTotalLockCount()
      Returns the total number of locks held by this class.
      int getTotalReadLockCount()  
      int getTotalWriteLockCount()  
      void releaseAllLocks​(java.util.List<java.lang.Object> keys)
      Releases locks on all keys passed in.
      void releaseGlobalLock​(boolean exclusive)  
      void releaseLock​(java.lang.Object key)
      Releases a lock the caller may be holding.
      void upgradeLock​(java.lang.Object key)  
      • Methods inherited from class java.lang.Object

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

      • StripedLock

        public StripedLock()
        This constructor just calls StripedLock(int) with a default concurrency value of 20.
      • StripedLock

        public StripedLock​(int concurrency)
        Creates a new StripedLock which uses a certain number of shared locks across all elements that need to be locked.
        Parameters:
        concurrency - number of threads expected to use this class concurrently.
    • Method Detail

      • acquireLock

        public void acquireLock​(java.lang.Object key,
                                boolean exclusive)
        Blocks until a lock is acquired.
        Parameters:
        exclusive - if true, a write (exclusive) lock is attempted, otherwise a read (shared) lock is used.
      • acquireLock

        public boolean acquireLock​(java.lang.Object key,
                                   boolean exclusive,
                                   long millis)
      • releaseLock

        public void releaseLock​(java.lang.Object key)
        Releases a lock the caller may be holding. This method is idempotent.
      • upgradeLock

        public void upgradeLock​(java.lang.Object key)
      • downgradeLock

        public void downgradeLock​(java.lang.Object key)
      • releaseAllLocks

        public void releaseAllLocks​(java.util.List<java.lang.Object> keys)
        Releases locks on all keys passed in. Makes multiple calls to releaseLock(Object). This method is idempotent.
        Parameters:
        keys - keys to unlock
      • acquireAllLocks

        public void acquireAllLocks​(java.util.List<java.lang.Object> keys,
                                    boolean exclusive)
        Acquires locks on keys passed in. Makes multiple calls to acquireLock(Object, boolean)
        Parameters:
        keys - keys to unlock
        exclusive - whether locks are exclusive.
      • getTotalLockCount

        public int getTotalLockCount()
        Returns the total number of locks held by this class.
      • acquireGlobalLock

        public boolean acquireGlobalLock​(boolean exclusive,
                                         long timeout)
        Acquires RL on all locks aggregated by this StripedLock, in the given timeout.
      • releaseGlobalLock

        public void releaseGlobalLock​(boolean exclusive)
      • getTotalReadLockCount

        public int getTotalReadLockCount()
      • getSharedLockCount

        public int getSharedLockCount()
      • getTotalWriteLockCount

        public int getTotalWriteLockCount()