掌上游戏新活动平台

Hashtable和ConcurrentHashMap如何实现线程安全

2026-01-21 07:30:49

感谢一起重温此知识点的同学--糖糖

HashMap线程不安全,效率高

put方法没有锁

// 任意地方声明HashMap,点击put即可进入源码

HashMap hashMap = new HashMap();

hashMap.put("heart","糖糖");

// HashMap.put(key,value)部分源码

public V put(K key, V value) {

return putVal(hash(key), key, value, false, true);

}

/**

* HashMap.put(key,value)部分sub源码

*/

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,

boolean evict) {

Node[] tab; Node p; int n, i;

if ((tab = table) == null || (n = tab.length) == 0)

n = (tab = resize()).length;

if ((p = tab[i = (n - 1) & hash]) == null)

tab[i] = newNode(hash, key, value, null);

else {

Node e; K k;

if (p.hash == hash &&

((k = p.key) == key || (key != null && key.equals(k))))

e = p;

else if (p instanceof TreeNode)

e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);

else {

for (int binCount = 0; ; ++binCount) {

if ((e = p.next) == null) {

p.next = newNode(hash, key, value, null);

if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st

treeifyBin(tab, hash);

break;

}

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

break;

p = e;

}

}

if (e != null) { // existing mapping for key

V oldValue = e.value;

if (!onlyIfAbsent || oldValue == null)

e.value = value;

afterNodeAccess(e);

return oldValue;

}

}

++modCount;

if (++size > threshold)

resize();

afterNodeInsertion(evict);

return null;

}

HashTable线程安全,效率不高

put方法上加了synchronized锁,每次put都会等锁

// 任意地方声明Hashtable,点击put即可进入源码

Hashtable hashtable = new Hashtable();

hashtable.put("heart","糖糖");

/**

* Hashtable.put(key,value)部分源码

*/

public synchronized V put(K key, V value) {

// Make sure the value is not null

if (value == null) {

throw new NullPointerException();

}

// Makes sure the key is not already in the hashtable.

Entry tab[] = table;

int hash = key.hashCode();

int index = (hash & 0x7FFFFFFF) % tab.length;

@SuppressWarnings("unchecked")

Entry entry = (Entry)tab[index];

for(; entry != null ; entry = entry.next) {

if ((entry.hash == hash) && entry.key.equals(key)) {

V old = entry.value;

entry.value = value;

return old;

}

}

addEntry(hash, key, value, index);

return null;

}

ConcurrentHashMap线程安全,效率高

put方法内部加了分段锁(synchronized锁):Jdk1.7版本默认为16段,可自定义配置;Jdk1.8及以后版本改为N段

// 任意地方声明ConcurrentHashMap,点击put即可进入源码

ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap<>();

concurrentHashMap.put("heart","糖糖");

// ConcurrentHashMap.put(key,value)部分源码

public V put(K key, V value) {

return putVal(key, value, false);

}

/**

* ConcurrentHashMap.put(key,value)部分sub源码

*/

final V putVal(K key, V value, boolean onlyIfAbsent) {

if (key == null || value == null) throw new NullPointerException();

int hash = spread(key.hashCode());

int binCount = 0;

for (Node[] tab = table;;) {

Node f; int n, i, fh;

if (tab == null || (n = tab.length) == 0)

tab = initTable();

else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {

if (casTabAt(tab, i, null,

new Node(hash, key, value, null)))

break; // no lock when adding to empty bin

}

else if ((fh = f.hash) == MOVED)

tab = helpTransfer(tab, f);

else {

V oldVal = null;

synchronized (f) {

if (tabAt(tab, i) == f) {

if (fh >= 0) {

binCount = 1;

for (Node e = f;; ++binCount) {

K ek;

if (e.hash == hash &&

((ek = e.key) == key ||

(ek != null && key.equals(ek)))) {

oldVal = e.val;

if (!onlyIfAbsent)

e.val = value;

break;

}

Node pred = e;

if ((e = e.next) == null) {

pred.next = new Node(hash, key,

value, null);

break;

}

}

}

else if (f instanceof TreeBin) {

Node p;

binCount = 2;

if ((p = ((TreeBin)f).putTreeVal(hash, key,

value)) != null) {

oldVal = p.val;

if (!onlyIfAbsent)

p.val = value;

}

}

}

}

if (binCount != 0) {

if (binCount >= TREEIFY_THRESHOLD)

treeifyBin(tab, i);

if (oldVal != null)

return oldVal;

break;

}

}

}

addCount(1L, binCount);

return null;

}