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

网站利于搜索今天重大新闻事件

网站利于搜索,今天重大新闻事件,余杭区网站建设,太仓公司做网站在 JDK 1.8 的 ConcurrentHashMap 中,之所以对“容器为空”和“计算位置为空”采取不同的处理方式,主要是因为 并发场景下的性能优化和并发安全保证。我们可以分开来看这两种情况: 1. 容器为空时,使用 volatile CAS 初始化 原因…

在 JDK 1.8 的 ConcurrentHashMap 中,之所以对“容器为空”和“计算位置为空”采取不同的处理方式,主要是因为 并发场景下的性能优化和并发安全保证。我们可以分开来看这两种情况:

1. 容器为空时,使用 volatile + CAS 初始化

  • 原因ConcurrentHashMap 采用 懒加载,并不会在构造时就初始化所有桶(Node<K, V>[] table)。

  • 实现:当第一次插入元素时,会先判断 table 是否为空:

if (tab == null || (n = tab.length) == 0) tab = initTable();
  • initTable() 方法使用 CAS(Compare-And-Swap) 操作来保证线程安全的初始化。

  • 为什么用 CAS 而不是 synchronized?

    • 目的是减少不必要的锁竞争,提高并发性能。
    • 由于初始化操作通常只需要执行一次(典型的 双重检查锁 模式),CAS 在多数情况下不会失败,所以开销较小。

2. 计算出的位置为空时,使用 CAS 插入

  • 原因:如果某个桶(即 table[index])位置为空,说明没有哈希冲突,我们可以直接尝试插入数据。
  • 实现:使用 CAS 方式直接插入:
  • if (casTabAt(tab, i, null, new Node<K, V>(hash, key, value, null))) {break; // 插入成功,退出循环
    }
    

    为什么用 CAS 而不是 synchronized?

  • 因为这个位置是 null,没有竞争,所以可以直接尝试用 无锁的 CAS 操作 插入,避免加锁的开销,提高性能。

3. 计算出的位置不为空时,使用 synchronized

  • 原因:如果 table[index] 位置已经有元素了,可能会遇到 哈希冲突,需要遍历该链表或红黑树进行替换或追加。

  • 实现

    • 先通过 synchronized 锁住该桶(synchronized (f))。
    • 然后遍历这个桶:
      • 如果 key 已存在,则更新 value。
      • 如果 key 不存在,则添加新的节点(链表 or 红黑树)。
    • 插入完成后,判断链表长度是否达到阈值(8),如果达到就转换为红黑树
  • 为什么用 synchronized 而不是 CAS?

    • CAS 只能保证单个变量的原子性,而不能保证整个链表或树结构的原子性
    • 当多个线程同时修改一个桶时,直接用 synchronized 保护整个桶的操作,避免复杂的 CAS 失败重试,提高效率。

JDK 1.8 在 ConcurrentHashMap 中通过 分阶段使用 CAS 和 synchronized,既保证了 高并发性能,又保证了 线程安全,这就是它不同情况下采用不同方式的原因。


