ehcache

net.sf.ehcache.concurrent
Class StripedReadWriteLockSync

java.lang.Object
  extended by net.sf.ehcache.concurrent.StripedReadWriteLockSync
All Implemented Interfaces:
CacheLockProvider

public class StripedReadWriteLockSync
extends java.lang.Object
implements CacheLockProvider

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 StripedMutex 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
static int DEFAULT_NUMBER_OF_MUTEXES
          The default number of locks to use.
 
Constructor Summary
StripedReadWriteLockSync()
          Constructs a striped mutex with the default 2048 stripes.
StripedReadWriteLockSync(int numberOfStripes)
          Constructs a striped mutex with the default 2048 stripes.
 
Method Summary
 Sync getSyncForKey(java.lang.Object key)
          Gets the Mutex 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 Sync getSyncForKey(java.lang.Object key)
Gets the Mutex Stripe to use for a given key.

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

Specified by:
getSyncForKey in interface CacheLockProvider
Parameters:
key - the key
Returns:
one of a limited number of Mutexes.

ehcache

true