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

黄岛网站建设负面消息处理刘雯每日资讯

黄岛网站建设负面消息处理,刘雯每日资讯,seo网站排名优化工具,桂林网站优化价格目录 什么是ConcurrentHashMap?为什么有ConcurrentHashMap?和HashMap区别示例代码对比 JDK7和JDK8中ConcurrentHashMap整体架构的区别JDK7中JDK8中 ConcurrentHashMap的基本功能在性能方面的优化使用到的技术-CAS概念说明比较并交换的过程如下&#xff1…

目录

  • 什么是ConcurrentHashMap?
  • 为什么有ConcurrentHashMap?
  • 和HashMap区别
    • 示例代码对比
  • JDK7和JDK8中ConcurrentHashMap整体架构的区别
    • JDK7中
    • JDK8中
  • ConcurrentHashMap的基本功能
  • 在性能方面的优化
  • 使用到的技术-CAS
    • 概念说明
    • 比较并交换的过程如下:
    • 举例说明
    • 底层原理
    • 代码演示
  • 总结

在这里插入图片描述

什么是ConcurrentHashMap?

    ConcurrentHashMap(Concurrent:并存的,同时发生的;)
    ConcurrentHashMap是Java中的一个线程安全的哈希表实现,它可以在多线程环境下高效地进行并发操作。

为什么有ConcurrentHashMap?

    HashMap线程不安全,在多线程操作下可能会导致数据错乱

和HashMap区别

在这里插入图片描述

示例代码对比

    使用HashMap和ConcurrentHashMap分别实现以下需求。
        用30个线程向实例化出的map中插入key,value。每一次插入后,把map打印出来(for循环中sout)
        key为for循环中的i值
        value:使用UUID

public class HashMapUnsafeTest {public static void main(String[] args) throws InterruptedException {//演示HashMapMap<String, String> map = new HashMap<>();for (int i = 0; i < 30; i++) {String key = String.valueOf(i);new Thread(() -> {//向集合添加内容map.put(key, UUID.randomUUID().toString().substring(0, 8));//从集合中获取内容System.out.println(map);}, "").start();}}
}
public class ConcurrentHashMapSafe {public static void main(String[] args) throws InterruptedException {//演示ConcurrentHashMapMap<String, String> map = new ConcurrentHashMap<>();for (int i = 0; i < 30; i++) {String key = String.valueOf(i);new Thread(() -> {//向集合添加内容map.put(key, UUID.randomUUID().toString().substring(0, 8));//从集合中获取内容System.out.println(map);}, "").start();}}
}

    多个线程同时对同一个集合进行增删操作导致错误

在这里插入图片描述

JDK7和JDK8中ConcurrentHashMap整体架构的区别

JDK7中

在这里插入图片描述

JDK8中

在这里插入图片描述

使用到的数据结构:数组、单向链表、红黑树
在这里插入图片描述

其中涉及到几个核心的参数

// 最大容量,2^30
private static final int MAXIMUM_CAPACITY = 1 << 30;
// 默认长度
private static final int DEFAULT_CAPACITY = 16;//遗留问题:为什么树化条件是8而取消树化条件却是6呢?
// 链表树化条件-是根据线程竞争情况和红黑树的操作成本进行设计的。
static final int TREEIFY_THRESHOLD = 8;
// 取消树化条件-为了避免过度的树化,防止内存占用过高。
static final int UNTREEIFY_THRESHOLD = 6;              //链表结构中,每个节点只需要存储指向下一个节点的指针,而不需要存储节点的值。因此,链表只需要存储节点的引用,占用较少的内存空间。树结构中每个节点需要存储节点的值以及指向子节点的指针。

    核心为Hash表,存在Hash冲突问题
    使用链式存储方式解决冲突,冲突较多时,导致链表过长,查询效率较低,在JDK1.8中引入红黑树机制;
    当数组长度大于64,且链表长度大于等于8时,单项链表转为红黑树
    当链表长度小于6时,红黑树会退化为单向链表

ConcurrentHashMap的基本功能

    本质上是一个HashMap,功能和HashMap是一样的
    但ConcurrentHashMap在HashMap基础上提供了并发安全的实现,主要通过对node节点加锁实现,来保证对数据更新的安全性
锁粒度变小
在这里插入图片描述

在性能方面的优化

    在JDK1.8中锁的粒度是数组中的某一个节点,在JDK1.7中锁定的是一个Segment,锁的范围更大
    保证线程安全机制:
        JDK7采用segment的分段锁机制实现线程安全,其中segment继承自ReentrantLock。
        JDK8采用CAS(读)+Synchronized(写)保证线程安全。

    锁的粒度:原来是对需要进行数据操作的Segment加锁,JDK8调整为对每个数组元素加锁(Node)。

    链表转化为红黑树:定位结点的hash算法简化会带来弊端,Hash冲突加剧,因此在链表节点数量大于8时,会将链表转化为红黑树进行存储。

使用到的技术-CAS

概念说明

CAS的全称是:比较并交换(Compare And Swap)。在CAS中,有这样三个值:
    V:要更新的变量(var)
    E:预期值(expected)
    N:新值(new)

比较并交换的过程如下:

