jdk1.8 ConcurrentHashMap的变化
来源:互联网 发布:源码修改内核打印级别 编辑:程序博客网 时间:2024/06/10 19:21
jdk以前的版本中ConcurrentHashMap采用分段管理加锁的方式(Segment),今天查看1.8版本的ConcurrentHashMap,发现了一些变化,在这里分享一下。
学习编程,本人感觉最重要的思想,否则只能沦为微软、Sun以及其它公司的“忠实用户”。这里只贴出ConcurrentHashMap新版本的put() 方法,废话说多了
public V put(K key, V value) {
return putVal(key, value, false);
}
/** Implementation for put and putIfAbsent */
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<K,V>[] tab = table;;) {
Node<K,V> 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<K,V>(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) {//最新的只对key对应的第一个元素加锁
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
for (Node<K,V> 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<K,V> pred = e;
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {
Node<K,V> p;
binCount = 2;
if ((p = ((TreeBin<K,V>)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;
}
- jdk1.8 ConcurrentHashMap的变化
- ConcurrentHashMap从jdk1.7到jdk1.8的变化
- ConcurrentHashMap从jdk1.7到jdk1.8的变化
- ConcurrentHashMap的JDK1.8实现
- jdk1.8的HashMap和ConcurrentHashMap
- jdk1.8中ConcurrentHashMap的实现原理
- ConcurrentHashMap的扩容机制(jdk1.8)
- jdk1.8中ConcurrentHashMap的实现原理
- jdk1.8中ConcurrentHashMap的实现原理
- JDK1.6的ConcurrentHashMap
- jdk1.8 ConcurrentHashMap
- jdk1.8 concurrenthashmap
- ConcurrentHashMap--- JDK1.8
- ConcurrentHashMap的jdk1.7源码
- ConcurrentHashMap的JDK1.7实现
- jdk1.8 ConcurrentHashMap实现相关内容
- ConcurrentHashmap源码分析(JDK1.8)
- JDK1.8 中的hashmap和concurrentHashMap
- 创建ActiveX简单步骤
- qml学习------------Grid(网格布局)
- Android模拟器的判定
- Android开发 SDK 4.4 以后访问网络的坑
- 第一篇
- jdk1.8 ConcurrentHashMap的变化
- ByteBuffer Android
- Eclipse导出Android项目时生成keystore的作用
- C#中汉字排序简单示例(拼音/笔划)
- ctrl+c和ctrl+z区别
- Javascript的this用法
- MAMP环境下为Mac OSX设置PHP开发环境
- 抓包工具的使用
- 非局部均值图像恢复的相关理解