从理论上讲,ConcurrentHashMap的Segment和HashMap的桶之间有什么区别? [英] What is the difference between Segment of ConcurrentHashMap and buckets of HashMap theoretically?

查看:617
本文介绍了从理论上讲,ConcurrentHashMap的Segment和HashMap的桶之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在HashMap中,条目(Key,Value)基于散列(Key.hashCode)放置在桶中 - >表示桶位置的索引。如果条目已经放置在该位置,则创建链接列表,并且新条目(如果它具有不同的键 - >通过equals()方法)放置在链接列表的开头。

I understand that in HashMap, the entries (Key, Value) are placed in buckets based on hash(Key.hashCode)--> The index that denotes the bucket location. In case an entry is already placed at that location, there is a linked list created and the new entry (if it has different key --> via equals() method) is placed at the beginning of the linked list.


  1. 我可以将这个概念与ConcurrentHashMap的概念联系起来,但是不是Buckets,而是有各个线程锁定的段。而不是条目,有HashEntry(ies)。以类似的方式,创建链接列表,如果插入的键值对不同,基于键的equals(),它将被放置在链接列表的末尾。

  2. 我正确的时候我说:
    放入CHM是不同步的,因此任何线程都可以访问此方法,此put方法计算传递给它的密钥的哈希值并获取段索引(有点像桶) 。然后,仅针对该段,它调用put方法。现在在Segment下,put方法指定会有一个lock(),这样只有一个线程可以改变特定段中的数据,从而得出结论,如果并发级别为16,则应该有16个线程,因此这些线程将是能够 PUT 一次仅限一个细分市场。

  1. Can i co-relate this concept with that of ConcurrentHashMap, but instead of Buckets, there are Segments upon which individual threads have a lock. And instead of Entries, there are HashEntry(ies). In similar fashion, a linked list is created and if the Key-Value pair being inserted is different, based on equals() of the key, it is placed at end of the linked list.
  2. Am i correct when i say that: put of CHM is not synchronized, thus any thread can access this method, this put method calculates hash value of the key passed to it and gets the segment index (Kinda like buckets). Then for that segment only, it calls the put method. Now under Segment , the put method specifies that there will be a lock(), so that only one thread can alter data in a particular segment, thus concluding that if the concurrency level is 16 there shall be 16 threads and thus these threads will be able to PUT values only one segment at a time.


推荐答案


  1. 存储桶是地图数组中的单个插槽。这与 HashMap ConcurrentHashMap 相同。从概念上讲,后者将其数组分成若干段(每个段是一个引用数组),但就是这样。请注意,Java 8中的CHM不再具有段,它们都是单个数组。

  1. A bucket is an individual slot in the map's array. This is the same with both HashMap and ConcurrentHashMap. Conceptually, the latter has its array broken into segments (each segment is an array of references), but that's it. Note that the CHM in Java 8 no longer has segments, it's all a single array.

是的,它是称为分段锁定的方案。它减少了线程间的争用,但并没有消除它。

Yes, it's the scheme known as segmented locking. It reduces inter-thread contention, but does not eliminate it.

这篇关于从理论上讲,ConcurrentHashMap的Segment和HashMap的桶之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