ConcurrentHashMap

本文详细介绍了ConcurrentHashMap的特性、构造函数、并发控制原理及其实现细节,突出其在线程安全性和并发操作方面的优势。
java.util.concurrent.ConcurrentHashMap支持检索的完全并发和更新的所期望可调整并发的哈希表。此类遵守与Hashtable相同的功能规范,并且包括对应于Hashtable的每个方法的方法版本。不过,尽管所有操作都是线程安全的,但检索操作不必锁定,并且不支持以某种防止所有访问的方式锁定整个表。此类可以通过程序完全与Hashtable进行互操作,这取决于其线程安全,而与其同步细节无关。
检索操作(包括get)通常不会受阻塞,因此,可能与更新操作交迭(包括put和remove)。
检索会影响最近完成的更新操作的结果。对于一些聚合操作,比如putAll和clear,并发检索可能只影响某些条目的插入和移除。
类似地,在创建迭代器/枚举时或自此之后,Iterators和Enumerations返回在某一时间点上影响哈希表状态的元素。
它们不会抛出ConcurrentModificationException。不过,迭代器被设计成每次仅由一个线程使用。
这允许通过可选的
concurrencyLevel构造方法参数(默认值为16)来引导更新操作之间的并发,
该参数用作内部调整大小的一个提示。表示在内部进行分区的,试图允许指示无争用并发更新的数量。
因为哈希表中的位置基本上是随意的,所以实际的并发将各不相同。理想情况下,应该选择一个尽可能多地容纳并发修改该表的线程的值。
使用一个比所需要的值高很多的值可能会浪费空间和时间,而使用一个显然低很多的值可能导致线程争用

对数量级估计过高或估计过低通常都会带来非常显著的影响。当仅有一个线程将执行修改操作,而其他所有线程都只是执行读取操作时,
才认为某个值是合适的
。此外,重新调整此类或其他任何种类哈希表的大小都是一个相对较慢的操作
因此,在可能的时候,提供构造方法中期望表大小的估计值是一个好主意。
此类及其视图和迭代器实现了Map和Iterator接口的所有可选方法。
此类与Hashtable相似,但与HashMap不同,它“不”允许将null用作键或值。
<wbr style="line-height:25px">注意1</wbr><wbr style="line-height:25px">:它“不”允许将null用作键或值。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:尽管所有操作都是线程安全的,但检索操作不必锁定,不会受阻塞,因此,可能与更新操作交迭(包括put和remove)。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:<span style="color:#993300; line-height:25px">ConcurrentHashMa</span>p允许多个修改操作并发进行,其关键在于使用了锁分离技术。它使用了多个锁来控制对hash表的不同部分进行的修改。<br style="line-height:25px"> 通过可选的<span style="color:#993300; line-height:25px">concurrencyLevel</span>构造方法参数(默认值为16)来设置更新操作之间的并发数。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:关于ConcurrentHashMap的原理及实现细节可以参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107104948143/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ConcurrentHashMap实现细节</a></strong>》<br style="line-height:25px"><div style="line-height:25px"><span style="line-height:25px"><span style="line-height:25px">构造函数</span></span></div> <div style="line-height:25px"> <span style="line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span></span> <table id="pubctors" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Public Constructors</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentHashMap.html#ConcurrentHashMap(int,%20float,%20int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ConcurrentHashMap</a></span>(int initialCapacity, float loadFactor, int concurrencyLevel)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates a new, empty map with the specified initial capacity, load factor and concurrency level.</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentHashMap.html#ConcurrentHashMap(int,%20float)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ConcurrentHashMap</a></span>(int initialCapacity, float loadFactor)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates a new, empty map with the specified initial capacity and load factor and with the default concurrencyLevel (16).</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentHashMap.html#ConcurrentHashMap(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ConcurrentHashMap</a></span>(int initialCapacity)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates a new, empty map with the specified initial capacity, and with default load factor (0.75) and concurrencyLevel (16).</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentHashMap.html#ConcurrentHashMap()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ConcurrentHashMap</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates a new, empty map with a default initial capacity (16), load factor (0.75) and concurrencyLevel (16).</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentHashMap.html#ConcurrentHashMap(java.util.Map&lt;?%20extends%20K,%20?%20extends%20V&gt;)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ConcurrentHashMap</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Map.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Map</a>&lt;?extendsK,?extendsV&gt; m)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates a new map with the same mappings as the given map.</div> </td> </tr> </tbody></table> </div> <div style="line-height:25px"><span style="line-height:25px">结束!</span></div> </wbr></wbr></wbr></wbr>
### ConcurrentHashMap 使用指南 ConcurrentHashMapJava 提供的一种线程安全的 Map 实现,专为高并发环境设计。相比传统的 HashMap 和 Hashtable,它在多线程访问时表现出更高的性能和安全性。通过分段锁机制与 CAS(Compare and Swap)操作,ConcurrentHashMap 能够支持多个线程同时读写而无需全局加锁。 #### 线程安全特性 ConcurrentHashMap 的线程安全特性来源于其内部实现机制。不同于 Hashtable 使用的 synchronized 方法进行全局加锁,ConcurrentHashMap 在 JDK 1.7 及之前版本中采用 **分段锁(Segment Locking)** 技术[^3]。每个 Segment 相当于一个独立的 HashTable,允许多个线程同时访问不同的 Segment,从而减少锁竞争,提高并发性能。 在 JDK 1.8 中,ConcurrentHashMap 进一步优化了其实现,使用 **桶锁(Bucket-level Locking)** 和 **红黑树结构** 来提升性能。此时,锁的粒度更细,仅对当前操作的桶进行加锁,进一步提高了并发能力[^5]。 #### 常用方法与并发操作 ConcurrentHashMap 提供了一系列线程安全的操作方法,例如: - `putIfAbsent(K key, V value)`:只有当指定键不存在或映射值为 null 时才插入键值对。 - `computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)`:如果键存在,则根据提供的函数重新计算该键的值。 - `compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)`:无论键是否存在,都尝试重新计算其映射值。 - `forEach(BiConsumer<? super K, ? super V> action)`:对每个键值对执行指定操作。 这些方法可以在不使用外部同步的情况下安全地用于多线程环境中[^2]。 #### 示例代码 以下是一个简单的示例,展示如何在 Spring Boot 应用中使用 ConcurrentHashMap 管理共享数据: ```java import java.util.concurrent.ConcurrentHashMap; public class UserService { private final ConcurrentHashMap<String, Integer> userCountMap = new ConcurrentHashMap<>(); public void incrementUserCount(String department) { userCountMap.compute(department, (key, oldValue) -> (oldValue == null) ? 1 : oldValue + 1); } public int getUserCount(String department) { return userCountMap.getOrDefault(department, 0); } } ``` 上述代码中的 `compute` 方法确保在多线程环境下对用户计数的更新是线程安全的,无需额外的同步控制[^4]。 #### 适用场景 ConcurrentHashMap 非常适合以下场景: - **缓存系统**:如 Web 应用中缓存用户信息、会话状态等。 - **计数器**:统计在线人数、请求次数等。 - **任务调度**:管理待处理任务队列或资源池。 - **配置中心**:存储可动态更新的配置项。 由于其良好的并发性能,ConcurrentHashMap 成为了构建高性能服务端应用的重要组件之一。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值