concurrenthashmap

Hashtable HashMap ConcurrentHashMap 源码分析

一笑奈何 提交于 2019-12-01 11:06:12
1.Hashtable与HashMap区别比较 先来说说这两者之间的不同: 1.Hashtable 是JDK1.2出现的, 父类继承Dictionary 实现的是Map, HashMap父类是AbstractMap实现的Map public class Hashtable extends Dictionary implements Map public class HashMap extends AbstractMap implements Map 2. Hashtable中的方法都是同步的, HashMap中的方法都是非同步的, 所以从性能上来说HashMap的效率比Hashtable快, 那么如果在多线程并发的环境下,HashMap如何实现同步处理 可以通过 : Collections.synchronizedMap(); 来实现线程同步, 看下synchronizedMap内部实现: public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) { return new SynchronizedMap<>(m); } private static class SynchronizedMap<K,V> implements Map<K,V>, Serializable { private static final long

Java HashMap 和 ConcurrentHashMap

被刻印的时光 ゝ 提交于 2019-12-01 10:21:00
转自: http://www.codeceo.com/article/java-hashmap-concurrenthashmap.html 前言 Map 这样的 Key Value 在软件开发中是非常经典的结构,常用于在内存中存放数据。 本篇主要想讨论 ConcurrentHashMap 这样一个并发容器,在正式开始之前我觉得有必要谈谈 HashMap,没有它就不会有后面的 ConcurrentHashMap。 HashMap 众所周知 HashMap 底层是基于 数组 + 链表 组成的,不过在 jdk1.7 和 1.8 中具体实现稍有不同。 Base 1.7 1.7 中的数据结构图: 先来看看 1.7 中的实现。 这是 HashMap 中比较核心的几个成员变量;看看分别是什么意思? 初始化桶大小,因为底层是数组,所以这是数组默认的大小。 桶最大值。 默认的负载因子(0.75) table 真正存放数据的数组。 Map 存放数量的大小。 桶大小,可在初始化时显式指定。 负载因子,可在初始化时显式指定。 重点解释下负载因子: 由于给定的 HashMap 的容量大小是固定的,比如默认初始化: public HashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashMap(int

集合 HashMap 的原理,与 Hashtable、ConcurrentHashMap 的区别

心已入冬 提交于 2019-12-01 07:13:36
一、HashMap 的原理 1.HashMap简介 简单来讲,HashMap底层是由 数组+链表 的形式实现,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的,如果定位到的数组位置不含链表(当前entry的next指向null),那么对于查找,添加等操作很快,仅需一次寻址即可;如果定位到的数组包含链表,对于添加操作,首先遍历链表,存在即覆盖,否则新增;对于查找操作来讲,仍需遍历链表,然后通过key对象的equals方法逐一比对查找。当新建一个HashMap的时候,就会初始化一个数组( 数组默认大小是16 ),可以 允许存入null键和null值,线程不安全。 源码如下: /** * The table, resized as necessary. Length MUST Always be a power of two. */ transient Entry[] table; static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; final int hash; …… } 可以看出,Entry就是数组中的元素,每个 Map.Entry 其实就是一个key-value对,它持有一个指向下一个元素的引用,这就构成了链表。 2

ConcurrentHashMap returns a weakly consistent iterator, why should we use it anyhow?

一笑奈何 提交于 2019-12-01 03:34:59
I am reading the book Java Concurrecny in practice. On page 85 section 5.2.1 it talks about the ConcurrentHashMap and its advantages. However, in one part, the books claims that the iterators returned by ConcurrentHashMap is weakly consistent. This means that this iterator can tolerate concurrent modification, traverses elements as they existed when iterator was constructed, and may (but not guaranteed to) reflect modifications to the collection after the construction of the iterator. From why I understand the whole point of synchronization in concurrent programs is to allow thread accessing a

ConcurrentHashMap

泪湿孤枕 提交于 2019-12-01 01:39:03
JDK1.5开始加入了ConcurrentHashMap 一个ConcurrentHashMap实例中包含由若干个Segment实例组成的数组,而一个Segment实例又包含由若干个桶,每个桶中都包含一条由若干个 HashEntry 对象链接起来的链表。特别地,ConcurrentHashMap 在默认并发级别下会创建16个Segment对象的数组,如果键能均匀散列,每个 Segment 大约守护整个散列表中桶总数的 1/16 (通俗:内存直接分为了16个segment,每个segment实际上还是存储的哈希表,写入的时候,先找到对应的segment,然后锁这个segment,写完,解锁,) ConcurrentHashMap具体是怎么实现线程安全的呢,肯定不可能是每个方法加synchronized,那样就变成了HashTable。 从ConcurrentHashMap代码中可以看出,它引入了一个“分段锁”的概念,具体可以理解为把一个大的Map拆分成N个小的HashTable,根据key.hashCode()来决定把key放到哪个HashTable中。 在ConcurrentHashMap中,就是把Map分成了N个Segment,put和get的时候,都是现根据key.hashCode()算出放到哪个Segment中: ConcurrentHashMap 就是一个分段的

