Home > Enterprise >  concurrentHashMap.merge(key,1,Integer::sum) : Is this operation threadsafe?
concurrentHashMap.merge(key,1,Integer::sum) : Is this operation threadsafe?

Time:02-26

concurrentHashMap.merge(key,1,Integer::sum)

Is the above operation thread-safe? What is the best way to make it threadsafe?

CodePudding user response:

The operation is atomic and hence it is thread-safe, for the docs

The entire method invocation is performed atomically. Some attempted update operations on this map by other threads may be blocked while computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this Map.

CodePudding user response:

ConcurrentHashMap class is thread-safe

  • Related