Comments (1)
如果 get 方法要获取的节点是个链表或红黑树
线程 1 在执行扩容操作
final Node<K,V>[] resize() { ... // 线程1正在执行扩容操作,但还未执行到这,所以目前通过get方法获取元素,使用的还是旧数组,计算下标时用的还是旧数组的大小 table = newTab; ... return newTab; }线程 2 执行 get 方法
final Node<K,V> getNode(int hash, Object key) { Node<K,V>[] tab; Node<K,V> first, e; int n; K k; // 线程2仍用旧数组的大小来获取下标位置 if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { ... // 假如发现该位置是个红黑树或链表,准备遍历红黑树或链表,这时已经不需要再使用数组了,所以线程1将当前的数组指向数组也没关系 if ((e = first.next) != null) { if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; }线程 1 继续执行扩容操作
final Node<K,V>[] resize() { ... // 线程1继续往后执行 table = newTab; if (oldTab != null) { // 将旧数组内的元素移到新数组中 for (int j = 0; j < oldCap; ++j) { Node<K,V> e; if ((e = oldTab[j]) != null) { oldTab[j] = null; ... // 红黑树的分割操作 else if (e instanceof TreeNode) ... // 链表的分割操作 else { ... } } } } return newTab; }如果线程 2 现在正在遍历红黑树或链表,这时线程 1 可能正在执行分割操作,也可能已经执行完分割操作。不管怎样,线程 2 当前遍历的红黑树或链表中,节点中维护的指针都可能产生了变化,如果很不巧的,线程 2 想查找的元素,正好在另一个红黑树或链表中,就会导致返回 null,虽然这个元素之前确实存在在该位置
感谢指出,如果感兴趣的话,可以在内容上提交一个PR完善一下,
from javaguide.
Related Issues (20)
- Firefox移动端页面返回到顶部按钮不会跟随页面阅读多少变化 HOT 1
- 死信队列这里的介绍语句 HOT 2
- 索引下推里例子的月份写错了 HOT 2
- Mongodb使用b+树,为什么放一篇b树的论证链接? HOT 1
- Java NIO 核心知识总结文章-个别图片背景是透明的,导致深色模式看不清字 HOT 1
- 构造方法用synchronized关键词修饰解释有误 HOT 1
- 关于线程等待和阻塞两种状态 HOT 1
- 双亲委派模型举例不严谨 HOT 1
- SQL语句中having的执行顺序是在select之前吧 HOT 2
- jdk1.7起,静态变量是位于堆区,而不是方法区
- 内容不严谨,复杂度错误 HOT 1
- SPI机制的设计思路和使用场景没有讲到核心
- 网站已经很久没有从此仓库更新 HOT 2
- HashMap 和 Hashtable 的区别补充
- 不推荐使用外键与级联原因的理解(对作者看法的其他意见) HOT 2
- 文章中一个无效的SQL举例 HOT 1
- 一个小的代码块语法格式笔误 HOT 1
- sql面试题中的一个错误答案 HOT 2
- sql面试题中一个作者想表达意思的疑惑 HOT 2
- 集合-常见面试题下-HashMap 的长度为什么是 2 的幂次方这里逻辑不好 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javaguide.