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 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 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
 Sync[] getAndWriteLockAllSyncForKeys(long timeout, Object... keys)
          Gets and write lock the Sync Stripes to use for the given keys.
 Sync[] getAndWriteLockAllSyncForKeys(Object... keys)
          Gets and write lock the Sync Stripes to use for the given keys.
 ReadWriteLockSync getSyncForKey(Object key)
          Gets the Sync Stripe to use for a given key.
 void unlockWriteLockForAllKeys(Object... keys)
          write unlock the Sync Stripes to use for the given keys.
 
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.

getAndWriteLockAllSyncForKeys

public Sync[] getAndWriteLockAllSyncForKeys(Object... keys)
Gets and write lock the Sync Stripes to use for the given keys.

This lookup must always return the same Sync for a given key. For keys.length > 0, it will return anything between 1 and keys.length Sync's

Specified by:
getAndWriteLockAllSyncForKeys in interface CacheLockProvider
Parameters:
keys - the keys to lock and get syncs for
Returns:
limited number of write locked Sync's matching the keys.

getAndWriteLockAllSyncForKeys

public Sync[] getAndWriteLockAllSyncForKeys(long timeout,
                                            Object... keys)
                                     throws TimeoutException
Gets and write lock the Sync Stripes to use for the given keys.

This lookup must always return the same Sync for a given key. For keys.length > 0, it will return anything between 1 and keys.length Sync's

If all locks cannot be acquired within the specified timeout they are all released and an exception is thrown.

Specified by:
getAndWriteLockAllSyncForKeys in interface CacheLockProvider
Parameters:
timeout - amount of milliseconds before timeout occurs
keys - the keys to lock and get syncs for
Returns:
limited number of write locked Sync's matching the keys.
Throws:
TimeoutException - thrown when locks could not be acquired within specified timeout.

unlockWriteLockForAllKeys

public void unlockWriteLockForAllKeys(Object... keys)
write unlock the Sync Stripes to use for the given keys.

Specified by:
unlockWriteLockForAllKeys in interface CacheLockProvider
Parameters:
keys - the keys to unlock

ehcache

true