Let's say I have a concurrent map with collections as value:
Map<Integer, List<Integer> map = new ConcurrentHashMap<>();
map.putIfAbsent(8, new ArrayList<>());
and I update the value as follows:
map.computeIfPresent(8, (i, c) -> {
c.add(5);
return c;
});
I know that computeIfPresent
entire method invocation is performed atomically. However, considering this map is accessed by multiple threads concurrently, I'm a little bit concerned about data visibility of the modifications done to the underlying collection. In this case will the value 5 be seen in the list after calling map.get
My question is will change to the list be visible in other threads upon calling map.get
if changes are performed within computeIfPresent
method call.
Please note that I am aware that changes to the list will not be visible if I were to take reference to the list before doing the update operation. I am unsure if the changes to the list will be visible if I take reference to the list (by calling map.get
) after the update operation.
I am unsure how to interpret the docs, but it seems to me that happens-before relationship will guarantee visibility of the changes to the underlying collection in this particular case
More formally, an update operation for a given key bears a happens-before relation with any (non-null) retrieval for that key reporting the updated value