Can concurrntHashMap guarantee true thread safety and concurrency at the same time?

We know that ConcurrentHashMap can provide simultaneous access to multiple threads to improve performance, and segments are synchronized inside this class (am I right?). The question is, can this design guarantee flow safety? Say we have 30+ threads that access and modify the object associated with the same key in the ConcurrentHashMap instance, in my opinion, they still have to line up for this, right?

From my recollection that the book "Java Concurrency in Practice" says that ConcurrentHashMap provides parallel reading and a decent level of parallel writing. in the above scenario, and if my guess is correct, the performance will not be better than using Collection Api's static synchronization wrapper?

Thanks for the clarification, John.

+3
source share
5 answers

You will still have to synchronize any access to the mutable object, and since you suspect that all access rights to the same key will still be in competition. Increased productivity is provided by access to various keys, which, of course, is a more typical case.

+6
source

a ConcurrentMap concurrency, - ( , .

, () , , . , , .

concurrency , , . , . ConcurrentMap putIfAbsent/replace . , , .

+2

, ?

; , .

. .

, 30 + , , ConcurrentHashMap, , , ?

, , . .

, , ConcurrentHashMap , . , , .

+2

first remember that a thread-protected facility does not guarantee the safe use of the stream in it in itself

The construct if(!map.contains(k))map.put(k,v);for putIfAbsent, for example, is not thread safe.

and every access / modification of the value should still be safe for threads regardless

+1
source

Reads are parallel, even for the same key, so performance will be better for typical applications.

0
source

All Articles