Package ch.tocco.nice2.toolbox.api
Class LockPool<K>
java.lang.Object
ch.tocco.nice2.toolbox.api.LockPool<K>
A pool of keyed locks. The pool makes sure that all user's share the same mapping from a key to
a lock while allowing the locks to be garbage collected when unused. Note that using such a
thing is bad practice, but may be useful in some seldom cases. ;)
Example Usage
LockPool.Lock myLock = LockPool.lock("myKey"): try { // code } finally { myLock.unlock(); }
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionGet or create a lock for the key without any action.Get or create a lock for the specified key and acquire the lock.lockInterruptibly
(K key) Get or create a lock for the specified key and acquire the lock interruptibly.@Nullable LockPool.Lock
Get or create a lock for the specified key and acquire the lock without waitinig.@Nullable LockPool.Lock
Get or create a lock for the specified key and acquire the lock waiting until the specified timeout.
-
Constructor Details
-
LockPool
public LockPool()Creates a non-fairLockPool
.- See Also:
-
LockPool
public LockPool(boolean fair) Creates aLockPool
with the specified fairness.- See Also:
-
-
Method Details
-
get
Get or create a lock for the key without any action. -
lock
Get or create a lock for the specified key and acquire the lock.- See Also:
-
lockInterruptibly
Get or create a lock for the specified key and acquire the lock interruptibly.- Throws:
InterruptedException
- See Also:
-
tryLock
Get or create a lock for the specified key and acquire the lock without waitinig.- Returns:
null
if the lock could not be acquired.- See Also:
-
tryLock
@Nullable public @Nullable LockPool.Lock tryLock(K key, long time, TimeUnit unit) throws InterruptedException Get or create a lock for the specified key and acquire the lock waiting until the specified timeout.- Returns:
null
if the lock could not be acquired.- Throws:
InterruptedException
- See Also:
-