深入分析ConcurrentHashMap(VS Hashtable VS HashMap)

不打扰是莪最后的温柔 提交于 2019-12-01 00:59:59
聊聊并发(四)深入分析ConcurrentHashMap 本文是作者原创,发表于InfoQ: http://www.infoq.com/cn/articles/ConcurrentHashMap 术语定义 术语 英文 解释 哈希算法 hash algorithm 是一种将任意内容的输入转换成相同长度输出的加密方式,其输出被称为哈希值。 哈希表 hash table 根据设定的哈希函数H(key)和处理冲突方法将一组关键字映象到一个有限的地址区间上,并以关键字在地址区间中的象作为记录在表中的存储位置,这种表称为哈希表或散列,所得存储位置称为哈希地址或散列地址。 线程不安全的HashMap 因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap。 如以下代码: 01 final HashMap<String, String> map = new HashMap<String, String>(2); 02 03 Thread t = new Thread(new Runnable() { 04 05 @Override 06 07 public void run() { 08 09 for (int i = 0; i < 10000; i++) { 10 11 new Thread(new Runnable(

ConcurrentHashMap深入分析

橙三吉。 提交于 2019-12-01 00:50:05
##Map体系## Hashtable是JDK 5之前Map唯一线程安全的内置实现(Collections.synchronizedMap不算)。Hashtable继承的是Dictionary(Hashtable是其唯一公开的子类),并不继承AbstractMap或者HashMap.尽管Hashtable和HashMap的结构非常类似,但是他们之间并没有多大联系。 ConcurrentHashMap是HashMap的线程安全版本,ConcurrentSkipListMap是TreeMap的线程安全版本。 最终可用的线程安全版本Map实现是ConcurrentHashMap、ConcurrentSkipListMap、Hashtable、Properties四个,但是Hashtable是过时的类库,因此如果可以的应该尽可能的使用ConcurrentHashMap和ConcurrentSkipListMap. ##简介## ConcurrentHashMap 是 java.util.concurrent 包的重要成员。本文将结合 Java 内存模型,分析 JDK 源代码,探索 ConcurrentHashMap 高并发的具体实现机制。 由于 ConcurrentHashMap 的源代码实现依赖于 Java 内存模型,所以阅读本文需要读者了解 Java 内存模型。同时

Hashtable、synchronizedMap、ConcurrentHashMap 比较

家住魔仙堡 提交于 2019-12-01 00:49:52
Doug Lea的util.concurrent包除了包含许多其他有用的并发构造块之外,还包含了一些主要集合类型List和Map的高性能的、线程安全的实现。Brian Goetz向您展示了用ConcurrentHashMap替换Hashtable或synchronizedMap,将有多少并发程序获益。 在Java类库中出现的第一个关联的集合类是Hashtable,它是JDK 1.0的一部分。Hashtable提供了一种易于使用的、线程安全的、关联的map功能,这当然也是方便的。然而,线程安全性是凭代价换来的——Hashtable的所有方法都是同步的。 此时,无竞争的同步会导致可观的性能代价。Hashtable的后继者HashMap是作为JDK1.2中的集合框架的一部分出现的,它通过提供一个不同步的基类和一个同步的包装器Collections.synchronizedMap,解决了线程安全性问题。 通过将基本的功能从线程安全性中分离开来,Collections.synchronizedMap允许需要同步的用户可以拥有同步,而不需要同步的用户则不必为同步付出代价。 Hashtable 和 synchronizedMap所采取的获得同步的简单方法(同步Hashtable中或者同步的Map包装器对象中的每个方法)有两个主要的不足。首先,这种方法对于可伸缩性是一种障碍

ConcurrentHashMap returns a weakly consistent iterator, why should we use it anyhow?

点点圈 提交于 2019-12-01 00:21:17
问题 I am reading the book Java Concurrecny in practice. On page 85 section 5.2.1 it talks about the ConcurrentHashMap and its advantages. However, in one part, the books claims that the iterators returned by ConcurrentHashMap is weakly consistent. This means that this iterator can tolerate concurrent modification, traverses elements as they existed when iterator was constructed, and may (but not guaranteed to) reflect modifications to the collection after the construction of the iterator. From

Is read operation in ConcurrentHashMap reliable regarding the return value?

隐身守侯 提交于 2019-11-30 21:24:35
I read in a book that read in ConcurrentHashmap does not guarantee the most recently updated state and it can sometimes gives the closer value. Is this correct? I have read its javadocs and many blogs which seems to say otherwise (i.e. it is accurate). Which one is true? Intuitively, a ConcurrentHashMap should behave like a set of volatile variables; map keys being the variable addresses. get(key) and put(key, value) should behave like volatile read and write. That is not explicitly stated in the document. However, I would strongly believe that it is the case. Otherwise there will be a lot of