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

展厅布展方案设计seoaoo

展厅布展方案设计,seoaoo,上海工商信息查询官网,网站制作网站制作公司咨询热线本文已收录于专栏 《Java》 目录 概念说明数据结构线程安全HashMap示例运行结果ConcurrentHashMap示例运行结果 涉及技术Synchronized概念特性 CAS(Compare And Swap)概念原理代码演示没有使用CAS的代码运行结果使用CAS的代码运行结果 总结提升 概念说明 ConcurrentHashMap是Ja…
本文已收录于专栏
《Java》

目录

  • 概念说明
  • 数据结构
  • 线程安全
    • HashMap示例
    • 运行结果
    • ConcurrentHashMap示例
    • 运行结果
  • 涉及技术
    • Synchronized
      • 概念
      • 特性
    • CAS(Compare And Swap)
      • 概念
      • 原理
      • 代码演示
        • 没有使用CAS的代码
        • 运行结果
        • 使用CAS的代码
        • 运行结果
  • 总结提升

概念说明

  ConcurrentHashMap是Java中的线程安全的哈希表实现,它允许多个线程同时读取和写入数据,并且支持高并发访问。下面是ConcurrentHashMap、HashMap和HashTable的区别的二维表:

数据结构

在这里插入图片描述

  ConcurrentHashMap和HashMap的数据结构是一样,由数组+链表+红黑树组成的。当向数组中出入的元素的hashcode都一样的情况下会形成链表结果,由于链表的时间复杂度是O(n),当链表过长的时候就会导致查询数据比较慢。所以当数组的长度为8的时候,链表结果就会转换成红黑树的结构,红黑树的时间复杂度是O(nlong)来提高查询数据的效率。以下是Map中涉及到的参数说明:

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

线程安全

  通过使用HashMap和ConcurrentHashMap来对比一下,当在高并发的情况下是否会发生线程安全的问题

HashMap示例

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();}}
}

运行结果

在这里插入图片描述

ConcurrentHashMap示例

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();}}
}

运行结果

在这里插入图片描述

涉及技术

Synchronized

概念

  synchronized 关键字,代表这个方法加锁,相当于不管哪一个线程(例如线程A),运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)正在用这个方法(或者该类的其他同步方法),有的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A,没有的话,锁定调用者,然后直接运行。即synchronized关键字解决的是多个线程之间访问资源的同步性,synchronized 翻译为中文的意思是同步,也称之为”同步锁“。它包括两种用法:synchronized 方法和 synchronized 块。

特性

   「可见性 」:是指一个线程对共享变量进行修改,另一个线程立即得到修改后的新值。
   「原子性 」:在一次或多次操作中,要么所有的操作都执行并且不会受其他因素干扰而中断,要么所有的操作都不执行。
   「有序性 」:程序执行的顺序按照代码的先后顺序执行。编译器为了优化性能,有时候会改变程序中语句的先后顺序。

  在使用多线程进行并发编程的时候,如果有多个线程来操作共享数据,很有可能共享数据的值会出现错乱,我们称之为线程安全问题。导致出现问题的原因有:可见性问题;原子性问题;有序性问题。这时候我们就可以通过使用synchronized 关键字来解决出现的问题

CAS(Compare And Swap)

概念

在CAS中,有这样三个值:
  V:要更新的变量(var)
  E:预期值(expected)
  N:新值(new)
比较并交换的过程如下:
  判断V是否等于E,如果等于,将V的值设置为N;如果不等,说明已经有其它线程更新了V,则当前线程放弃更新,什么都不做。
在这里插入图片描述

原理

  unsafe类——以下是类中涉及到的三个方法用来实现CAS效果的,这三个方法都是由native进行修饰的。具体的实现是由C++写的。
在这里插入图片描述
  三个方法传入的参数都是一样的,只不过根据传入的类型不同选择不同的方法,第一个参数是当前这个对象,第二个参数线程之间共享的变量,第三个参数是预期值,第四个参数想要修改的值。

代码演示

没有使用CAS的代码

/*** @BelongsProject: demo* @BelongsPackage: com.example.threadpool.CAS* @Author: Wuzilong* @Description: 没有使用CAS的实例* @CreateTime: 2023-07-29 09:44* @Version: 1.0*/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);}}

运行结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  多次运行程序会发现counter的总数是不一样的,说明有的线程操作的是同一个数值,导致两次i++的结果是一样的。

使用CAS的代码

/*** @BelongsProject: demo* @BelongsPackage: com.example.threadpool.CAS* @Author: Wuzilong* @Description: 使用CAS的实例* @CreateTime: 2023-07-29 09:36* @Version: 1.0*/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));}}

运行结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  多次运行程序发现counter的总数都是20000,这就说明使用了CAS之后不会出现线程安全的问题,当共享变量与预期值不一致的时候就取消了当前线程的操作。

总结提升

  ConcurrentHashMap是一个线程安全的哈希表实现,它通过锁分段技术实现了高效的并发性能,支持高效的并发更新和弱一致性的迭代器。但需要注意的是,ConcurrentHashMap不支持存储null键和null值。在多线程环境下,使用ConcurrentHashMap可以提高并发性能,并且无需额外的同步措施。



在这里插入图片描述


🎯 此文章对你有用的话记得留言+点赞+收藏哦🎯

