Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consequences of updating other key(s) in ConcurrentHashMap#computeIfAbsent

Javadoc from ConcurrentHashMap#computeIfAbsent says

The computation should be short and simple, and must not attempt to update any other mappings of this map.

But, from what I see, using remove() and clear() methods inside mappingFunction works fine. For example this

Key element = elements.computeIfAbsent(key, e -> {
    if (usages.size() == maxSize) {
        elements.remove(oldest);
    }
    return loader.load(key);
});

What bad consequences of using remove() method inside mappingFunction could be?

like image 346
Vitalii Vitrenko Avatar asked May 28 '17 07:05

Vitalii Vitrenko


People also ask

What will happen if you add a new mapping in ConcurrentHashMap while one thread is iterating over it?

It is guaranteed that things will not break if you do this (that's part of what the "concurrent" in ConcurrentHashMap means). However, there is no guarantee that one thread will see the changes to the map that the other thread performs (without obtaining a new iterator from the map).

Can two threads update the ConcurrentHashMap simultaneously?

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!

What happens happens when we iterate over ConcurrentHashMap and HashMap?

In HashMap, if one thread is iterating the object and the other thread will try to iterate the object, it will throw a runtime exception. But, in ConcurrentHashMap, it is possible to iterate the objects simultaneously by two or more threads.

How do you atomically update a value in ConcurrentHashMap?

The compute(Key, BiFunction) method of ConcurrentHashMap class is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). This method is used to atomically update a value for given key in ConcurrentHashMap.


1 Answers

Here's an example of a bad consequence:

ConcurrentHashMap<Integer,String> cmap = new ConcurrentHashMap<> ();
cmap.computeIfAbsent (1, e-> {cmap.remove (1); return "x";});

This code causes a deadlock.

like image 142
Eran Avatar answered Nov 15 '22 18:11

Eran