jdk7的ConcurrentHashMap实现?

👌jdk7的ConcurrentHashMap实现?

题目详细答案

在JDK 7中,ConcurrentHashMap的实现与JDK 8有所不同。JDK 7中的ConcurrentHashMap使用了分段锁(Segment Locking)来实现高并发性能。

主要结构

JDK 7中的ConcurrentHashMap由以下几个主要部分组成:

  1. Segment:分段锁的核心,每个Segment是一个小的哈希表,拥有独立的锁。
  2. HashEntry:哈希表中的每个节点,存储键值对。
  3. ConcurrentHashMap:包含多个Segment,每个Segment管理一部分哈希表。

Segment 类

Segment类是ReentrantLock的子类,它是ConcurrentHashMap的核心部分。

1
2
3
4
5
6
7
8
9
10
11
12
13
static final class Segment<K,V> extends ReentrantLock implements Serializable {
transient volatile HashEntry<K,V>[] table;
transient int count;
transient int modCount;
transient int threshold;
final float loadFactor;

Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
this.loadFactor = lf;
this.threshold = threshold;
this.table = tab;
}
}

HashEntry 类

HashEntry类是哈希表中的节点,存储键值对和指向下一个节点的指针。

1
2
3
4
5
6
7
8
9
10
11
12
13
static final class HashEntry<K,V> {
final K key;
final int hash;
volatile V value;
volatile HashEntry<K,V> next;

HashEntry(K key, int hash, HashEntry<K,V> next, V value) {
this.key = key;
this.hash = hash;
this.next = next;
this.value = value;
}
}

ConcurrentHashMap 类

ConcurrentHashMap类包含多个Segment,每个Segment管理一部分哈希表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable {

final Segment<K,V>[] segments;
transient Set<K> keySet;
transient Set<Map.Entry<K,V>> entrySet;
transient Collection<V> values;
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
static final int DEFAULT_CONCURRENCY_LEVEL = 16;
static final int MAXIMUM_CAPACITY = 1 << 30;
static final int MIN_SEGMENT_TABLE_CAPACITY = 2;
static final int MAX_SEGMENTS = 1 << 16; // slightly conservative

// Other fields and methods...
}

put 操作

put操作是ConcurrentHashMap的核心操作之一,以下是其简化版实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public V put(K key, V value) {
Segment<K,V> s;
if (value == null)
throw new NullPointerException();
int hash = hash(key);
int j = (hash >>> segmentShift) & segmentMask;
if ((s = (Segment<K,V>)UNSAFE.getObject // nonvolatile; recheck
(segments, (j << SSHIFT) + SBASE)) == null) // in ensureSegment
s = ensureSegment(j);
return s.put(key, hash, value, false);
}

final V put(K key, int hash, V value, boolean onlyIfAbsent) {
HashEntry<K,V> node = tryLock() ? null : scanAndLockForPut(key, hash, value);
V oldValue;
try {
HashEntry<K,V>[] tab = table;
int index = (tab.length - 1) & hash;
HashEntry<K,V> first = entryAt(tab, index);
for (HashEntry<K,V> e = first;;) {
if (e != null) {
K k;
if ((k = e.key) == key || (e.hash == hash && key.equals(k))) {
oldValue = e.value;
if (!onlyIfAbsent) {
e.value = value;
++modCount;
}
break;
}
e = e.next;
} else {
if (node != null)
node.setNext(first);
else
node = new HashEntry<K,V>(key, hash, first, value);
int c = count + 1;
if (c > threshold && tab.length < MAXIMUM_CAPACITY)
rehash(node);
else
setEntryAt(tab, index, node);
++modCount;
count = c;
oldValue = null;
break;
}
}
} finally {
unlock();
}
return oldValue;
}

get 操作

get操作是ConcurrentHashMap的另一个核心操作,以下是其简化版实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public V get(Object key) {
Segment<K,V> s;
HashEntry<K,V>[] tab;
int h = hash(key);
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
(tab = s.table) != null) {
for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
(tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
e != null; e = e.next) {
K k;
if ((k = e.key) == key || (e.hash == h && key.equals(k)))
return e.value;
}
}
return null;
}

主要特点

  1. 分段锁:ConcurrentHashMap将整个哈希表分成多个Segment,每个Segment是一个独立的小哈希表,拥有自己的锁。这样不同的线程可以并发地访问不同的Segment,显著提高并发性能。
  2. 高效并发:通过细粒度的锁机制,ConcurrentHashMap在高并发环境下表现出色,避免了全表锁的性能瓶颈。
  3. 线程安全:所有的操作都在锁的保护下进行,确保了线程安全性。

JDK 7中的ConcurrentHashMap通过分段锁机制实现高并发性能。每个Segment是一个独立的小哈希表,拥有自己的锁,允许多个线程并发地访问不同的Segment。这种设计在高并发环境下显著提高了性能,同时保证了线程安全性。

/krsz9bcuq049gs4t>

 wechat
天生我才必有用