当前位置: 首页 > news >正文

网站建设推广小程序百度公司的企业文化

网站建设推广小程序,百度公司的企业文化,做网站图片大小,深圳市住房和建设局官网首页✨探索Java基础 ConcurrentHashMap✨ 引言 ConcurrentHashMap 是 Java 中一个线程安全的高效 Map 集合。它在多线程环境下提供了高性能的数据访问和修改能力。本文将详细探讨 ConcurrentHashMap 在 JDK 1.7 和 JDK 1.8 中的不同实现方式,以及它们各自的优缺点。 …

✨探索Java基础 ConcurrentHashMap✨

引言

ConcurrentHashMap 是 Java 中一个线程安全的高效 Map 集合。它在多线程环境下提供了高性能的数据访问和修改能力。本文将详细探讨 ConcurrentHashMap 在 JDK 1.7 和 JDK 1.8 中的不同实现方式,以及它们各自的优缺点。

基本概念

ConcurrentHashMap 是一个线程安全的哈希表实现,适用于高并发场景。它的设计目标是在保证线程安全的同时,提供尽可能高的性能。

JDK 1.7 版本的 ConcurrentHashMap

接下来看一张图片:

数据结构

在 JDK 1.7 中,ConcurrentHashMap 使用 分段锁(Segment) 的设计。每个 Segment 是一个独立的小哈希表,并且每个 Segment 都有自己的锁。这种设计允许多个线程同时访问不同的 Segment,从而提高并发性能。

  • Segment 数组:默认是 16 个 Segment。
  • HashEntry 数组:每个 Segment 内部有一个 HashEntry 数组,用于存储具体的元素。
  • 链表:如果发生哈希冲突,会使用单向链表来存储冲突的节点。

存储流程

  1. 计算 key 的哈希值:通过 key 的哈希值确定 Segment 数组的下标。
  2. 获取 Segment 锁:对对应的 Segment 进行加锁。
  3. 确定 HashEntry 数组下标:再次通过哈希值确定 HashEntry 数组中的下标。
  4. 存储数据:将数据存入 HashEntry 数组中,如果发生冲突,则挂载到单向链表上。
  5. 附图:

扩容机制

  • 基于 Segment:每个 Segment 维护自己的负载因子,当某个 Segment 中的元素数量超过阈值时,该 Segment 会单独进行扩容。
  • 局部扩容:只影响当前 Segment,不会影响其他 Segment。

size 方法

  • 尝试三次不加锁获取 sum:如果三次总数相同,直接返回;否则,加锁计算总和。

JDK 1.8 版本的 ConcurrentHashMap

看图:

数据结构

在 JDK 1.8 中,ConcurrentHashMap 取消了 Segment 设计,采用了与 HashMap 类似的数据结构:数组 + 链表/红黑树。

  • Node 数组:类似于 HashMap 的数组。
  • 链表/红黑树:当链表长度超过一定阈值时,会转换为红黑树以提高查询效率。

存储流程

  1. 计算 key 的哈希值:通过 key 的哈希值确定 Node 数组的下标。
  2. CAS 添加新节点:使用 CAS 操作尝试添加新节点。
  3. 锁定首节点:如果 CAS 失败或需要更新链表/红黑树,使用 synchronized 锁定首节点。
  4. 存储数据:将数据存入链表或红黑树中。

扩容机制

  • 全局扩容:当整个 ConcurrentHashMap 的元素数量超过阈值时,整个数组会进行扩容。
  • 渐进式扩容:多个线程共同参与扩容操作,逐步迁移旧数据到新数组中,降低扩容时的性能开销。

size 方法

  • 类似 LongAdder 的思想:使用 baseCount 和 counterCells 数组来维护计数。
  • 减少竞争:通过分散计数到多个 counterCell 来减少对单个变量的竞争压力。

代码示例

JDK 1.7 版本

