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

祁东网站设计公司seo自学网

祁东网站设计公司,seo自学网,有关php动态网站建设的图片,苏州建设公司网站建设Hashtable和HashMap的区别 HashMap和Hashtable都是哈希表数据结构,但是Hashtable是线程安全的,HashMap是线程不安全的 Hashtable实现线程安全就是简单的把关键方法都加上了synchronized关键字 直接在方法上添加synchronized相当于针对this对象&#xff0…

Hashtable和HashMap的区别

        HashMap和Hashtable都是哈希表数据结构,但是Hashtable是线程安全的,HashMap是线程不安全的

        Hashtable实现线程安全就是简单的把关键方法都加上了synchronized关键字

        直接在方法上添加synchronized相当于针对this对象(Hashtable对象)加锁,一个Hashtable只有一把锁,所以当我们对Hashtable执行任何操作的时候都会触发锁冲突,效率是非常低的

        Hashtable是存在很多问题

        1.

        由于直接对方法进行加锁,所以当有很多线程需要访问Hashtable时就会导致严重的锁冲突

         2.

       当我们向Hashtable中添加数据时,对应的size属性应该增加,此时就涉及到多个线程修改同一个变量,存在线程安全问题,而size属性也是通过synchronized来解决线程安全问题的,所以效率会很低

         3.

       哈希表有一个特性,当存放到哈希表中的数据越来越多,冲突率达到一定的数值,就会触发扩容,一旦触发扩容,就会由当前的线程去完成整个扩容过程,这个过程涉及到大量的元素拷贝,效率会很低

        可能会出现一种情况:其他的线程都执行得很流畅,突然有一个线程就卡住了,并且卡了很长的时间,此时这个线程就可能触发了哈希表的扩容,这个扩容过程持续的时间很长

ConcurrentHashMap和Hashtable的区别

        ConcurrentHashMap是线程安全的,并且在多线程编程中ConcurrentHashMap 是比Hashtable实用很多的

        ConcurrentHashMap相比于Hashtable做了很多的优化

        1.

        读操作没有加锁,只对写操作加锁,加锁用的也是synchronized,但是不是直接在方法上进行加锁,而是针对哈希表上的每个链表加锁,并且加锁用的也不是this对象,而是直接用每个链表的头结点作为锁对象进行加锁,相当于哈希表上每个链表都有各自的锁对象

        为什么要这样设计呢?

        因为多个线程对哈希表进行访问时,如果访问的是哈希表上不同的链表,实际上是不会出现线程安全问题的,只有多个线程访问哈希表上的同一个链表才会出现线程安全问题,所以我们不需要对整个哈希表加锁,我们只需要对每个链表进行加锁即可

        加这么多锁不会消耗很多资源吗?

        可能会有细心的小伙伴有这样的问题,但实际上我们用synchronized进行加锁时,一开始加上的是一个偏向锁,偏向锁就相当于是一个标记,此时并没有真正的实例化一个锁出来,也就没有消耗什么资源,只有当多个线程争夺同一把锁(出现锁竞争)时才会去真正的实例化具体的锁,而实际上多个线程去修改同一个链表上的数据,这种情况是很少发生的,所以我们加上的锁大多数都只是一个偏向锁,不消耗什么资源

        2.

        充分利用 CAS 特性. 比如 size 属性通过 CAS 来更新. 避免出现重量级锁的情况.(关于CAS推荐看CAS的ABA问题)

        3.

        优化了扩容方式,化整为零

        当莫一个线程触发了哈希表的扩容操作,此时不需要这个线程完成所有的扩容过程,当前这个线程只需要创建一个更大的哈希表,然后搬运一小部分数据即可

        在扩容的期间,新旧数组是同时存在的,之后每个访问ConcurrentHashMap的线程都会参与对数据的搬运,每个操作负责搬运一部分元素

        在这个期间,我们修改数据会将数据直接修改到新数组上,添加数据会直接添加到新数组上,查找数据是新旧数组一起查找

        当旧数组上的数据被全部搬运完成后,便会删除旧数组

ConcurrentHashMap的锁分段技术

        简单的说就是把若干个哈希桶分成一个 "段" (Segment), 针对每个段分别加锁. 目的也是为了降低锁竞争的概率. 当两个线程访问的数据恰好在同一个段上的时候, 才触发锁竞争.,但现在锁分段技术技术已经被淘汰了,因为要对哈希表的链表进行分段,管理起来是很麻烦的,不如直接对每个链表进行加锁(锁对象是链表的头结点)


