ConcurrentHashMap
is a thread safe collection, but what happen if ConcurrentHashMap.get("key")
is executing and in meanwhile the map reference itself is changed. Will that produce unexpected result or it will still get the old values(this is expected).
Asked
Active
Viewed 606 times
0

Kaustubh Khare
- 3,280
- 2
- 32
- 48

Java Beginner
- 87
- 10
-
Why do not you try and post your answer here? – Koray Tugay Apr 05 '16 at 08:45
-
an interesting question would be " how a get operation is expected to perform atomically ? " declaring volatile value for a key doesn't guarantee atomicity , if its a 64 bit object there may be two instructions to read it completely right ? – amarnath harish Jan 24 '20 at 17:55
1 Answers
0
If the map reference is being changed, either you still have the reference to the old one, and it will get fro the old map, or you have a reference to the new one, and it will get from the new one.
Setting a reference is an atomic operation, and you will never have a reference pointing to some unexisting object. The visibility of the change itself depends on whether you properly publish the new reference: it should at least be volatile if you intend any other thread to see the new reference immediately after it has been changed.

JB Nizet
- 678,734
- 91
- 1,224
- 1,255