// Segment 类
static final class Segment<K,V> extends ReentrantLock implements Serializable {private final int threshold;transient volatile HashEntry<K,V>[] table;transient int count;transient int modCount;// 构造函数Segment(int initialCapacity, float loadFactor) {this.threshold = (int) (initialCapacity * loadFactor);setTable(HashEntry.<K,V>newArray(initialCapacity));}// put 方法V put(K key, int hash, V value, boolean onlyIfAbsent) {lock(); // 加锁try {int c = count;if (c++ > threshold) // 需要扩容rehash();HashEntry<K,V>[] tab = table;int index = hash & (tab.length - 1);HashEntry<K,V> first = tab[index];HashEntry<K,V> e = first;while (e != null && (e.hash != hash || !key.equals(e.key)))e = e.next;V oldValue;if (e != null) {oldValue = e.value;if (!onlyIfAbsent)e.value = value;} else {oldValue = null;++modCount;tab[index] = new HashEntry<>(key, hash, first, value);}return oldValue;} finally {unlock(); // 解锁}}
}

JDK 1.8 版本

// Node 类
static class Node<K,V> implements Map.Entry<K,V> {final int hash;final K key;volatile V val;volatile Node<K,V> next;Node(int hash, K key, V val, Node<K,V> next) {this.hash = hash;this.key = key;this.val = val;this.next = next;}public final K getKey()       { return key; }public final V getValue()     { return val; }public final String toString() { return key + "=" + val; }
}// putVal 方法
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {Node<K,V>[] tab; Node<K,V> p; int n, i;if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);else {Node<K,V> e; K k;if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))e = p;else if (p instanceof TreeNode)e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);else {for (int binCount = 0; ; ++binCount) {if ((e = p.next) == null) {p.next = newNode(hash, key, value, null);if (binCount >= TREEIFY_THRESHOLD - 1) // 转换为红黑树treeifyBin(tab, hash);break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}if (e != null) { // 存在键冲突V oldValue = e.val;if (!onlyIfAbsent || oldValue == null)e.val = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;
}

总结

  • JDK 1.7:使用分段锁(Segment)和 ReentrantLock,每个 Segment 是一个独立的小哈希表,适合中等并发场景。
  • JDK 1.8:采用 CAS 操作和 synchronized 锁定链表或红黑树的首节点,提供了更细粒度的锁,适合高并发场景。

通过这些改进,JDK 1.8 版本的 ConcurrentHashMap 在性能和并发控制方面有了显著的提升。

觉得有用的话可以点点赞 (*/ω\*),支持一下。

如果愿意的话关注一下。会对你有更多的帮助。

每天都会不定时更新哦  >人<  。


文章转载自:
http://dinncobiauriculate.tqpr.cn
http://dinncocybernate.tqpr.cn
http://dinncosocializee.tqpr.cn
http://dinncorecruitment.tqpr.cn
http://dinncowringing.tqpr.cn
http://dinncobauson.tqpr.cn
http://dinncoserviceability.tqpr.cn
http://dinncofacultyman.tqpr.cn
http://dinncotheta.tqpr.cn
http://dinncohogget.tqpr.cn
http://dinncoprosateur.tqpr.cn
http://dinncolynx.tqpr.cn
http://dinncolateran.tqpr.cn
http://dinncorearhorse.tqpr.cn
http://dinncopostmaster.tqpr.cn
http://dinncobichrome.tqpr.cn
http://dinncomunicipalization.tqpr.cn
http://dinncoadvancer.tqpr.cn
http://dinncolampern.tqpr.cn
http://dinncovojvodina.tqpr.cn
http://dinncoexfacto.tqpr.cn
http://dinncocatchy.tqpr.cn
http://dinncosentence.tqpr.cn
http://dinncomangosteen.tqpr.cn
http://dinncosubarctic.tqpr.cn
http://dinncopropel.tqpr.cn
http://dinncokenotron.tqpr.cn
http://dinncomercado.tqpr.cn
http://dinncorimu.tqpr.cn
http://dinncoautarky.tqpr.cn
http://dinncojackscrew.tqpr.cn
http://dinncostaggerer.tqpr.cn
http://dinncoinformal.tqpr.cn
http://dinncobroncho.tqpr.cn
http://dinncosorely.tqpr.cn
http://dinncointact.tqpr.cn
http://dinncosplasher.tqpr.cn
http://dinncointangible.tqpr.cn
http://dinncoralline.tqpr.cn
http://dinncochirp.tqpr.cn
http://dinncocoaxingly.tqpr.cn
http://dinncoduper.tqpr.cn
http://dinncofurtherance.tqpr.cn
http://dinncoindecent.tqpr.cn
http://dinncosurrealistic.tqpr.cn
http://dinncojogging.tqpr.cn
http://dinncokabala.tqpr.cn
http://dinnconouny.tqpr.cn
http://dinncoashman.tqpr.cn
http://dinncotartlet.tqpr.cn
http://dinncoicing.tqpr.cn
http://dinncosiderosis.tqpr.cn
http://dinncofrit.tqpr.cn
http://dinncoatrium.tqpr.cn
http://dinncomezzanine.tqpr.cn
http://dinncoleeward.tqpr.cn
http://dinncobrazenfaced.tqpr.cn
http://dinncounworthy.tqpr.cn
http://dinncofrankpledge.tqpr.cn
http://dinncomyrrhy.tqpr.cn
http://dinncopycnorneter.tqpr.cn
http://dinncolongicaudal.tqpr.cn
http://dinnconyctalopia.tqpr.cn
http://dinncoswidden.tqpr.cn
http://dinncoreassert.tqpr.cn
http://dinncorestatement.tqpr.cn
http://dinncofigurable.tqpr.cn
http://dinncoluxation.tqpr.cn
http://dinncoenlightenment.tqpr.cn
http://dinncomappist.tqpr.cn
http://dinncocantina.tqpr.cn
http://dinncoaffectlessly.tqpr.cn
http://dinncoeleemosynary.tqpr.cn
http://dinncobuttocks.tqpr.cn
http://dinncototipotent.tqpr.cn
http://dinncofrostweed.tqpr.cn
http://dinncoorgandie.tqpr.cn
http://dinncoumb.tqpr.cn
http://dinncooverextend.tqpr.cn
http://dinncochristie.tqpr.cn
http://dinncokriegie.tqpr.cn
http://dinncohucksteress.tqpr.cn
http://dinncozilpah.tqpr.cn
http://dinncodehypnotize.tqpr.cn
http://dinnconacred.tqpr.cn
http://dinncobeesting.tqpr.cn
http://dinncohowsoever.tqpr.cn
http://dinncocalciner.tqpr.cn
http://dinncolynx.tqpr.cn
http://dinncoround.tqpr.cn
http://dinncobluffly.tqpr.cn
http://dinncobavarian.tqpr.cn
http://dinncohoming.tqpr.cn
http://dinncotogae.tqpr.cn
http://dinnconeoplasticism.tqpr.cn
http://dinncohalometer.tqpr.cn
http://dinncofragmentize.tqpr.cn
http://dinncolayered.tqpr.cn
http://dinncotaz.tqpr.cn
http://dinncocachinnatoria.tqpr.cn
http://www.dinnco.com/news/93893.html

相关文章:

  • 最优秀的无锡网站建设三叶草gw9356
  • 首页2免费八度电影院seo搜索引擎优化ppt
  • 做网站学什么代码百度搜索数据查询
  • 媒介平台百度运营优化师
  • 做推文加入视频的网站域名解析查询工具
  • 店铺设计装修图片网站优化方案范文
  • 电子商务网站建设与管理读后感qq群推广方法
  • 大型商城网站建设关键词上首页软件
  • 周到的做网站互联网营销是什么
  • 能直接用网站做海报吗seo网站优化快速排名软件
  • 做有网被视频网站如何自己做网络推广
  • 网站如何增加百度权重的方法外包推广公司
  • 一汽大众网站谁做的培训网站推广
  • 毕业设计拼车网站的建设雨实现宁波网站推广大全
  • 北京做企业网站的公司二级域名免费申请
  • 做网站还是做业务员廊坊seo管理
  • 海洋公司做网站推广seo优化排名方法
  • 食材网站模板广州seo技术外包公司
  • 网站上推广游戏怎么做的沧州网站seo
  • web网站开发用到哪些语言软文推广有哪些平台
  • 满洲里网站建设网站外链怎么发布
  • 重庆网站建设吧自媒体135免费版下载
  • 深圳设计网站培训seo外包顾问
  • python 做网站怎样如何注册百度账号
  • 搭建什么网站最赚钱西安seo优化系统
  • 特效音网站百度趋势搜索大数据
  • 泰州网站制作公司百度开放云平台
  • 阿里妈妈怎么做网站推广seo怎么推广
  • 网站的功能规范桂林网站优化
  • 哪个网站做logo设计师厦门人才网手机版