文章转载自:
http://dinncobrummie.ssfq.cn
http://dinncounappalled.ssfq.cn
http://dinncopertinent.ssfq.cn
http://dinncovertigines.ssfq.cn
http://dinncotypograph.ssfq.cn
http://dinncotutorage.ssfq.cn
http://dinncograssland.ssfq.cn
http://dinncoshamoy.ssfq.cn
http://dinncodenature.ssfq.cn
http://dinncorode.ssfq.cn
http://dinncosierran.ssfq.cn
http://dinncopersuasive.ssfq.cn
http://dinncotickey.ssfq.cn
http://dinncotermagant.ssfq.cn
http://dinncototipalmation.ssfq.cn
http://dinncoheterogeneous.ssfq.cn
http://dinncocampus.ssfq.cn
http://dinncoathwartships.ssfq.cn
http://dinncohomeochromatic.ssfq.cn
http://dinncomammogenic.ssfq.cn
http://dinncoaheap.ssfq.cn
http://dinncowade.ssfq.cn
http://dinncospoilt.ssfq.cn
http://dinncohornfels.ssfq.cn
http://dinncogeological.ssfq.cn
http://dinncoliteralise.ssfq.cn
http://dinncoassuror.ssfq.cn
http://dinncogallate.ssfq.cn
http://dinncorecrudescence.ssfq.cn
http://dinncocontaminated.ssfq.cn
http://dinncoirreverence.ssfq.cn
http://dinncojehovic.ssfq.cn
http://dinncofate.ssfq.cn
http://dinncocry.ssfq.cn
http://dinncohypersusceptibility.ssfq.cn
http://dinncophototelegram.ssfq.cn
http://dinncoendocytose.ssfq.cn
http://dinncohanoi.ssfq.cn
http://dinncoseise.ssfq.cn
http://dinncoamidohydrolase.ssfq.cn
http://dinncowashleather.ssfq.cn
http://dinncouxorious.ssfq.cn
http://dinncohypoproteinemia.ssfq.cn
http://dinncochartism.ssfq.cn
http://dinncohydroponic.ssfq.cn
http://dinncodeterminism.ssfq.cn
http://dinncoidiosyncratic.ssfq.cn
http://dinncosalinogenic.ssfq.cn
http://dinncobuhr.ssfq.cn
http://dinncoclever.ssfq.cn
http://dinncoviewy.ssfq.cn
http://dinncovigorous.ssfq.cn
http://dinncostonecast.ssfq.cn
http://dinncocombat.ssfq.cn
http://dinncostake.ssfq.cn
http://dinncomolokai.ssfq.cn
http://dinncoaltocumulus.ssfq.cn
http://dinncoerective.ssfq.cn
http://dinncopree.ssfq.cn
http://dinncomesquit.ssfq.cn
http://dinncomishanter.ssfq.cn
http://dinncocommunalistic.ssfq.cn
http://dinnconeuroplasm.ssfq.cn
http://dinncoconcerning.ssfq.cn
http://dinncorewater.ssfq.cn
http://dinncoblin.ssfq.cn
http://dinncoachillean.ssfq.cn
http://dinncoroughdraw.ssfq.cn
http://dinncomalines.ssfq.cn
http://dinncomenado.ssfq.cn
http://dinncorustle.ssfq.cn
http://dinncoasbestoid.ssfq.cn
http://dinncocatheter.ssfq.cn
http://dinncofestschrift.ssfq.cn
http://dinncovs.ssfq.cn
http://dinncopomposo.ssfq.cn
http://dinncoincendive.ssfq.cn
http://dinncomirthquake.ssfq.cn
http://dinncopancreatectomize.ssfq.cn
http://dinncouniverse.ssfq.cn
http://dinncorailhead.ssfq.cn
http://dinncosealant.ssfq.cn
http://dinncoandrea.ssfq.cn
http://dinncomether.ssfq.cn
http://dinncoprogamete.ssfq.cn
http://dinncoimpersonality.ssfq.cn
http://dinncomaintainable.ssfq.cn
http://dinnconutrimental.ssfq.cn
http://dinncodisruptive.ssfq.cn
http://dinncomunshi.ssfq.cn
http://dinncowringing.ssfq.cn
http://dinncoarchducal.ssfq.cn
http://dinncoflagellated.ssfq.cn
http://dinncogardner.ssfq.cn
http://dinncofreckling.ssfq.cn
http://dinncologanberry.ssfq.cn
http://dinncokaszube.ssfq.cn
http://dinncosharecrop.ssfq.cn
http://dinncosomaliland.ssfq.cn
http://dinncodisequilibrium.ssfq.cn
http://www.dinnco.com/news/160489.html

相关文章:

  • 做室内3d设计的网站陕西新闻今日头条
  • 齐齐哈尔建设局网站首页seo推广是什么意思呢
  • 做网站怎么找优质客户广告营销案例100例
  • 深圳建设集团大厦网站排名优化专业定制
  • 网站建设工作半年通报做搜索引擎推广多少钱
  • 简单介绍网站建设的一般流程企业网站推广的方法有哪些
  • 高端h5网站百度手机助手app
  • 做网站要下载的软件成都网多多
  • 做货代哪个网站上好找客户学历提升
  • 网站开发论文开题报告百度竞价开户渠道
  • 购物网站建设论文答辩每日新闻
  • 网站域名空间怎么提交郑州网络营销顾问
  • 2016网站设计风格推广之家app
  • 网页界面设计使用的单位主要是搜索引擎优化实训报告
  • wordpress显示浏览量江苏seo和网络推广
  • 网站地图制作怎么做小红书关键词搜索量查询
  • 易语言 wordpressseo优化培训公司
  • mac无法修改wordpress做百度seo
  • 公司备案证查询网站爱网站关键词挖掘工具
  • 如何做外贸网站2022百度收录越来越难了
  • 兰州营销型网站建设代运营网店公司
  • 西安网站建站品牌建站模板网站
  • 大型网站开发方案福州网络推广运营
  • 中企动力大连公司咋样郑州seo优化服务
  • 济南网站建设招聘上海有名网站建站开发公司
  • 济南网站制作经验晋城网站seo
  • 阿里巴巴旗下跨境电商平台有哪些seo的优化方案
  • web网站开发用到什么工具优化人员是什么意思
  • 有哪些网站是用vue做的全能搜
  • 政府网站开发多钱百度站长工具官网