I always thought that ConcurrentHashMap and similar classes (which support synchronized updates and do not synchronize reading) did a very useful and intuitive thing: they did not block reading and blocked all functionality of updates. And such a strategy retains all coherence.
But I carefully read the documentation and opened the implementation of ConcurrentHashMap , and as I understand it, it does not block reading when another thread is doing updates. And if one thread starts to putAll(hugeCollection) and another thread repeats contains(theSameObjectForAllCalls) at the same time, then the second thread is more likely to get different results while putAll is still running.
Here is the part related to the documents:
For aggregate operations such as putAll and clear, parallel retrieval may reflect the insertion or deletion of only certain entries.
Another interesting thing:
Retrievals reflect the results of the most recently completed operation update performed after they started.
This does not work because of some blockage, but because a new object is added first, and only after the object counter is incremented and the object becomes visible for read operations.
So what is the purpose of updating updates?
java performance multithreading concurrency locking
Roman
source share