ConcurrentHashMap的Segment和HashMap的bucket在理论上有什么区别?

What is the difference between Segment of ConcurrentHashMap and buckets of HashMap theoretically?(ConcurrentHashMap的Segment和HashMap的bucket在理论上有什么区别?)
本文介绍了ConcurrentHashMap的Segment和HashMap的bucket在理论上有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我了解在HashMap中,条目(Key,Value)是根据hash(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,而是有各个线程在其上具有锁的 Segment.而不是 Entries,有 HashEntry(ies).以类似的方式创建一个链表,如果插入的键值对不同,则根据键的 equals() 将其放在链表的末尾.
  2. 我这样说对吗:CHM 的 put 是不同步的,因此任何线程都可以访问这个方法,这个 put 方法计算传递给它的键的哈希值并获取段索引(有点像桶).然后仅针对该段,它调用 put 方法.现在在 Segment 下, put 方法指定将有一个 lock(),因此只有一个线程可以更改特定段中的数据,因此得出结论,如果并发级别为 16,则应有 16 个线程,因此这些线程将是一次只能PUT值一个段.

推荐答案

  1. 桶是地图数组中的一个单独的槽.这对于 HashMapConcurrentHashMap 都是一样的.从概念上讲,后者将其数组分解为段(每个段都是引用数组),仅此而已.请注意,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的bucket在理论上有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)