java - Guava - intialCapacity 和 concurrencyLevel 的差异

标签 java guava

Guava 文档说

https://guava.dev/releases/16.0/api/docs/com/google/common/cache/CacheBuilder.html#initialCapacity(int)

Sets the minimum total size for the internal hash tables. For example, if the initial capacity is 60, and the concurrency level is 8, then eight segments are created, each having a hash table of size eight. Providing a large enough estimate at construction time avoids the need for expensive resizing operations later, but setting this value unnecessarily high wastes memory.

如果要创建 8 个 8 行的表,那么使用 initialCapacity 有什么意义呢?这是一些文档错字吗?

最佳答案

在底层,Guava 的缓存被实现为多个哈希表,每个哈希表都有一个专用锁,因此并发写入不会全部竞争单个锁。

initialCapacity 指的是整个缓存的容量,而不是每个单独的基础表的容量。 8 个表,每个表的大小为 8,最多可以处理 64 个元素,因此文档是正确的。

关于java - Guava - intialCapacity 和 concurrencyLevel 的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58956594/

相关文章:

Java - 创建返回偏导数的方法

java - Guava 中 EnumMap 和 ConcurrentHashMap 的组合?

java - Android 上的 Google Collection

guava - SimpleTimeLimiter 关闭

java - Maven:如何处理包含模块的模块?

java - CentOS Java 应用程序出现 SELinux 错误

java - 有没有办法让lombok在使用@Getter @Setter注释时在null的情况下创建一个对象?

java - 如何使用 java 自定义比较器来订购 Spark RDD

java - Guava 缓存/可配置驱逐/缓冲区替换策略

java - 用 Streams 替换 guava Fluent Iterables