Package org.kiwiproject.concurrent
Class StripedLock
- java.lang.Object
-
- org.kiwiproject.concurrent.StripedLock
-
public class StripedLock extends Object
StripedLockprovides 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:
Striped,ReadWriteLock- 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
Constructors Constructor Description StripedLock()Creates 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidrunWithReadLock(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 Detail
-
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
public StripedLock(com.google.common.util.concurrent.Striped<ReadWriteLock> lock)
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 Detail
-
runWithReadLock
public void runWithReadLock(String lockKey, Runnable task)
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
public <T> T supplyWithReadLock(String lockKey, Supplier<T> task)
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
public void runWithWriteLock(String lockKey, Runnable task)
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
public <T> T supplyWithWriteLock(String lockKey, Supplier<T> task)
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
-
-