Java Concurrency Incrementing a Value

后端 未结 3 478
眼角桃花
眼角桃花 2021-01-03 07:38

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

3条回答
  •  执笔经年
    2021-01-03 08:07

    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()

提交回复
热议问题