Is the read operation in ConcurrentHashMap reliable regarding the return value?

I read in a book that reads in ConcurrentHashmap , it does not guarantee the last updated state, and sometimes it can give a closer meaning. Is it correct?

I read its javadocs and many blogs that seem to say differently (i.e. that's for sure).

Which one is true?

+5
source share
3 answers

Intuitively, ConcurrentHashMap should behave like a set of mutable variables; The keys of the card are variable addresses. get(key) and put(key, value) should behave like mutable read and write.

This is not explicitly stated in the document. Nevertheless, I firmly believe that this is so. Otherwise, there will be many unexpected, amazing behaviors that undermine the application logic. I don't think Doug Lee will do this with us. Of course, someone asks him on the concurrency-interest mailing list.

Suppose it obeys volatile semantics, we can reason based on the Java memory model -

All volatile reads and writes form a single common order. This can be considered a pseudo-temporary string, where read / write are the points on it.

Volatile reading sees the immediate preceding volatile record and sees only the record. The “preceding” here corresponds to the pseudo-timeline.

The pseudo-timeline may differ from the “real” timeline. However, theoretically, volatile recording cannot be infinitely delayed on a pseudo-time line. And, by right, the two lines of time are quite close.

Therefore, we can be sure that the volatile record should become visible “very quickly” for reading.

+1
source

This is the nature of concurrency. Any parallel collection can give you the old value in the field if the current pending write operation is not performed safely. This is in all cases what you should expect. Collections made for concurrency will not give corrupted values ​​or break if you access them from multiple threads, but they can give you old values ​​if the write failed.

0
source

From ConcurrentHashMap javadoc :

Search operations (including get) are usually not blocked, so they may overlap with update operations (including put and remove). Retrievals reflect the results of the latest completed upgrade operations as they began. For cumulative operations, such as putAll and clear, fetching at the same time may reflect the insertion or deletion of only certain entries. Similarly, iterators and enumerations return elements that reflect the state of the hash table at some point in time or after the creation of the iterator / enumeration. They do not throw a ConcurrentModificationException. However, iterators are designed to be used only one thread at a time.

One point to emphasize is that Iterator will not reflect the changes that are created after the creation of Iterator . Therefore, if you need to iterate over the map values, while other streams add to the map, and you care about the current state of the map, then using ConcurrentHashMap may not be the implementation of the map you need.

0
source

All Articles