Handles in Maps
Hi,
I have three methods for manipulating data: addData, delData, modData.
All the changes in the data are done through handles which are stored in a map.
In each method I want to:
1. lock the map
2. get the data handle
3. lock the data handle
4. unlock the map
5. manipulate data
6. unlock the data handle
What would be the best way to implement this?
Regards,
Miguel
You mean synchronization*, perhaps?* http://java.sun.com/docs/books/tutorial/essential/concurrency/sync.html
I think the synchronization doesn't solve my problem:
synchronize(map){
Handle handle = (Handle)map.get(key);
synchronize(handle){
/* change data*/
(...)
}
I wanted to release the map as soon as I got the handle.
I meant like this:Map<Key, Handle> yourMap = Collections.synchronizedMap(new HashMap<Key, Handle>());// 'yourMap' is now thread safe
Yes the map is thread safe, but the map + handle is not.
> Yes the map is thread safe, but the map + handle is not.
Like this?
Map<Key, Handle> map = Collections.synchronizedMap(new HashMap<Key, Handle>());
Key k = new Key(/*?*/);
Handle h = map.get(k);
synchronized(h) {
// do something with 'h' here
}
Have a better look at the tutorial I posted earlier: it describes it all very well.
That's what I'm doing right now, but it has some potential problems.I really wanted to do a two-phase lock, but I couldn't find examples of it.Any additional hints?
http://en.wikipedia.org/wiki/Strict_two-phase_locking
I really wanted some examples in Java.
Here is what I was thinking:
private ReentrantLock mapLock;
private ReentrantLock objectLock;
private Map map;
public method(Object key){
try{
mapLock.lock();
Object obj = map.get(key);
objectLock.lock();
try{
mapLock.unlock();
/* Do stuff with the object */
}finally{
objectLock.unlock();
}
}finally{
if(mapLock.isHeldByCurrentThread()) mapLock.unlock();
}
}
Message was edited by:
olliegator
All that it really means is that you must have a lock while you're writing and you must release it when you've done. A synchronized{} block does that nicely.
I want to release the map as soon as I get the object, so that other threads are free to get different objects from the map.
You pobably do not need to nest the locks.
If you lock the map you are sure that the structure of the map is safe. The object returned from the get method can then be locked sperately. This second lock ensures the integrity of the object retrieved from the map. This is generally the part you care about. You generally are not interested in the order of events only that they occur atomically.
If this does not satisfy your requirements you may be able to use ConcurrentHashMap and its replace(key, oldvalue, newvalue) method. This is atomic and does not block access to the rest of the map.
remember that synchronization does not order events. It just provides exclusive access. Nested synchronization can order some events but only within the scope of the outermost synchronized block.
matfud
> Nested> synchronization can order some events but only within> the scope of the outermost synchronized block.And if you don't obtain the locks in the same order in all cases, you'll probably end up with deadlock.