at.molindo.utils.concurrent
Class KeyLock<K,V>

java.lang.Object
  extended by at.molindo.utils.concurrent.KeyLock<K,V>
Type Parameters:
K - key type
V - 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

Nested Class Summary
static class KeyLock.KeyLockedException
           
 
Constructor Summary
KeyLock()
           
KeyLock(boolean wait)
           
 
Method Summary
 int activeCount()
           
 List<K> activeKeys()
           
static
<K,V> KeyLock<K,V>
newKeyLock()
           
static
<K,V> KeyLock<K,V>
newKeyLock(boolean wait)
           
protected  V replace(V result, Exception ex)
          how to replace already computed results.
 V withLock(K key, Callable<? extends V> callable)
          execute callable while locking concurrent execution for key
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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
Method Detail

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.