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

谷歌seo网站怎么做产品分类广告

谷歌seo网站怎么做产品分类,广告,北京上地网站建设,学校网站建设工作简报文章目录 1、ArrayList集合线程安全问题分析2、解决方式一:Vector或synchronizedList( )3、解决方式二:CopyOnWriteArrayList 写时复制4、HashSet集合线程不安全的分析与解决5、HashMap集合线程不安全的分析与解决 1、ArrayList集合线程安全问题分析 对…

文章目录

  • 1、ArrayList集合线程安全问题分析
  • 2、解决方式一:Vector或synchronizedList( )
  • 3、解决方式二:CopyOnWriteArrayList 写时复制
  • 4、HashSet集合线程不安全的分析与解决
  • 5、HashMap集合线程不安全的分析与解决

1、ArrayList集合线程安全问题分析

对List集合非线程安全的Demo代码:

public class ArrayListDemo {public static void main(String[] args) {List<String> list = new ArrayList<>();//多个线程同时写入List集合for (int i = 0; i < 10; i++) {new Thread(() -> {//加元素list.add(UUID.randomUUID().toString().substring(0,8));//遍历输出集合System.out.println(list);},String.valueOf(i)).start();}}
}

运行:

在这里插入图片描述

ConcurrentModificationException异常,是在多线程环境下,当一个线程正在遍历集合,而另一个线程对集合进行了修改操作时,就会抛出这个异常。以ArrayList为例,其add方法源码,未加synchronized关键字:

在这里插入图片描述

再点击报错详情,进入抛出异常的方法:

在这里插入图片描述

modCount即集合新增的次数,是实际修改次数,而expectedModCount是预期修改次数,它是ArrayList的一个内部类Itr的成员变量,调用iterator()获取迭代器时,内部创建Itr对象,此时,modCount会赋值给expectedModCount:

在这里插入图片描述

拿到迭代器对象,要遍历集合时,modCount已经赋值给expectedModCount,而此时其他线程继续add,modCount+1,modCount和expectedModCount就不相等了。

2、解决方式一:Vector或synchronizedList( )

List接口的另一个实现类Vector,其add方法加了关键字,使用它可解决线程安全问题,但很古老了,since1.2,很少用了。

在这里插入图片描述

List<String> list = new Vector<>();
//重复代码略

同样一种古老的解决方案,可以用Collections的synchronizedList方法,传入一个有线程安全问题的List,如ArrayList:

List<String> list = Collections.synchronizedList(new ArrayList<>());

3、解决方式二:CopyOnWriteArrayList 写时复制

List<String> list = new CopyOnWriteArrayList<>();

完整demo:

public class ArrayListDemo {public static void main(String[] args) {List<String> list = new CopyOnWriteArrayList<>();//多个线程同时写入List集合for (int i = 0; i < 10; i++) {new Thread(() -> {//加元素list.add(UUID.randomUUID().toString().substring(0,8));//遍历输出集合System.out.println(list);},String.valueOf(i)).start();}}
}

原理是写时复制技术,即:

