ehcache

net.sf.ehcache.concurrent
Class StripedMutex

java.lang.Object
  extended by net.sf.ehcache.concurrent.StripedMutex

public class StripedMutex
extends java.lang.Object

Provides a number of mutexes 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:
Greg Luck

Field Summary
static int DEFAULT_NUMBER_OF_MUTEXES
          The default number of locks to use.
protected  Mutex[] mutexes
          The array of mutexes
 
Constructor Summary
StripedMutex()
          Constructs a striped mutex with the default 2048 stripes.
StripedMutex(int numberOfStripes)
          Constructs a striped mutex with the default 2048 stripes.
 
Method Summary
 Mutex getMutexForKey(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

mutexes

protected final Mutex[] mutexes
The array of mutexes

Constructor Detail

StripedMutex

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


StripedMutex

public StripedMutex(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

getMutexForKey

public Mutex getMutexForKey(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.

Parameters:
key - the key
Returns:
one of a limited number of Mutexes.

ehcache

true