Skip to content

Commit ba9e5dc

Browse files
committed
ConcurrentHashMap
1 parent b97328d commit ba9e5dc

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

MD/ConcurrentHashMap.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ConcurrentHashMap 实现原理
2+
3+
由于 `HashMap` 是一个线程不安全的容器,主要体现在容量大于`总量*负载因子`发送扩容时会出现环形链表从而导致死循环。
4+
5+
因此需要支持线程安全的并发容器 `ConcurrentHashMap`
6+
7+
## 数据结构
8+
![](https://ws2.sinaimg.cn/large/006tNc79ly1fn2f5pgxinj30dw0730t7.jpg)
9+
10+
如图所示,是由 Segment 数组,以及 `HashEntry` 数组组成,和 HashMap 一样,仍然是数组加链表组成。
11+
12+
`ConcurrentHashMap` 采用了分段锁技术,其中 `Segment` 继承于 `ReentrantLock`。不会像 `HashTable` 那样不管是 `put` 还是 `get` 操作都需要做同步处理,理论上 ConcurrentHashMap 支持 `CurrencyLevel` (Segment数组数量)的线程并发。每当一个线程占用锁访问一个 `Segment` 时,不会影响到其他的 `Segment`
13+
14+
## get 方法
15+
16+
## put 方法
17+

0 commit comments

Comments
 (0)