1. HashMap

1) 并发问题

HashMap的并发问题源于多线程访问HashMap时, 如果存在修改Map的结构的操作(增删, 不包括修改), 则有可能会发生并发问题, 表现就是get()操作会进入无限循环

    public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue();
}
    final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
} int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}

究其原因, 是因为 getEntry 先获取了 table 中的链表, 而链表是一个循环链表, 所以进入了无限循环, 在正常情况下, 链表并不会出现循环的情况

出现这种情况是在多线程进行put的时候, 因为put会触发resize(rehash)操作, 当多个rehash同时发生时, 链表就有可能变得错乱, 变成一个循环链表

    void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
} createEntry(hash, key, value, bucketIndex);
} void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
} Entry[] newTable = new Entry[newCapacity];
transfer(newTable, initHashSeedAsNeeded(newCapacity)); // transfer 方法对所有Entry进行了rehash
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
} void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}

多线程resize的时候会同时创建多个newTable, 然后同时rehash, 造成链表错乱

另外rehash对于hashmap的性能代价也是相当大的, 所以选择一个合适的table长度也是很重要的

2) iterator 与 fail-fast

遍历的两种方法

for (int i = 0; i < collection.size(); i++) {
T t = collection.get(i)
// ...
} for (T t : collection) {
// ...
}

为什么使用iterator, 是因为有的数据结构 get(i) 的效率是O(n), 而非O(1), 例如 LinkedList, 那么整个循环的效率则会变为 O(n2)

iterator内部使用fail-fast机制来提醒并发问题的发生, 例如在遍历的时候同时修改map, 则会抛出ConcurrentModificationException异常

for (Entry<K, V> t : map) {
map.remove(t.key);
// Exception throw
}

之所以抛出异常是因为在遍历的时候同时修改map, 会导致一些意想不到的情况发生

1) remove 操作.

假如在遍历的时候进行remove , 则有可能拿到的当前元素变为空, 导致遍历无法往下进行, 而直接跳到hashMap table的下一个槽位, 丢失整个槽位的链表数据

    final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
} int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) { // 例如这里的 e.next 在遍历的时候被删除, 则会导致这个槽位的元素全被跳过
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}

2) put 操作

put操作的resize会导致table链表重新分配, 遍历则会变得混乱, 不再赘述

2. ConcurrentHashMap

ConcurrentHashMap是HashMap的线程安全实现, 不同于HashTable, 他并不是用对整个HashMap使用synchronized来保证同步, 而是对map进行分段, 在插入时只使用重入锁锁定特定的段

这样根据段位的数量则可以达到不同的并发数量, 所以在使用他时可以根据我们的并发线程来定制这个段的数量.

1) segment的数量是ssize = 1 << concurrencyLevel, 默认 DEFAULT_CONCURRENCY_LEVEL = 16

2) 每个segment的长度是 initialCapacity / ssize, 最小值为 MIN_SEGMENT_TABLE_CAPACITY = 2

同样, 他的Iterator也不同于传统的HashIterator, 他并不会抛出ConcurrentModificationException, 这是因为他的遍历器的next()方法, 每次都是返回一个new的WriteThroughEntry, 这个东西保证了你在获取到Entry以后即使Map遭到修改, 也不会影响你当前遍历的结果. 但是, 如果你对WriteThroughEntry进行setValue操作, 还是可以影响到原来的map的, 代码如下

final class EntryIterator
extends HashIterator
implements Iterator<Entry<K,V>>
{
public Map.Entry<K,V> next() {
HashEntry<K,V> e = super.nextEntry();
return new WriteThroughEntry(e.key, e.value);
}
} /**
* Custom Entry class used by EntryIterator.next(), that relays
* setValue changes to the underlying map.
*/
final class WriteThroughEntry
extends AbstractMap.SimpleEntry<K,V>
{
WriteThroughEntry(K k, V v) {
super(k,v);
} /**
* Set our entry's value and write through to the map. The
* value to return is somewhat arbitrary here. Since a
* WriteThroughEntry does not necessarily track asynchronous
* changes, the most recent "previous" value could be
* different from what we return (or could even have been
* removed in which case the put will re-establish). We do not
* and cannot guarantee more.
*/
public V setValue(V value) {
if (value == null) throw new NullPointerException();
V v = super.setValue(value);
ConcurrentHashMap.this.put(getKey(), value); // 将改变写入到原来的map中
return v;
}
}

