开发者

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

开发者 https://www.devze.com 2023-03-13 12:39 出处:网络
We know that ConcurrentHashMap can provide concurrent access to multiple threads to boost performance , and inside this class, segments are synchronized up (am I right?). Question is, can this design

We know that ConcurrentHashMap can provide concurrent access to multiple threads to boost performance , and inside this class, segments are synchronized up (am I right?). Question is, can this design guarantee the thread safety? Say we have 30+ threads accessing &changing an object mapped by the same key in a ConcurrentHashMap inst开发者_如何学Pythonance, my guess is, they still have to line up for that, don't they?

From my recollection that the book "Java Concurrency in Practice" says the ConcurrentHashMap provide concurrent reading and a decent level of concurrent writing. in the aforementioned scenario, and if my guess is correct, the performance won't be better than using the Collection's static synchonization wrapper api?

Thanks for clarifying, John


You will still have to synchronize any access to the object being modified, and as you suspect all access to the same key will still have contention. The performance improvement comes in access to different keys, which is of course the more typical case.


All a ConcurrentMap can give you wrt to concurrency is that modifications to the map itself are done atomically, and that any writes happen-before any reads (this is important as it provides safe publishing of any reference from the map.

Safe-publishing means that any (mutable) object retrieved from the map will be seen with all writes to it before it was placed in the map. It won't help for publishing modifications that are made after retrieving it though.

However, concurrency and thread-safety is generally hard to reason about and make correct if you have mutable objects that are being modified by multiple parties. Usually you have to lock in order to get it right. A better approach is often to use immutable objects in conjunction with the ConcurrentMap conditional putIfAbsent/replace methods and linearize your algorithm that way. This lock-free style tends to be easier to reason about.


Question is, can this design guarantee the thread safety?

It guarantees the thread safety of the map; i.e. that access and updates on the map have a well defined and orderly behaviour in the presence of multiple threads performing updates simultaneously.

It does guarantee thread safety of the key or value objects. And it does not provide any form of higher level synchronization.

Say we have 30+ threads accessing &changing an object mapped by the same key in a ConcurrentHashMap instance, my guess is, they still have to line up for that, don't they?

If you have multiple threads trying to use the same key, then their operations will inevitably be serialized to some degree. That is unavoidable.

In fact, from briefly looking at the source code, it looks like ConcurrentHashMap falls back to using conventional locks if there is too much contention for a particular segment of the map. And if you have multiple threads trying to access AND update the same key simultaneously, that will trigger locking.


first remember that a thread safe tool doesn't guarantee thread safe usage of it in and of itself

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

and each value access/modification still has to be made thread safe independently


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

0

精彩评论

暂无评论...
验证码 换一张
取 消