Does a concurrent hashmap not require synchronized getters/setters?

后端 未结 2 675
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 15:14

If i was using a concurrent hashmap and i had methods which set and got values, as im using a concurrent hashmap would i need to make the getter and setter synchronized? Is

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 15:40

    1) If you've getter & setter methods which does only one operation (like get method will only return the map value for the given key) or any thread-safe operation, then you don't need an explicit synchronized block for those getter & setter.

    2) Yes, using concurrent hashmap without synchronized block will greatly improve performance.

    Note : ConcurrentHashMap is weakly consistent which is acceptable in most cases.

提交回复
热议问题