1. HashMap<K, V>
以put()方法为例,结合JDK源码分析
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n

本文通过分析HashMap和HashSet的JDK 1.8源码,揭示了它们在多线程环境下可能出现的问题。在HashMap中,线程安全问题主要出现在hash碰撞判断和size更新操作;而在HashSet中,由于依赖HashMap,它同样面临线程不安全的挑战。并发put操作可能导致数据覆盖和size计数不准确,体现了这两个集合类在并发场景下的局限性。
最低0.47元/天 解锁文章
2260

被折叠的 条评论
为什么被折叠?



