简体   繁体   中英

Does a thread put a new value in its own segment in ConcurrentHashMap

I have a ConcurrentHashMap and i try to put a key value pair using putIfAbsent(). Now since each thread working on a concurrentHashMap has its own segment which consists of a set of key value pairs . Will a new key value pair be placed in it own segment or there is nothing mandatory like this ?

Thanks

You have a fundamental misunderstanding. Threads do not have their own segment. The mappings of a ConcurrentHashMap are distributed over the segments depending on the hash codes of their keys and in the best case , threads accessing different keys end up at different segments and hence, may work independently.

Threads accessing the same key can never end up at different segments. The principle of a Map that all keys are unique doesn't change.

But this is a description of an outdated technology anyway. Since Java 8, ConcurrentHashMap does not use segments anymore. Depending on the capacity and the hash code distribution, all keys may get updated concurrently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM