ConcurrentHashMap and compound operations
collections, java, thread-safety
Solution
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.
Problem
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?