HashMap 与 ConcurrentHashMap的更多相关文章

  1. [Java集合] 彻底搞懂HashMap,HashTable,ConcurrentHashMap之关联.

    注: 今天看到的一篇讲hashMap,hashTable,concurrentHashMap很透彻的一篇文章, 感谢原作者的分享. 原文地址: http://blog.csdn.net/zhanger ...

  2. HashMap和ConcurrentHashMap流程图

    本文表达HashMap和ConcurrentHashMap中的put()方法的执行流程图,基于JDK1.8的源码执行过程. HashMap的put()方法: ConcurrentHashMap的put ...

  3. HashMap与ConcurrentHashMap的测试报告

    日期:2008-9-10 测试平台: CPU:Intel Pentium(R) 4 CPU 3.06G 内存:4G 操作系统:window server 2003 一.HashMap与Concurre ...

  4. 轻松理解 Java HashMap 和 ConcurrentHashMap

    前言 Map 这样的 Key Value 在软件开发中是非常经典的结构,常用于在内存中存放数据. 本篇主要想讨论 ConcurrentHashMap 这样一个并发容器,在正式开始之前我觉得有必要谈谈 ...

  5. Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析

    Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 今天发一篇”水文”,可能很多读者都会表示不理解,不过我想把它作为并发序列文章中不可缺少的一块来介绍.本来以为花不了 ...

  6. Java中关于Map的使用(HashMap、ConcurrentHashMap)

    在日常开发中Map可能是Java集合框架中最常用的一个类了,当我们常规使用HashMap时可能会经常看到以下这种代码: Map<Integer, String> hashMap = new ...

  7. Java7/8 中 HashMap 和 ConcurrentHashMap的对比和分析

    大家可能平时用HashMap比较多,相对于ConcurrentHashMap 来说并不是很熟悉.ConcurrentHashMap 是 JDK 1.5 添加的新集合,用来保证线程安全性,提升 Map ...

  8. 高并发第九弹:逃不掉的Map --> HashMap,TreeMap,ConcurrentHashMap

    平时大家都会经常使用到 Map,面试的时候又经常会遇到问Map的,其中主要就是 ConcurrentHashMap,在说ConcurrentHashMap.我们还是先看一下, 其他两个基础的 Map ...

  9. 深入理解HashMap和concurrentHashMap

    原文链接:https://segmentfault.com/a/1190000015726870 前言 Map 这样的 Key Value 在软件开发中是非常经典的结构,常用于在内存中存放数据. 本篇 ...

  10. 沉淀再出发:java中的HashMap、ConcurrentHashMap和Hashtable的认识

    沉淀再出发:java中的HashMap.ConcurrentHashMap和Hashtable的认识 一.前言 很多知识在学习或者使用了之后总是会忘记的,但是如果把这些只是背后的原理理解了,并且记忆下 ...

随机推荐

  1. java okhttp 发送图片

    @RequestMapping(value="/demo2", method=RequestMethod.POST) @ResponseBody public String dem ...

  2. .NET工作准备--04ASP.NET

    (已过时) ASP.NET 1.开发基础 *asp.net以什么形式运行?.net宿主的概念,ISAPI的概念,ASP.NET基本运行机制; .net宿主的概念:CLR被实现为一个标准的COM服务器组 ...

  3. vue-router填坑之路

    1.在结构化css时,习惯将不同的css文件通过一个入口文件打包,而入口文件在引入其他css文件时,需要强烈注意,要在单行末尾加分号: 少分号的,单行相对应的css文件会引用无效 @import '. ...

  4. 表达式树(Expression Tree)

    饮水思源 本文并非原创而是下面网址的一个学习笔记 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/e ...

  5. web前端面试经历分享

    十天前,我还在纠结这个暑假到底是呆在实验室研究技术好还是找一份实习见识世面好,而现在我已经接到offer准备工作了.这几天真是累得够呛,一方面需要拼命准备期末考试,另一方面,需要往公司里面跑接受面试. ...

  6. HTTP协议-缓存

    HTTP 协议中,缓存更多关心的文档资源的再利用.其目的是减少数据传输,加快相应速度等等.而对于缓存采用的是什么方案,也就是存在内存中还是硬盘中之类的问题,就属于另外的内容了. 假设,我身在广东,但是 ...

  7. [BZOJ3757]苹果树(树上莫队)

    树上莫队共有三种写法: 1.按DFS序列分块,和普通莫队类似.常数大,不会被卡. 2.按块状树的方式分块.常数小,会被菊花图卡到O(n). 3.按[BZOJ1086]王室联邦的方式分块.常数小,不会被 ...

  8. BZOJ.2938.[POI2000]病毒(AC自动机)

    题目链接 \(Description\) 给n个模式串,问是否存在长度无限的主串,使得任何一个模式串都没有在主串中出现. \(Solution\) 先建AC自动机. 假设我们有了一个无限长的安全代码, ...

  9. hdu 4407 容斥原理

    题意: 1 //一组数据 3 3 //数字为1-3,3次运算 2 2 3 //将2号位变成3 1 1 3 4 //计算1-3号位上与4互质的数的和 1 2 3 6 好题,需要重复练习 #include ...

  10. Spring(完成毕业设计后的简单回顾)

    最近刚刚做完了毕业设计,在开发时用的是spring框架,做的时候踩了好多坑,又把当初的笔记给翻了翻,做一次简单的回顾 # 1.Spring是什么? 是一个开源的.用于简化企业级应用开发的应用开发框架. ...