I have tons of update operation on ConcurrentHashMap and I need to minimize synchronization for update this map.
please see code below.
public ConcurrentHashMap<String, Integer> dataMap = new ConcurrentHashMap<>();
public void updateData(String key, int data) {
if ( dataMap.containsKey(key)) {
// do something with previous value and new value and update it
}
}
For example, When one thread calls updateData
with Key "A", then every other try calling updateData
with key "A" should be blocked until first thread is done. meanwhile, I want another thread trying to call updateData
with key "B" runs concurrently.
I wonder if there is any fancy way to lock hashMap simply with its key.