java - 是什么导致 ConcurrentHashMap 调整大小

标签 java hashmap concurrenthashmap

我相信一个 ConcurrentHashMap is resized here .

我原以为当负载因子达到某个阈值时会调整 ConcurrentHashMap 的大小。

但是我看不出 addCount 方法的大小调整与加载因子有什么关系。

调整 ConcurrentHashMap 大小的标准是什么?负载因子是其中之一吗?

最佳答案

来自 ConcurrentHashMap 的 javadoc :

The table is resized when occupancy exceeds a percentage threshold (nominally, 0.75, but see below).

The table is dynamically expanded when there are too many collisions (i.e., keys that have distinct hash codes but fall into the same slot modulo the table size), with the expected average effect of maintaining roughly two bins per mapping (corresponding to a 0.75 load factor threshold for resizing). There may be much variance around this average as mappings are added and removed, but overall, this maintains a commonly accepted time/space tradeoff for hash tables. However, resizing this or any other kind of hash table may be a relatively slow operation. When possible, it is a good idea to provide a size estimate as an optional {@code initialCapacity} constructor argument. An additional optional {@code loadFactor} constructor argument provides a further means of customizing initial table capacity by specifying the table density to be used in calculating the amount of space to allocate for the given number of elements. Also, for compatibility with previous versions of this class, constructors may optionally specify an expected {@code concurrencyLevel} as an additional hint for internal sizing.

关于java - 是什么导致 ConcurrentHashMap 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55893118/

相关文章:

java - 在 Java Keystore 中导入 pkcs12 (.p12) 客户端证书文件时,CertificateChain 为空

java - Android HttpURLConnection接收HTTP 301响应代码

c++ - gnu C++ hash_map : erasing a key: defined?

java - ConcurrentHashMap.keySet().removeAll()性能问题

java - 对单元测试 DAO 的疑问

java - 从 servlet-mapping 中剥离图像、CSS 和 JS

java - HashMap 中的 NullPointerException

android - 理解 SoftReference<Bitmap> 在 HashMap 中的作用

java - 为什么 ConcurrentHashMap 中的 get 方法是阻塞的?

java - ConcurrentHashMap 的计算、computeIfAbsent 和computeIfPresent 方法是完全原子的吗?