ConcurrentHashMap 分析
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/76795383
JDK 1.5 引入了 ConcurrentHashMap 。
ConcurrentHashMap是线程安全且高效的HashMap。
HashTable容器使用synchronized来保证线程安全,但是在线程竞争激烈的情况下,HashTable的效率非常低。
当一个线程访问 HashTable 的同步方法时,其他线程也无法访问其他的同步方法,这样效率就很低下。
ConcurrentHashMap它采锁分段技术 来保证高效的并发操作!
ConcurrentHashMap把容器分为多个 segment(片段) ,每个片段有一把锁,当多线程访问容器里不同数据段的数据时,线程间就不会存在竞争关系;一个线程占用锁访问一个segment的数据时,并不影响另外的线程访问其他segment中的数据。
构造函数
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
static final int DEFAULT_CONCURRENCY_LEVEL = 16;
static final int MAXIMUM_CAPACITY = 1 << 30;
static final int MIN_SEGMENT_TABLE_CAPACITY = 2;
static final int MAX_SEGMENTS = 1 << 16;
static final int RETRIES_BEFORE_LOCK = 2;
public ConcurrentHashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
}
public ConcurrentHashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
}
public ConcurrentHashMap(int initialCapacity, float loadFactor) {
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
}
@SuppressWarnings("unchecked")
public ConcurrentHashMap(int initialCapacity,
float loadFactor, int concurrencyLevel) {
if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
throw new IllegalArgumentException();
if (concurrencyLevel > MAX_SEGMENTS)
concurrencyLevel = MAX_SEGMENTS;
// Find power-of-two sizes best matching arguments
int sshift = 0;
int ssize = 1;
while (ssize < concurrencyLevel) {
++sshift;
ssize <<= 1;
}
this.segmentShift = 32 - sshift;
this.segmentMask = ssize - 1;
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
int c = initialCapacity / ssize;
if (c * ssize < initialCapacity)
++c;
int cap = MIN_SEGMENT_TABLE_CAPACITY;
while (cap < c)
cap <<= 1;
// create segments and segments[0]
Segment<K,V> s0 =
new Segment<K,V>(loadFactor, (int)(cap * loadFactor),
(HashEntry<K,V>[])new HashEntry[cap]);
Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize];
UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
this.segments = ss;
}
https://my.oschina.net/hosee/blog/639352
http://blog.csdn.net/javazejian/article/details/76167357
https://my.oschina.net/hosee/blog/607677
http://www.importnew.com/22007.html
http://blog.csdn.net/xuefeng0707/article/details/40834595
http://www.cnblogs.com/ITtangtang/p/3948786.html
http://www.importnew.com/21781.html
ConcurrentHashMap 分析的更多相关文章
- jdk8 ConcurrentHashMap分析
ConcurrentHashMap分析 tryPresize() transfer() putVal() addCount() sumCount() class ConcurrentHashMap { ...
- 基于JDK1.8的ConcurrentHashMap分析
之前看过ConcurrentHashMap的分析,感觉也了解的七七八八了.但昨晚接到了面试,让我把所知道的ConcurrentHashMap全部说出来. 然后我结结巴巴,然后应该毫无意外的话就G了,今 ...
- java源码-ConcurrentHashMap分析-1
ConcurrentHashMap源码分析 版本jdk8 摈弃了jdk7之前的segement段锁: 首先分析一下put方法,大致的流程就是首先对key取hash函数 判断是否first节点是否存在 ...
- ConcurrentHashMap分析
1.ConcurrentHashMap锁分段技术 ConcurrentHashMap使用锁分段技术,首先将数据分成一段一段地存储,然后给每一段数据配一把锁,当一 ...
- Java并发集合(三)-ConcurrentHashMap分析和使用
1 http://ifeve.com/hashmap-concurrenthashmap-%E7%9B%B8%E4%BF%A1%E7%9C%8B%E5%AE%8C%E8%BF%99%E7%AF%87% ...
- Java 容器源码分析之Map-Set-List
HashMap 的实现原理 HashMap 概述 HashMap 是基于哈希表的 Map 接口的非同步实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.此类不保证映射的顺序 ...
- ConcurrentHashMap 的实现原理
概述 我们在之前的博文中了解到关于 HashMap 和 Hashtable 这两种集合.其中 HashMap 是非线程安全的,当我们只有一个线程在使用 HashMap 的时候,自然不会有问题,但如果涉 ...
- 聊聊并发——深入分析ConcurrentHashMap
术语定义 术语 英文 解释 哈希算法 hash algorithm 是一种将任意内容的输入转换成相同长度输出的加密方式,其输出被称为哈希值. 哈希表 hash table 根据设定的哈希函数H(key ...
- Java---ConcurrentHashMap分析
这是第二次分析concurrentHashMap 先回顾一下 1.concurrentHashMap是在jdk1.5版本之后推出的,位于java.util.concurrent包中. 2.基于Hash ...
随机推荐
- React Native专题-江清清
本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶相关讲解. 刚创建的React Native交流8群:533435865 欢迎各位大牛, ...
- 菜单和按钮-EasyUI Menu 菜单、EasyUI Linkbutton 链接按钮、EasyUI Menubutton 菜单按钮、EasyUI Splitbutton 分割按钮
EasyUI Menu 菜单 通过 $.fn.menu.defaults 重写默认的 defaults. 菜单(Menu)通常用于上下文菜单.它是创建其他菜单组件(比如:menubutton.spli ...
- python3 os.walk()使用
os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. 在Unix,Windows中有效. os.walk(top[, topdown=True[, onerror=Non ...
- 编程当道,学点Python技术好傍身
为了填满AI时代的人才缺口,编程语言教育都从娃娃抓起了!如果你还不懂Python是什么将来怎么给孩子辅导作业呢? Python新手入门教程 近期,浙江省信息技术课程改革方案出台,Python言语现已断 ...
- link标签 rel="stylesheet"
首先,link标签是用于当前文档引用外部文档的,其次,这个标签的rel属性用于设置对象和链接目的间的关系,说白了就是指明你链进来的对象是个什么东西的,具体的值及其所表示的关系如下:Alternate: ...
- xshell过期了怎么办,是学生就用学生版吧
访问这里:https://www.netsarang.com/download/software.html 点击Free for Home & School 下载家庭版和学生版 来到这个页面了 ...
- windows server2003+IIS6+PHP5.3.2
windows下搭建PHP环境有很多种方法.传说,FastCGI下运行PHP 是 兼顾安全和效率的一种.传说.传说.下面讲解在windows server2003 IIS6中安装 PHP 以下文字, ...
- 如何解决tensorflow报:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
答:使能AVX,AVX2和FMA来进行源码编译,这样可以提速噢 具体编译方法,请参考windows10下如何进行源码编译安装tensorflow
- hdu_2048 错排问题
错排问题本质上就是一个动态规划问题,其状态转移方程为: 记d[n]为n个人错排情况的总数. 那么策略可以描述为:分析第n个人错排的可能情况: 1)前n-1个人满足错排的情况,那么第n个人加入后还要错排 ...
- JMeter的下载以及安装使用
下载 https://jmeter.apache.org/download_jmeter.cgi 安装 无须安装,解压之后即可使用. 解压到C:\Program Files\apache-jmeter ...