Package org.kiwiproject.concurrent
Class StripedLock
java.lang.Object
org.kiwiproject.concurrent.StripedLock
StripedLock provides simple lambdas for encapsulating a block of code with a read/write lock.
The number of "stripes" indicates the maximum concurrent processes where the lock "key" is hashed and is used to select a lock. This is useful when you want tasks that operate in the same "context" to block one another without blocking unrelated tasks.
- See Also:
-
StripedReadWriteLock
- Implementation Note:
- This
StripedLockuses Guava'sStripedunder the covers. TheReadWriteLocks are re-entrant, and read locks can be held by multiple readers, while write locks are exclusive.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newStripedLock, usingDEFAULT_KEY_WHEN_BLANKas the number of stripes.StripedLock(int numStripes) Creates a newStripedLockwith the given number of stripes.StripedLock(com.google.common.util.concurrent.Striped<ReadWriteLock> lock) Create a newStripedLockusing the givenReadWriteLock. -
Method Summary
Modifier and TypeMethodDescriptionvoidrunWithReadLock(String lockKey, Runnable task) Execute aRunnabletask using the provided lock key and associated a READ lock.voidrunWithWriteLock(String lockKey, Runnable task) Execute aRunnabletask using the provided lock key and associated a WRITE lock.<T> TsupplyWithReadLock(String lockKey, Supplier<T> task) Execute aSupplierusing the provided lock key and associated READ lock.<T> TsupplyWithWriteLock(String lockKey, Supplier<T> task) Execute aSupplierusing the provided lock key and associated WRITE lock.
-
Constructor Details
-
StripedLock
public StripedLock()Creates a newStripedLock, usingDEFAULT_KEY_WHEN_BLANKas the number of stripes. -
StripedLock
public StripedLock(int numStripes) Creates a newStripedLockwith the given number of stripes.- Parameters:
numStripes- number of stripes- See Also:
-
Striped.readWriteLock(int)
-
StripedLock
Create a newStripedLockusing the givenReadWriteLock. This is useful if you have multiple interdependent usages that you want to share across the same set of locks.- Parameters:
lock- the striped lock to use
-
-
Method Details
-
runWithReadLock
Execute aRunnabletask using the provided lock key and associated a READ lock.This implementation will block until the read lock is acquired.
- Parameters:
lockKey- the lock keytask- the task to run
-
supplyWithReadLock
Execute aSupplierusing the provided lock key and associated READ lock.This implementation will block until the read lock is acquired.
- Type Parameters:
T- the type of object being supplied- Parameters:
lockKey- the lock keytask- the task to supply a value- Returns:
- the supplied value
-
runWithWriteLock
Execute aRunnabletask using the provided lock key and associated a WRITE lock.This implementation will block until the write lock is acquired.
- Parameters:
lockKey- the lock keytask- the task to run
-
supplyWithWriteLock
Execute aSupplierusing the provided lock key and associated WRITE lock.This implementation will block until the write lock is acquired.
- Type Parameters:
T- the type of object being supplied- Parameters:
lockKey- the lock keytask- the task to supply a value- Returns:
- the supplied value
-