sk.baka.tools.concurrent
Interface ILockingStorage<K,V>

Type Parameters:
K - the key type. The key must comply contract to act as a key in Java Map - for example, Object.equals(java.lang.Object) and Object.hashCode() have to be implemented correctly.
V - the object type.
All Known Implementing Classes:
InMemoryLockingStorage

public interface ILockingStorage<K,V>

An object storage, allows thread locking and retrieving of objects stored under a key. Similar to a Map. The main difference is that each entry is protected by a lock: when a thread retrieves an object, the key is locked until the thread puts the object back into the storage. Other threads have to wait to retrieve the same key contents. The storage must handle null values.

Implementation note: implementors usually ask for IValueProducer in their constructors.

Memory consistency effects: actions in a thread prior to unlocking a key in a storage happen-before actions subsequent to the locking of that key in the storage in another thread.

Author:
Martin Vysny

Method Summary
 V lockAndGet(K key)
          Performs an atomic lock on given key and atomically retrieves a value stored under given ID.
 void unlock(K key)
          Atomically unlocks a lock on given key.
 void unlockAndRemove(K key)
          Atomically unlocks a lock with given key and deletes the value object stored under the key.
 

Method Detail

lockAndGet

V lockAndGet(K key)
Performs an atomic lock on given key and atomically retrieves a value stored under given ID. The value is constructed if it is not yet stored in the storage.

Parameters:
key - the key, must not be null. The key must be suitable for insertion into a Map: for example, the Object.equals(java.lang.Object) and Object.hashCode() must be implemented correctly.
Returns:
an object instance.

unlock

void unlock(K key)
Atomically unlocks a lock on given key. The value object is left in the storage.

Parameters:
key - the key to unlock, must not be null.
Throws:
NullPointerException - if no such key was locked.
IllegalArgumentException - if no thread holds a lock for given key.

unlockAndRemove

void unlockAndRemove(K key)
Atomically unlocks a lock with given key and deletes the value object stored under the key.

Parameters:
key - the key, must not be null.
Throws:
NullPointerException - if no such key was locked.
IllegalArgumentException - if no thread holds a lock for given key.


Copyright © 2009-2010. All Rights Reserved.