at.molindo.utils.concurrent
Class KeyLock<K,V>
java.lang.Object
at.molindo.utils.concurrent.KeyLock<K,V>
- Type Parameters:
K - key typeV - return type of Callable
public class KeyLock<K,V>
- extends Object
concurrency utility that locks execution of callable by a key, i.e. not using
key's identity but equality.
- Author:
- stf@molindo.at
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
KeyLock
public KeyLock()
KeyLock
public KeyLock(boolean wait)
- Parameters:
wait - should withLock(Object, Callable) calls wait for
concurrent calls to finish or should they fail fast
newKeyLock
public static <K,V> KeyLock<K,V> newKeyLock()
newKeyLock
public static <K,V> KeyLock<K,V> newKeyLock(boolean wait)
withLock
public V withLock(K key,
Callable<? extends V> callable)
throws Exception
- execute
callable while locking concurrent execution for
key
- Parameters:
key - callable -
- Returns:
-
- Throws:
KeyLock.KeyLockedException - only if key lock configured not to wait and key locked by
other thread
KeyLock.KeyLockedException - any exception thrown by callable.call()
NullPointerException - if key or callable are
null
Exception
replace
protected V replace(V result,
Exception ex)
throws Exception
- how to replace already computed results. by default it's null, as users
might not want to get concurrency issues on shared results. As a
replacement, one could perform a DB lookup if the task is to
create/update a DB entity or do a deep clone of the object.
- Parameters:
result - ex -
- Returns:
-
- Throws:
Exception
activeCount
public int activeCount()
- Returns:
- number of active keys
activeKeys
public List<K> activeKeys()
- Returns:
- a newly created
List of all active keys. Changes to this
list don't have any effect to this KeyLock (changes to
the values will have an undefined effect though).
Copyright © 2013 Molindo GmbH. All Rights Reserved.