Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConcurrentHashMap and compound operations

Hashtable and Collections.synchronizedMap are thread safe but still compound operations like

if (!map_obj.containsKey(key)) {
   map_obj.put(key, value);
}

needs external synchronization as:

synchronized(map_obj) {
    if (!map_obj.containsKey(key)) {
       map_obj.put(key, value);
    }
}

Suppose we have ConcurrentHashMap(CHM) instead of Hashtable or HashMap. CHM provides an alternative putIfAbsent() method for the above compound operation, thus removing the need of external synchronization.

But suppose there is no putIfAbsent() provided by CHM. Then can we write following code:

synchronized(concurrenthashmap_obj) {
    if (!concurrenthashmap_obj.containsKey(key)) {
       concurrenthashmap_obj.put(key, value);
    }
}

I mean can we use external synchronization on CHM object?Will it work?

For above compound operation there is putIfAbsent() method in CHM but how can we achieve thread safety for other compound operations if we are using CHM. I mean can we use external synchronization on CHM object?

like image 801
a Learner Avatar asked Feb 09 '14 11:02

a Learner


People also ask

What is difference between ConcurrentHashMap and HashMap?

HashMap is non-Synchronized in nature i.e. HashMap is not Thread-safe whereas ConcurrentHashMap is Thread-safe in nature. HashMap performance is relatively high because it is non-synchronized in nature and any number of threads can perform simultaneously.

What is ConcurrentHashMap and how does it work?

ConcurrentHashMap class is thread-safe i.e. multiple threads can operate on a single object without any complications. At a time any number of threads are applicable for a read operation without locking the ConcurrentHashMap object which is not there in HashMap.

What is difference between SynchronizedMap and ConcurrentHashMap?

synchronizedMap() requires each thread to acquire a lock on the entire object for both read/write operations. By comparison, the ConcurrentHashMap allows threads to acquire locks on separate segments of the collection, and make modifications at the same time.

Can 2 threads perform write operations on same ConcurrentHashMap object?

Having two threads that change the map at the very same point time is not possible. Because the code within that ConcurrentHashMap will not allow two threads to manipulate things in parallel!


1 Answers

No, you cannot use external synchronization to ensure atomicity of compound operations over ConcurrentHashMap.

To be precise, you can use external synchronization to ensure atomicity of compound operations, but only if all operations with ConcurrentHashMap are synchronized over the same lock as well (though use of ConcurrentHashMap won't make sense in this case - you can replace it with regular HashMap).

Approach with external synchronization works with Hashtable and Collections.synchronizedMap() only because they guarantee that their primitive operations are synchronized over these objects as well. Since ConcurrentHashMap doesn't provide such a guarantee, primitive operations may interfere with execution of your compound operations, breaking their atomicity.

However, ConcurrentHashMap provides number of methods that can be used to implement compound operations in optimistic manner:

  • putIfAbsent(key, value)
  • remove(key, value)
  • replace(key, value)
  • replace(key, oldValue, newValue)

You can use these operation to implement certain compound operations without explict synchronization, the same way as you would do for AtomicReference, etc.

like image 58
axtavt Avatar answered Sep 23 '22 20:09

axtavt