I have been reading about volatile
and synchronized
in Java but have been scratching my head in confusion. I am hoping someone can help me clear up
Volatile ensures that threads are not caching this value. Volatile ensures write / read of this value from memory is atomic but it doesn't provide locking of this variable over multi step operation.
Replace Map with ConcurrentHashMap and use atomic CAS operation.
Use : ConcurrentHashMap.putIfAbsent()