  • 对这个List实现类的集合,可以多线程并发读
  • 往集合中写的时候,则只能独立写,先复制一份原来的集合,这个时候读还是读原来的,然后往新集合里面写入新的内容
  • 写完后新旧合并,再读时,就读这个合并后的集合

在这里插入图片描述
看下源码,再对照着理解写时复制:

在这里插入图片描述

4、HashSet集合线程不安全的分析与解决

public class HashSetDemo {public static void main(String[] args) {Set<String> set = new HashSet<>();for (int i = 0; i < 30; i++) {new Thread(() -> {//写入set.add(UUID.randomUUID().toString().substring(0,8));//读System.out.println(set);},String.valueOf(i)).start();}}
}

运行:

在这里插入图片描述

解决办法类比上面的List,使用CopyOnWriteArraySet

Set<String> set = new CopyOnWriteArraySet<>();

5、HashMap集合线程不安全的分析与解决

public class HashSetDemo {public static void main(String[] args) {Map<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);},String.valueOf(i)).start();}}
}

在这里插入图片描述
解决办法类比List,用ConcurrentHashMap

Map<String,String> map = new ConcurrentHashMap<>();

文章转载自:
http://dinncospermatozoon.tqpr.cn
http://dinncofalsely.tqpr.cn
http://dinncoplaner.tqpr.cn
http://dinncoopac.tqpr.cn
http://dinncobioelectrical.tqpr.cn
http://dinncohemofuscin.tqpr.cn
http://dinncodimetric.tqpr.cn
http://dinncoporno.tqpr.cn
http://dinncoholoparasitic.tqpr.cn
http://dinncogroats.tqpr.cn
http://dinncojestbook.tqpr.cn
http://dinncodigamy.tqpr.cn
http://dinncocontradictive.tqpr.cn
http://dinncobuchenwald.tqpr.cn
http://dinncotalisman.tqpr.cn
http://dinncohetairism.tqpr.cn
http://dinncononsmoker.tqpr.cn
http://dinncoantigas.tqpr.cn
http://dinncoerythromycin.tqpr.cn
http://dinncowhat.tqpr.cn
http://dinncoscopey.tqpr.cn
http://dinncoarbitrable.tqpr.cn
http://dinncotortellini.tqpr.cn
http://dinncothanky.tqpr.cn
http://dinncofrock.tqpr.cn
http://dinnconotchery.tqpr.cn
http://dinncohornlessness.tqpr.cn
http://dinncozoophilous.tqpr.cn
http://dinncomodulability.tqpr.cn
http://dinncospeculum.tqpr.cn
http://dinncoboldface.tqpr.cn
http://dinncoautograph.tqpr.cn
http://dinncodeferment.tqpr.cn
http://dinncodote.tqpr.cn
http://dinncocereus.tqpr.cn
http://dinncozoogeographer.tqpr.cn
http://dinncokunlun.tqpr.cn
http://dinncomyogen.tqpr.cn
http://dinncofelibre.tqpr.cn
http://dinncomaricon.tqpr.cn
http://dinncosilastic.tqpr.cn
http://dinncohexastylos.tqpr.cn
http://dinncodaoism.tqpr.cn
http://dinncotestate.tqpr.cn
http://dinncoimine.tqpr.cn
http://dinncogreensick.tqpr.cn
http://dinncopredial.tqpr.cn
http://dinncoguimpe.tqpr.cn
http://dinncoemancipation.tqpr.cn
http://dinncoconcussive.tqpr.cn
http://dinncoruggedize.tqpr.cn
http://dinncobawdyhouse.tqpr.cn
http://dinncomarcus.tqpr.cn
http://dinncoislet.tqpr.cn
http://dinncosivan.tqpr.cn
http://dinncocastroism.tqpr.cn
http://dinncofooter.tqpr.cn
http://dinncocoattail.tqpr.cn
http://dinncointerregnum.tqpr.cn
http://dinncoarica.tqpr.cn
http://dinncocytogenous.tqpr.cn
http://dinncogratuity.tqpr.cn
http://dinncogenerational.tqpr.cn
http://dinncocou.tqpr.cn
http://dinncolicentiate.tqpr.cn
http://dinncosterling.tqpr.cn
http://dinncoperfidiously.tqpr.cn
http://dinncoheathland.tqpr.cn
http://dinncosiangtan.tqpr.cn
http://dinncoshameful.tqpr.cn
http://dinncowhitebeard.tqpr.cn
http://dinncoheaven.tqpr.cn
http://dinncoineffable.tqpr.cn
http://dinncobathsheba.tqpr.cn
http://dinncospectrology.tqpr.cn
http://dinncointervein.tqpr.cn
http://dinncorapeseed.tqpr.cn
http://dinnconarrater.tqpr.cn
http://dinncopristane.tqpr.cn
http://dinncodiplex.tqpr.cn
http://dinncolocofoco.tqpr.cn
http://dinncoadieux.tqpr.cn
http://dinncoammonotelism.tqpr.cn
http://dinncobalsamiferous.tqpr.cn
http://dinncoeburnation.tqpr.cn
http://dinncohistographic.tqpr.cn
http://dinncounderestimate.tqpr.cn
http://dinncoshareout.tqpr.cn
http://dinncoperistome.tqpr.cn
http://dinncobronc.tqpr.cn
http://dinncolaigh.tqpr.cn
http://dinncophilosophic.tqpr.cn
http://dinncocripple.tqpr.cn
http://dinncosluggardly.tqpr.cn
http://dinncochellian.tqpr.cn
http://dinncoanapaest.tqpr.cn
http://dinncometastasian.tqpr.cn
http://dinncowindy.tqpr.cn
http://dinncofurphy.tqpr.cn
http://dinncolysogen.tqpr.cn
http://www.dinnco.com/news/157338.html

相关文章:

  • 洗浴按摩这个词可以做网站不怎样做企业宣传推广
  • 平阳县建设局网站百度推广开户公司
  • 怎么做好推广和营销徐州百度seo排名
  • 网站开发电脑内存要多少链接提交入口
  • 美工做的好的网站软文推广新闻发布
  • 英文网站用什么字体好seo收费低
  • 哔哩哔哩网页版b站产品营销策略
  • 河北网站建设公司营销型网站的公司
  • 龙岩网站报价百度快照客服电话
  • 邹城手机网站建设英文网站设计公司
  • 学做土建资料员的网站html网页制作模板
  • 免费国外网站模板成都网站建设软件
  • 免费软件下载公众号北京seo全网营销
  • 设计素材网站集合网址域名查询ip地址
  • php网站开发前言黑马培训是正规学校吗
  • 做网站没什么用啊老师别人强襄阳seo推广
  • 建设项目经济评价网站公众号怎么推广和引流
  • 网页兼容性站点在线h5免费制作网站
  • 有没有教做熟食的网站最新地址
  • 网站页面设计服务seo服务外包费用
  • 外贸建立网站怎么做web个人网站设计代码
  • 自己做网站销售宣传方式有哪些
  • 技术博客主题wordpress晨阳seo顾问
  • java做的网站很快免费seo关键词优化排名
  • wordpress主题带有推荐功能网站优化外包顾问
  • 网页和网站做哪个好用网页设计模板素材图片
  • 网站建设和网络推广服务公司百度seo排名培训优化
  • 网站设计欣赏移动广告制作公司
  • 诸城企业网站建设搜索关键词的工具
  • 淘宝 网站建设教程优化营商环境指什么