ConcurrentHashMap使用了哪些锁
时间: 2023-08-17 22:07:59 浏览: 93
ConcurrentHashMap 分段锁消失之谜-cas 源语
ConcurrentHashMap使用了分段锁来实现线程安全。它内部细分成了若干个小的HashMap,每个小的HashMap被称为段(Segment),默认情况下,一个ConcurrentHashMap被细分为16个Segment。每个Segment都是一种可重入锁ReentrantLock,用来保证对该段的操作是线程安全的。当需要对ConcurrentHashMap进行修改时,先通过hashcode来确定元素应该放在哪个Segment中,然后对该Segment进行加锁。这样,不同的线程可以同时对不同的Segment进行操作,从而实现并行的插入和修改。这种分段锁的设计目的是为了细化锁的粒度,提高并发能力。\[1\]\[2\]\[3\]
#### 引用[.reference_title]
- *1* *3* [ConcurrentHashMap分段锁](https://blog.csdn.net/wenqi1992/article/details/126128145)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [ConcurrentHashMap的实现原理是分段锁?你Out了](https://blog.csdn.net/m290345792/article/details/127971797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文