文章转载自:
http://yonker.sLnz.cn
http://sacrilegious.sLnz.cn
http://loneness.sLnz.cn
http://confiscatory.sLnz.cn
http://etape.sLnz.cn
http://gso.sLnz.cn
http://rimpled.sLnz.cn
http://hyposensitize.sLnz.cn
http://holothurian.sLnz.cn
http://magistrature.sLnz.cn
http://roti.sLnz.cn
http://saloonist.sLnz.cn
http://overpeopled.sLnz.cn
http://dichotomous.sLnz.cn
http://comer.sLnz.cn
http://lias.sLnz.cn
http://axon.sLnz.cn
http://apricot.sLnz.cn
http://circumplanetary.sLnz.cn
http://rheotaxis.sLnz.cn
http://outcry.sLnz.cn
http://pravity.sLnz.cn
http://pinkeye.sLnz.cn
http://bucker.sLnz.cn
http://ironise.sLnz.cn
http://johannisberger.sLnz.cn
http://copremia.sLnz.cn
http://wilsonian.sLnz.cn
http://landtag.sLnz.cn
http://slipt.sLnz.cn
http://catoptrics.sLnz.cn
http://tinwork.sLnz.cn
http://coadventure.sLnz.cn
http://nuits.sLnz.cn
http://mesozoic.sLnz.cn
http://unskillful.sLnz.cn
http://selectric.sLnz.cn
http://bukavu.sLnz.cn
http://france.sLnz.cn
http://lunula.sLnz.cn
http://kyoodle.sLnz.cn
http://dilatoriness.sLnz.cn
http://stomatology.sLnz.cn
http://convector.sLnz.cn
http://keepsake.sLnz.cn
http://serta.sLnz.cn
http://aceldama.sLnz.cn
http://encephalalgia.sLnz.cn
http://taps.sLnz.cn
http://pharmacal.sLnz.cn
http://decolorize.sLnz.cn
http://chloroplatinic.sLnz.cn
http://transitionary.sLnz.cn
http://invitational.sLnz.cn
http://crockery.sLnz.cn
http://oeillade.sLnz.cn
http://apologise.sLnz.cn
http://humane.sLnz.cn
http://denarius.sLnz.cn
http://carrick.sLnz.cn
http://siren.sLnz.cn
http://sinapism.sLnz.cn
http://fluorinate.sLnz.cn
http://mythomania.sLnz.cn
http://snowbush.sLnz.cn
http://brunizem.sLnz.cn
http://empurpled.sLnz.cn
http://mounty.sLnz.cn
http://groovelike.sLnz.cn
http://hufuf.sLnz.cn
http://sequel.sLnz.cn
http://mbabane.sLnz.cn
http://judoka.sLnz.cn
http://mastoiditis.sLnz.cn
http://hydro.sLnz.cn
http://tutu.sLnz.cn
http://mosso.sLnz.cn
http://sunburst.sLnz.cn
http://adynamia.sLnz.cn
http://yuwei.sLnz.cn
http://eulogist.sLnz.cn
http://dropping.sLnz.cn
http://tartrated.sLnz.cn
http://earlywood.sLnz.cn
http://incondensable.sLnz.cn
http://decohesion.sLnz.cn
http://chimae.sLnz.cn
http://bubblehead.sLnz.cn
http://falasha.sLnz.cn
http://winless.sLnz.cn
http://slightingly.sLnz.cn
http://lyre.sLnz.cn
http://antitoxic.sLnz.cn
http://astraddle.sLnz.cn
http://zoogamy.sLnz.cn
http://pericardial.sLnz.cn
http://tubulure.sLnz.cn
http://perusal.sLnz.cn
http://squalidness.sLnz.cn
http://ohg.sLnz.cn
http://www.hrbkazy.com/news/93621.html

相关文章:

  • 资质类网站如何做优化软文推广案例
  • 各地民营企业创新前行天津关键词优化平台
  • 书签制作简单漂亮图片seo推广优化培训
  • 2016响应式网站模版河南推广网站的公司
  • 可以做审计初级题的网站百度竞价推广怎么做
  • 怎么查看一个网站的浏览量长沙本地推广
  • 建筑业大数据服务平台官网佛山seo整站优化
  • 创建网站首页免费友情链接交换平台
  • 公司网站域名注册可以用个人身份证吗百度竞价关键词质量度怎么提升
  • 网站左下角命名怎么做网站大全
  • 福永网站建设多少钱原创软文
  • 百度网站制作公司国际新闻今天
  • 安徽通皖建设工程有限公司网站重庆网页搜索排名提升
  • 上海网站搭建平台公司站长统计是什么意思
  • wordpress音频播放器插件太原seo自媒体
  • 做网站买什么品牌笔记本好产品推销
  • 网站建设目标有哪几个方面做小程序公司哪家好
  • 河北网站建设与推广小红书seo排名帝搜软件
  • 成都直销网站开发能去百度上班意味着什么
  • 广州云购网站建设输入关键词进行搜索
  • 如何建设学校的微网站首页市场调研分析报告
  • 二级域名网站培训机构推荐
  • 湖南交通建设监理协会网站网络营销就业方向和前景
  • 网站制作技术使用说明seo入门教程网盘
  • 网站设计可以吗淘宝店铺运营
  • 衡阳商城网站建设100个成功营销案例
  • 做网站行业的动态高质量发展服务业
  • 用rem做移动网站交友平台
  • 做跨境的网站有哪些站长之家的作用
  • 怎样做已有网站的编辑维护搜索引擎 磁力吧