Top Description Fields Constructors Methods
net.sf.ehcache.concurrent

public Class StripedReadWriteLockSync

extends Object
implements StripedReadWriteLock
Class Inheritance
All Implemented Interfaces
net.sf.ehcache.concurrent.StripedReadWriteLock, net.sf.ehcache.concurrent.CacheLockProvider
Imports
java.util.Arrays, .Collections, .List, java.util.concurrent.locks.ReadWriteLock, net.sf.ehcache.CacheException

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

Modifier and TypeField and Description
public static final int
DEFAULT_NUMBER_OF_MUTEXES

The default number of locks to use.

private final ReadWriteLockSync[]
private final List<ReadWriteLockSync>

Constructor Summary

AccessConstructor and Description
public
StripedReadWriteLockSync()

Constructs a striped mutex with the default 2048 stripes.

public
StripedReadWriteLockSync(int
- must be a factor of two
numberOfStripes
)

Constructs a striped mutex with the default 2048 stripes.

Method Summary

Modifier and TypeMethod and Description
public List<ReadWriteLockSync>

Returns:

all internal syncs
getAllSyncs
()

Implements net.sf.ehcache.concurrent.StripedReadWriteLock.getAllSyncs.

Returns all internal syncs
public ReadWriteLock

Returns:

one of a limited number of RWLs.
getLockForKey
(final Object
the key
key
)

Implements net.sf.ehcache.concurrent.StripedReadWriteLock.getLockForKey.

Gets the RWL Stripe to use for a given key.
public ReadWriteLockSync

Returns:

one of a limited number of Sync's.
getSyncForKey
(final Object
the key
key
)

Implements net.sf.ehcache.concurrent.CacheLockProvider.getSyncForKey.

Gets the Sync Stripe to use for a given key.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_NUMBER_OF_MUTEXESback to summary
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.

mutexesback to summary
private final ReadWriteLockSync[] mutexes
mutexesAsListback to summary
private final List<ReadWriteLockSync> mutexesAsList

Constructor Detail

StripedReadWriteLockSyncback to summary
public StripedReadWriteLockSync()

Constructs a striped mutex with the default 2048 stripes.

StripedReadWriteLockSyncback to summary
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:int

- must be a factor of two

Method Detail

getAllSyncsback to summary
public List<ReadWriteLockSync> getAllSyncs()

Implements net.sf.ehcache.concurrent.StripedReadWriteLock.getAllSyncs.

Returns all internal syncs

Returns:List<ReadWriteLockSync>

all internal syncs

getLockForKeyback to summary
public ReadWriteLock getLockForKey(final Object key)

Implements net.sf.ehcache.concurrent.StripedReadWriteLock.getLockForKey.

Gets the RWL Stripe to use for a given key.

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

Parameters
key:Object

the key

Returns:ReadWriteLock

one of a limited number of RWLs.

getSyncForKeyback to summary
public ReadWriteLockSync getSyncForKey(final Object key)

Implements net.sf.ehcache.concurrent.CacheLockProvider.getSyncForKey.

Gets the Sync Stripe to use for a given key.

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

Parameters
key:Object

the key

Returns:ReadWriteLockSync

one of a limited number of Sync's.