Class StripedReadWriteLockSync

  • All Implemented Interfaces:
    CacheLockProvider, StripedReadWriteLock

    public class StripedReadWriteLockSync
    extends java.lang.Object
    implements StripedReadWriteLock
    Provides a number of Sync which allow fine-grained concurrency. Rather than locking a cache or a store, the individual elements or constituent objects can be locked. This dramatically increases the possible concurrency.

    The more stripes, the higher the concurrency. To be threadsafe, the instance of CacheLockProvider needs to be maintained for the entire life of the cache or store, so there is some added memory use.

    Though a new class, this code has been refactored from BlockingCache, where it has been in use in highly concurrent production environments for years.

    Based on the lock striping concept from Brian Goetz. See Java Concurrency in Practice 11.4.3

    Author:
    Alex Snaps
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_NUMBER_OF_MUTEXES
      The default number of locks to use.
    • Constructor Summary

      Constructors 
      Constructor Description
      StripedReadWriteLockSync()
      Constructs a striped mutex with the default 2048 stripes.
      StripedReadWriteLockSync​(int numberOfStripes)
      Constructs a striped mutex with the default 2048 stripes.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<ReadWriteLockSync> getAllSyncs()
      Returns all internal syncs
      java.util.concurrent.locks.ReadWriteLock getLockForKey​(java.lang.Object key)
      Gets the RWL Stripe to use for a given key.
      ReadWriteLockSync getSyncForKey​(java.lang.Object key)
      Gets the Sync Stripe to use for a given key.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_NUMBER_OF_MUTEXES

        public static final int DEFAULT_NUMBER_OF_MUTEXES
        The default number of locks to use. Must be a power of 2.

        The choice of 2048 enables 2048 concurrent operations per cache or cache store, which should be enough for most uses.

        See Also:
        Constant Field Values
    • Constructor Detail

      • StripedReadWriteLockSync

        public StripedReadWriteLockSync()
        Constructs a striped mutex with the default 2048 stripes.
      • StripedReadWriteLockSync

        public StripedReadWriteLockSync​(int numberOfStripes)
        Constructs a striped mutex with the default 2048 stripes.

        The number of stripes determines the number of concurrent operations per cache or cache store.

        Parameters:
        numberOfStripes - - must be a factor of two
    • Method Detail

      • getSyncForKey

        public ReadWriteLockSync getSyncForKey​(java.lang.Object key)
        Gets the Sync Stripe to use for a given key.

        This lookup must always return the same Sync for a given key.

        Specified by:
        getSyncForKey in interface CacheLockProvider
        Parameters:
        key - the key
        Returns:
        one of a limited number of Sync's.
      • getLockForKey

        public java.util.concurrent.locks.ReadWriteLock getLockForKey​(java.lang.Object key)
        Gets the RWL Stripe to use for a given key.

        This lookup must always return the same RWL for a given key.

        Specified by:
        getLockForKey in interface StripedReadWriteLock
        Parameters:
        key - the key
        Returns:
        one of a limited number of RWLs.