Is ConcurrentHashMap.putAll (...) "atomic?

Is the ConcurrentHashMap.putAll (Map) method a putative atom?

I cannot find it in the documentation and it is not mentioned in the ConcurrentMap interface, so I think the answer is no. I ask him to be sure, because it would not make sense if this operation was not atomic, to be honest.

If it is not atomic, what would be the best way to support atomic inserts of several elements? Back to the good old sync?

+8
java atomic concurrenthashmap
source share
4 answers

It is not atomic, no. According to class documentation :

For aggregate operations such as putAll and clear , fetching at the same time may reflect the insertion or deletion of only certain entries.

To atomize it, you will need to use synchronized , yes. There is no non-blocking way to do this.

+9
source share

top document

For aggregate operations, such as putAll and clear , simultaneous searches may only reflect the insertion or deletion of certain records.

+3
source share

putAll() not atomic, but simply ensures that every single put() is atomic.

+2
source share

To atomize it, you have to use synchronized, yes

Not only that: you must place a synchronized block around each open card method, thereby reducing concurrency.

+2
source share

All Articles