I\'m looking into ConcurrentHashMap implementation and have a thing make me confused.
/* Specialized implementations of map methods */
V get(Object
For those interested in an answer from the Doug Lea on this topic, he recently explained the reason for readValueUnderLock
This is in response to someone who had the question:
In the ConcurrentHashMap the get method does not require "readValueUnderLock" because a racing remove does not make the value null. The value never becomes null on the from the removing thread. this means it is possible for get to return a value for key even if the removing thread (on the same key) has progressed till the point of cloning the preceding parts of the list. This is fine so long as it is the desired effect.
But this means "readValueUnderLock" is not required for NEW memory model.
However for the OLD memory model a put may see the value null due to reordering(Rare but possible).
Is my understanding correct.
Response:
Not quite. You are right that it should never be called. However, the JLS/JMM can be read as not absolutely forbidding it from being called because of weaknesses in required ordering relationships among finals vs volatiles set in constructors (key is final, value is volatile), wrt the reads by threads using the entry objects. (In JMM-ese, ordering constraints for finals fall outside of the synchronizes-with relation.) That's the issue the doc comment (pasted below) refers to. No one has ever thought of any practical loophole that a processor/compiler might find to produce a null value read, and it may be provable that none exist (and perhaps someday a JLS/JMM revision will fill in gaps to clarify this), but Bill Pugh once suggested we put this in anyway just for the sake of being conservatively pedantically correct. In retrospect, I'm not so sure this was a good idea, since it leads people to come up with exotic theories.
It can all be viewed here