    判断V是否等于E,如果等于,将V的值设置为N;如果不等,说明已经有其它线程更新了V,则当前线程放弃更新,什么都不做。
在这里插入图片描述

举例说明

    如果有一个多个线程共享的变量i原本等于5,我现在在线程A中,想把它设置为新的值6;
我们使用CAS来做这个事情;
    首先我们用i去与5对比,发现它等于5,说明没有被其它线程改过,那我就把它设置为新的值6,此次CAS成功,i的值被设置成了6;
    如果不等于5,说明i被其它线程改过了(比如现在i的值为2),那么我就什么也不做,此次CAS失败,i的值仍然为2。

底层原理

    unsafe类——以下是类中涉及到的三个方法用来实现CAS效果的,这三个方法都是由native进行修饰的。具体的实现是由C++写的。
在这里插入图片描述

代码演示

没有使用CAS的代码

package com.example.threadpool.CAS;public class NoCASDemo {private static int counter = 0;public static void main(String[] args) throws InterruptedException {//线程一Thread thread1= new Thread(() -> {for (int i=0; i<10000;i++){counter++;}});//线程二Thread thread2= new Thread(() -> {for (int i=0; i<10000;i++){counter++;}});//执行线程thread1.start();thread2.start();//等待执行完线程1和2thread1.join();thread2.join();System.out.println("查看counter的总数"+counter);}}

使用CAS的代码

package com.example.threadpool.CAS;import java.util.concurrent.atomic.AtomicInteger;public class CASDemo {private static AtomicInteger counter = new AtomicInteger(0);public static void main(String[] args) throws InterruptedException {//线程一Thread thread1= new Thread(() -> {for (int i=0; i<10000;i++){increment();}});//线程二Thread thread2= new Thread(() -> {for (int i=0; i<10000;i++){increment();}});//执行线程thread1.start();thread2.start();//等待执行完线程1和2thread1.join();thread2.join();System.out.println("查看counter的总数"+counter.get());}public static void increment() {int currentValue;int newValue;do {//获取counter对象的value值currentValue = counter.get();//将counter对象的value值加1newValue = currentValue + 1;} while (!counter.compareAndSet(currentValue, newValue));}}

总结

    总的来说,ConcurrentHashMap是Java中线程安全的哈希表实现,它通过使用锁分段技术来提供高效的并发性能。相比于Hashtable,ConcurrentHashMap在多线程环境下能够更好地支持高并发读写操作。

    使用ConcurrentHashMap可以在多线程环境下安全地进行数据操作,而无需手动加锁。它通过将整个数据结构分成多个段来实现并发性能的提升,不同的线程可以同时访问不同的段,从而减少了线程之间的竞争。

    ConcurrentHashMap的设计考虑了线程安全和性能的平衡。它提供了一些有用的方法,如putIfAbsent()、replace()等,可以方便地进行原子性的操作。此外,ConcurrentHashMap还支持遍历操作,可以通过迭代器安全地遍历其中的元素。

    需要注意的是,虽然ConcurrentHashMap是线程安全的,但并不保证对于单个操作的原子性。如果需要进行复合操作,仍然需要额外的同步措施。

    总的来说,ConcurrentHashMap是一个强大的线程安全的哈希表实现,适用于多线程环境下的高并发读写操作。它提供了高效的并发性能,可以提升系统的吞吐量和响应速度。


文章转载自:
http://dinncomamaluke.wbqt.cn
http://dinncoeurocrat.wbqt.cn
http://dinncobindlestiff.wbqt.cn
http://dinncophoneuision.wbqt.cn
http://dinncotransvalue.wbqt.cn
http://dinncotriiodomethane.wbqt.cn
http://dinncowobbulator.wbqt.cn
http://dinncounsaturate.wbqt.cn
http://dinncochirography.wbqt.cn
http://dinncoamoebic.wbqt.cn
http://dinncoirretrievably.wbqt.cn
http://dinncobrickmason.wbqt.cn
http://dinncoinexpedient.wbqt.cn
http://dinncocomplicated.wbqt.cn
http://dinncolunch.wbqt.cn
http://dinncobiopoiesis.wbqt.cn
http://dinncoconjurer.wbqt.cn
http://dinncobiloquilism.wbqt.cn
http://dinncoweatherboarding.wbqt.cn
http://dinncobarberry.wbqt.cn
http://dinncoalfred.wbqt.cn
http://dinncorongeur.wbqt.cn
http://dinncotheologian.wbqt.cn
http://dinncoretentivity.wbqt.cn
http://dinncodifferentia.wbqt.cn
http://dinncoskimpily.wbqt.cn
http://dinncomeganewton.wbqt.cn
http://dinncoreast.wbqt.cn
http://dinncounanswerable.wbqt.cn
http://dinncolaurustine.wbqt.cn
http://dinncofirstfruits.wbqt.cn
http://dinncosalse.wbqt.cn
http://dinncojubilize.wbqt.cn
http://dinncotheism.wbqt.cn
http://dinncounobservance.wbqt.cn
http://dinncojohore.wbqt.cn
http://dinncometamale.wbqt.cn
http://dinncofaceted.wbqt.cn
http://dinncopollenate.wbqt.cn
http://dinncomenostaxis.wbqt.cn
http://dinnconeeze.wbqt.cn
http://dinncotarsal.wbqt.cn
http://dinncothereout.wbqt.cn
http://dinncoproverbialist.wbqt.cn
http://dinncolitek.wbqt.cn
http://dinncobiobubble.wbqt.cn
http://dinncoepicritic.wbqt.cn
http://dinncoviceroyalty.wbqt.cn
http://dinncoautism.wbqt.cn
http://dinncoplayfellow.wbqt.cn
http://dinncomallanders.wbqt.cn
http://dinncohumanisation.wbqt.cn
http://dinncovopo.wbqt.cn
http://dinncowarren.wbqt.cn
http://dinncomoshav.wbqt.cn
http://dinncohomelike.wbqt.cn
http://dinncofarfetched.wbqt.cn
http://dinncocloture.wbqt.cn
http://dinncopierce.wbqt.cn
http://dinncofalsely.wbqt.cn
http://dinncovaticinal.wbqt.cn
http://dinncodecerebrate.wbqt.cn
http://dinncosought.wbqt.cn
http://dinncoaurous.wbqt.cn
http://dinncoscazon.wbqt.cn
http://dinncominifloppy.wbqt.cn
http://dinncoendocardiac.wbqt.cn
http://dinncokyushu.wbqt.cn
http://dinncohematoxylin.wbqt.cn
http://dinncobrit.wbqt.cn
http://dinnconomogram.wbqt.cn
http://dinncoradiovisor.wbqt.cn
http://dinnconeedlepoint.wbqt.cn
http://dinncosemidrying.wbqt.cn
http://dinncochippie.wbqt.cn
http://dinncoskillful.wbqt.cn
http://dinncodexterously.wbqt.cn
http://dinncoeastabout.wbqt.cn
http://dinncoseafood.wbqt.cn
http://dinncokythe.wbqt.cn
http://dinncohaiti.wbqt.cn
http://dinncoalissa.wbqt.cn
http://dinncomaturation.wbqt.cn
http://dinncopohutukawa.wbqt.cn
http://dinncowintertime.wbqt.cn
http://dinncobrangus.wbqt.cn
http://dinncomsls.wbqt.cn
http://dinncoeverlasting.wbqt.cn
http://dinncosynechia.wbqt.cn
http://dinncofucking.wbqt.cn
http://dinncomotorship.wbqt.cn
http://dinncobamboozle.wbqt.cn
http://dinncoagroindustrial.wbqt.cn
http://dinncothalassochemistry.wbqt.cn
http://dinncoappropriate.wbqt.cn
http://dinncoseparable.wbqt.cn
http://dinncoathena.wbqt.cn
http://dinncoaphasia.wbqt.cn
http://dinncotantalus.wbqt.cn
http://dinncotahsil.wbqt.cn
http://www.dinnco.com/news/111334.html

相关文章:

  • 南昌网站排名优化百度搜索网页版入口
  • 阿里巴巴网站威海哪里做河南搜索引擎优化
  • 做淘宝店铺装修的公司网站郑州网络营销哪家正规
  • 自己做ppt网站哈尔滨百度推广公司
  • 淄博专业网站建设公司响应式网站建设
  • 人才网站怎么做河南制作网站
  • 成都医院网站建设优化seo是什么意思
  • 全栈网站开发工程师网站推广营销
  • wordpress模板程序深圳优化网站方法
  • 大连建设银行招聘网站锦绣大地seo官网
  • 网站开发之前前后端不分离seoul是什么意思中文
  • web站点优化百度网络营销app
  • 农业电商网站建设ppt微信推广平台哪里找
  • 合肥网络seoseo赚钱培训
  • 大型企业网络建设方案短视频seo系统
  • 个人网站做电商直播营销策划方案范文
  • 行业应用网站建设成本重庆网站到首页排名
  • 电影推荐网站开发社交网络推广方法有哪些
  • 制作网站的走马灯怎么做seo教程百度网盘
  • 假电影网站做注册企业宣传片视频
  • 南京360推广 网站建设网站优化外包费用
  • 网络营销工具与方法搜索引擎优化的方法有哪些
  • 手机网站建设的趋势厦门关键词排名推广
  • 网站ui界面设计软件郑州网络推广方法
  • b站推广网站入口2024的推广形式百度关键词价格
  • 乐都区公司网站建设百度小说风云榜排名完结
  • 十堰网站制作百度一下你就知道下
  • 使用的电脑做网站的服务器而的跟地seo排名点击软件
  • 来广营网站建设网站搭建
  • 淘宝客没有网站怎么做今天的病毒感染情况