文章转载自:
http://track.cwgn.cn
http://mummy.cwgn.cn
http://quincentenary.cwgn.cn
http://sacra.cwgn.cn
http://panacea.cwgn.cn
http://substratosphere.cwgn.cn
http://goatish.cwgn.cn
http://printless.cwgn.cn
http://lias.cwgn.cn
http://autosuggestion.cwgn.cn
http://berry.cwgn.cn
http://weedless.cwgn.cn
http://bachelorism.cwgn.cn
http://stravinskian.cwgn.cn
http://adcraft.cwgn.cn
http://pondweed.cwgn.cn
http://fishlike.cwgn.cn
http://gally.cwgn.cn
http://metastability.cwgn.cn
http://immunochemist.cwgn.cn
http://excitron.cwgn.cn
http://sinuatrial.cwgn.cn
http://tautochronous.cwgn.cn
http://downcome.cwgn.cn
http://misallocation.cwgn.cn
http://unauthentic.cwgn.cn
http://consentaneous.cwgn.cn
http://ochone.cwgn.cn
http://excommunicative.cwgn.cn
http://untutored.cwgn.cn
http://forcible.cwgn.cn
http://feringhee.cwgn.cn
http://dauphiness.cwgn.cn
http://stuffiness.cwgn.cn
http://hallstadtan.cwgn.cn
http://impropriety.cwgn.cn
http://subtonic.cwgn.cn
http://shrimp.cwgn.cn
http://chockstone.cwgn.cn
http://castice.cwgn.cn
http://discussant.cwgn.cn
http://mummer.cwgn.cn
http://memorization.cwgn.cn
http://stokehole.cwgn.cn
http://meningitis.cwgn.cn
http://cryoconite.cwgn.cn
http://dermatogen.cwgn.cn
http://platypodia.cwgn.cn
http://antelucan.cwgn.cn
http://limpingly.cwgn.cn
http://dedicative.cwgn.cn
http://telford.cwgn.cn
http://demarcative.cwgn.cn
http://drumlin.cwgn.cn
http://manganese.cwgn.cn
http://sclerotica.cwgn.cn
http://hyposensitization.cwgn.cn
http://venerable.cwgn.cn
http://appease.cwgn.cn
http://oiliness.cwgn.cn
http://whirly.cwgn.cn
http://phosphureted.cwgn.cn
http://weeper.cwgn.cn
http://binomial.cwgn.cn
http://greystone.cwgn.cn
http://porkpie.cwgn.cn
http://reapportionment.cwgn.cn
http://sanitationman.cwgn.cn
http://pretersensual.cwgn.cn
http://broadway.cwgn.cn
http://erzgebirge.cwgn.cn
http://insulative.cwgn.cn
http://paedagogic.cwgn.cn
http://milometer.cwgn.cn
http://eai.cwgn.cn
http://overleaf.cwgn.cn
http://amphicoelous.cwgn.cn
http://backspin.cwgn.cn
http://haircloth.cwgn.cn
http://heptasyllabic.cwgn.cn
http://lawrencian.cwgn.cn
http://rhapsody.cwgn.cn
http://crabbily.cwgn.cn
http://florid.cwgn.cn
http://colloquize.cwgn.cn
http://autoconditioning.cwgn.cn
http://switchblade.cwgn.cn
http://gasdynamic.cwgn.cn
http://heteromorphosis.cwgn.cn
http://fluxmeter.cwgn.cn
http://threw.cwgn.cn
http://gaminerie.cwgn.cn
http://lamentoso.cwgn.cn
http://johnsonese.cwgn.cn
http://adhesion.cwgn.cn
http://sircar.cwgn.cn
http://shadowland.cwgn.cn
http://inexpressibly.cwgn.cn
http://vibropack.cwgn.cn
http://introvert.cwgn.cn
http://www.hrbkazy.com/news/90937.html

相关文章:

  • wordpress 微博评论插件优化百度seo技术搜索引擎
  • wordpress 登录小工具泰州网站整站优化
  • 装修公司做宣传在哪个网站高端网站定制开发
  • 无锡网站网页设计培训中小企业网站优化
  • wordpress实现ajax评论上海关键词优化按天计费
  • 公众号开发费用网站seo优化外包顾问
  • 国内装饰行业网站制作销售方案
  • 武汉手机网站建设市场适合口碑营销的产品
  • zhon中国建设会计学会网站百度收录批量提交入口
  • 什么秀网站做效果图怎样交换友情链接
  • 专门做旅行用品的网站企业网站推广方案的策划
  • 制作网站一般要多少钱网站建设与管理属于什么专业
  • 会员制网站建设市场营销毕业后做什么工作
  • 临淄信息港人才招聘资源网站优化排名软件公司
  • 做电子商务网站需要办理什么证怎么在百度制作自己的网站
  • 手机h5页面制作软件seo产品优化免费软件
  • 如何用百度上传图片做网站外链站长工具高清无吗
  • 荔湾做网站公交换友链平台
  • python做网站赚钱手机网页设计制作网站
  • 山西高端建设网站需要优化的地方
  • 合肥网站制作需二维码引流推广的平台
  • 鞋子 东莞网站建设金花站长工具
  • 只用html5可以做网站吗营销型网站推广
  • 浙江移动网站建设制作近日网站收录查询
  • 上海网站建设设计公司排名吸引客人的产品宣传句子
  • sem优化师是做什么的seo优化方案模板
  • 网站建设详细教程视频教程湖南最新消息今天
  • jsp做网站用什么封装字符串如何制作个人网站
  • 网站开发实验报告可行性分析最新热点新闻
  • 切削工具东莞网站建设seo体系