@ThreadSafe public class StripedLock extends Object
CacheLoader
implemtations thread safe.
Backed by a set of ReentrantReadWriteLock
instances, and using the key hashcodes
to determine buckets.
Since buckets are used, it doesn't matter that the key in question is not removed from the lock map when no longer in
use, since the key is not referenced in this class. Rather, the hash code is used.
Constructor and Description |
---|
StripedLock()
This constructor just calls
StripedLock(int) with a default concurrency value of 20. |
StripedLock(int concurrency)
Creates a new StripedLock which uses a certain number of shared locks across all elements that need to be locked.
|
Modifier and Type | Method and Description |
---|---|
void |
acquireAllLocks(List<Object> keys,
boolean exclusive)
Acquires locks on keys passed in.
|
boolean |
acquireGlobalLock(boolean exclusive,
long timeout)
Acquires RL on all locks aggregated by this StripedLock, in the given timeout.
|
void |
acquireLock(Object key,
boolean exclusive)
Blocks until a lock is acquired.
|
boolean |
acquireLock(Object key,
boolean exclusive,
long millis) |
void |
downgradeLock(Object key) |
int |
getSharedLockCount() |
int |
getTotalLockCount()
Returns the total number of locks held by this class.
|
int |
getTotalReadLockCount() |
int |
getTotalWriteLockCount() |
void |
releaseAllLocks(List<Object> keys)
Releases locks on all keys passed in.
|
void |
releaseGlobalLock(boolean exclusive) |
void |
releaseLock(Object key)
Releases a lock the caller may be holding.
|
void |
upgradeLock(Object key) |
public StripedLock()
StripedLock(int)
with a default concurrency value of 20.public StripedLock(int concurrency)
concurrency
- number of threads expected to use this class concurrently.public void acquireLock(Object key, boolean exclusive)
exclusive
- if true, a write (exclusive) lock is attempted, otherwise a read (shared) lock is used.public boolean acquireLock(Object key, boolean exclusive, long millis)
public void releaseLock(Object key)
public void upgradeLock(Object key)
public void downgradeLock(Object key)
public void releaseAllLocks(List<Object> keys)
releaseLock(Object)
. This method is
idempotent.keys
- keys to unlockpublic void acquireAllLocks(List<Object> keys, boolean exclusive)
acquireLock(Object, boolean)
keys
- keys to unlockexclusive
- whether locks are exclusive.public int getTotalLockCount()
public boolean acquireGlobalLock(boolean exclusive, long timeout)
public void releaseGlobalLock(boolean exclusive)
public int getTotalReadLockCount()
public int getSharedLockCount()
public int getTotalWriteLockCount()
Copyright © 2021 JBoss by Red Hat. All rights reserved.