Package alluxio.collections
Class LockPool<K>
- java.lang.Object
-
- alluxio.collections.LockPool<K>
-
- Type Parameters:
K
- key for the locks
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class LockPool<K> extends java.lang.Object implements java.io.Closeable
A resource pool specifically designed to contain locks and will NOT evict any entries that are in use. The pool size is unlimited, when the pool size is larger than the configured max size, a background thread will try to evict locks that are no longer locked, but if all the locks are locked, none of them will be evicted. In the worst case (e.g. deadlock), the pool size might keep growing until exhausting system memories.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
boolean
containsKey(K key)
Returns whether the pool contains a particular key.LockResource
get(K key, LockMode mode)
Locks the specified key in the specified mode.RWLockResource
get(K key, LockMode mode, boolean useTryLock)
Locks the specified key in the specified mode.java.util.Map<K,java.util.concurrent.locks.ReentrantReadWriteLock>
getEntryMap()
java.util.concurrent.locks.ReentrantReadWriteLock
getRawReadWriteLock(K key)
Get the raw readwrite lock from the pool.int
size()
java.lang.String
toString()
java.util.Optional<RWLockResource>
tryGet(K key, LockMode mode)
Attempts to take a lock on the given key.
-
-
-
Constructor Detail
-
LockPool
public LockPool(java.util.function.Function<? super K,? extends java.util.concurrent.locks.ReentrantReadWriteLock> defaultLoader, int initialSize, int lowWatermark, int highWatermark, int concurrencyLevel)
Constructor for a lock pool.- Parameters:
defaultLoader
- specify a function to generate a value based on a keyinitialSize
- initial size of the poollowWatermark
- low watermark of the pool sizehighWatermark
- high watermark of the pool sizeconcurrencyLevel
- concurrency level of the pool
-
-
Method Detail
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
get
public LockResource get(K key, LockMode mode)
Locks the specified key in the specified mode.- Parameters:
key
- the key to lockmode
- the mode to lock in- Returns:
- a lock resource which must be closed to unlock the key
-
get
public RWLockResource get(K key, LockMode mode, boolean useTryLock)
Locks the specified key in the specified mode.- Parameters:
key
- the key to lockmode
- the mode to lock inuseTryLock
- Determines whether or not to useLock.tryLock()
orLock.lock()
to acquire the lock. Differs fromtryGet(Object, LockMode)
in that it will block until the lock has been acquired.- Returns:
- a lock resource which must be closed to unlock the key
-
tryGet
public java.util.Optional<RWLockResource> tryGet(K key, LockMode mode)
Attempts to take a lock on the given key.- Parameters:
key
- the key to lockmode
- lockMode to acquire- Returns:
- either empty or a lock resource which must be closed to unlock the key
-
getRawReadWriteLock
public java.util.concurrent.locks.ReentrantReadWriteLock getRawReadWriteLock(K key)
Get the raw readwrite lock from the pool.- Parameters:
key
- key to look up the value- Returns:
- the lock associated with the key
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
containsKey
public boolean containsKey(K key)
Returns whether the pool contains a particular key.- Parameters:
key
- the key to look up in the pool- Returns:
- true if the key is contained in the pool
-
size
public int size()
- Returns:
- the size of the pool
-
getEntryMap
public java.util.Map<K,java.util.concurrent.locks.ReentrantReadWriteLock> getEntryMap()
- Returns:
- all entries in the pool, for debugging purposes
-
-