ehcache

net.sf.ehcache.concurrent
Class StripedReadWriteLockSync

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

public class StripedReadWriteLockSync
extends 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
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
 List<ReadWriteLockSync> getAllSyncs()
          Returns all internal syncs
 ReadWriteLock getLockForKey(Object key)
          Gets the RWL Stripe to use for a given key.
 ReadWriteLockSync getSyncForKey(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(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 ReadWriteLock getLockForKey(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.

getAllSyncs

public List<ReadWriteLockSync> getAllSyncs()
Returns all internal syncs

Specified by:
getAllSyncs in interface StripedReadWriteLock
Returns:
all internal syncs

ehcache

Copyright 2001-2015, Terracotta, Inc.