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

莆田自助建站软件百度搜索榜

莆田自助建站软件,百度搜索榜,做钓鱼网站查处,迅雷资源做下载网站文章目录 1、邮戳锁2、锁饥饿问题的解决思路3、邮戳锁的特点4、代码演示:邮戳锁的传统读写用法5、代码演示:邮戳锁之乐观读6、邮戳锁的缺点7、终章回顾 前面提到了从无锁 ⇒ 独占锁 ⇒ 读写锁,但读写锁存在写锁饥饿的情况。 📕【读…

文章目录

  • 1、邮戳锁
  • 2、锁饥饿问题的解决思路
  • 3、邮戳锁的特点
  • 4、代码演示:邮戳锁的传统读写用法
  • 5、代码演示:邮戳锁之乐观读
  • 6、邮戳锁的缺点
  • 7、终章回顾

前面提到了从无锁 ⇒ 独占锁 ⇒ 读写锁,但读写锁存在写锁饥饿的情况。

  • 📕【读写锁的演化与锁降级】

本篇邮戳锁(也称版本锁、票据锁)即是对读写锁的再一次演化。

1、邮戳锁

StampedLock是JDK1.8中新增的一个读写锁,也是对JDK1.5中的读写锁ReentrantReadWriteLock的优化

stamp,戳记,long类型,代表了锁的状态,当stamp返回0时,表示线程获取锁失败,且当释放锁或者转换锁时,都要传入最初获取的stamp值。

2、锁饥饿问题的解决思路

idea1:使用公平锁可一定程度上缓解锁饥饿问题,但这样是以牺牲系统吞吐量为代价的

new ReentrantReadWriteLock(true);

idea2:Java8StampedLock类的乐观读锁

读写锁的实现类ReentrantReadWriteLock是读写互斥,写写互斥,但读读共享,因此性能比synchronized等独占锁好很多。而其读写互斥的特点,在读线程多,写线程少时,会导致写锁饥饿,基于此,邮戳锁提供乐观锁。

获取乐观锁后,其他线程尝试获取写锁时不再会被阻塞,同时,乐观读后也要对结果进行校验。很乐观,认为我读的时候,不会有人改,如此,相比ReentrantReadWriteLock读写锁,性能又上了一个台阶。

对短的只读代码段,使用乐观模式通常可以减少争用并提高吞吐量 (强调短的读,是因为读的时间短了,中间被写线程改数据的概率就低,更容易乐观成功)

3、邮戳锁的特点

  • 所有获取锁的方法,都返回一个邮戳Stamp,Stamp为零表示锁获取失败,其余都表示成功
  • 所有释放锁的方法,都需要一个邮戳Stamp,这个Stamp必须是和成功获取锁时得到的stamp一致
  • StampedLock是不可重入的,因此,如果一个线程已经持有了写锁,又再去获取写锁的话就会造成死锁

StampedLock有三种访问模式:

  • Reading(读模式悲观):功能和ReentrantReadWriteLock的读锁类似
  • Writing(写模式):功能和ReentrantRerdWriteLock的写锁类似
  • Optimistic reading (乐观读模式):无锁机制,类似于数据库中的乐观锁,支持读写并发,很乐观的认为读取时没人修改,假如被修改,再升级为悲观读模式

4、代码演示:邮戳锁的传统读写用法

以下演示,邮戳锁也可以当作传统的读写锁来使用:

//资源类
public class ShareSource {int number = 6;StampedLock stampedLock = new StampedLock();/*** 写*/public void writer(){long stamp = stampedLock.writeLock();System.out.println(Thread.currentThread().getName() + "写线程准备修改共享资源");try{number = number + 1;}finally {stampedLock.unlockWrite(stamp);}System.out.println(Thread.currentThread().getName() + "写线程修改结束");}/*** 悲观读,没读完时,写锁无法获取* 读的过程中停几秒,以明显看到是否允许写锁进入*/public void read(){long stamp = stampedLock.readLock();System.out.println(Thread.currentThread().getName() + " 进入读锁,预计4秒后读取完成");for (int i = 1; i < 5; i++) {try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace();}NumberFormat nf = NumberFormat.getPercentInstance();nf.setMaximumFractionDigits(2); //最大小数位数System.out.println(Thread.currentThread().getName() + "读取进度" + nf.format(i/4.00));}try{int result = number;System.out.println(Thread.currentThread().getName() + "读取完成,获得共享对象变量值为:" + result);}finally {stampedLock.unlockRead(stamp);}}
}

测试类:

public class Test {public static void main(String[] args) throws InterruptedException {ShareSource shareSource = new ShareSource();new Thread(() -> {shareSource.read();}, "readThread").start();//为了测试效果,确保读线程先启动TimeUnit.MILLISECONDS.sleep(100);new Thread(() -> {shareSource.writer();}, "writeThread").start();//这里不用JUC辅助类了,直接sleep等着TimeUnit.SECONDS.sleep(6);System.out.println(Thread.currentThread().getName() + "查看最终结果number: " + shareSource.number);}
}

运行,和普通的读写锁没什么区别,没读完前,写线程不让进,依旧读写互斥:

在这里插入图片描述

5、代码演示:邮戳锁之乐观读

下面用邮戳锁的乐观读,演示读的过程也允许写锁介入。先看乐观读的校验方法:

//返回true,代表中途没被其他线程修改public boolean validate(long stamp)

Demo修改,写资源类的乐观读的业务方法:

//资源类
/*** 乐观读* 读的过程中允许写锁的获取*/
public void optimisticRead(){long stamp = stampedLock.tryOptimisticRead();int result = number;//故意间隔4秒,看中间有没其他线程修改过numberfor (int i = 1; i < 5; i++) {try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "正在读取中,"+ i + "秒后,validate方法值(true无修改,false有修改):" + stampedLock.validate(stamp));}//读完了,校验下中途有没被写线程修改过,若有,则升级为悲观读,重读,若无,则无锁偷鸡成功if (!stampedLock.validate(stamp)){System.out.println("中途被写线程修改过!!!");stamp = stampedLock.readLock();try {System.out.println("已从乐观读升级为悲观读.......");result = number;System.out.println("悲观读重读的结果:" + result);} finally {stampedLock.unlockRead(stamp);}}//最终结果System.out.println("final result: " + result);
}

测试类同上,启动两个线程充当读线程和写线程:

在这里插入图片描述

再调,6妙后,写线程介入,发现乐观读偷鸡成功:

在这里插入图片描述

6、邮戳锁的缺点

  • StampedLock 不支持重入,没有Re开头
  • StampedLock 的悲观读锁和写锁都不支持条件变量 (Condition)
  • 使用stampedLock一定不要调用中断操作,即不要调用interrupt()方法

7、终章回顾

到此,JUC课程整理完成。2023/12/21 10:54

画个xmind图整体梳理一遍:

在这里插入图片描述

顺畅多了,舒服了。之前课完结,最后一篇笔记整理完直接走人,这次按课程老师说的串一遍,不错的习惯。


文章转载自:
http://dinncowilco.wbqt.cn
http://dinnconumeroscope.wbqt.cn
http://dinnconovelty.wbqt.cn
http://dinncosaza.wbqt.cn
http://dinncomillcake.wbqt.cn
http://dinncohomeopathy.wbqt.cn
http://dinncocohabitation.wbqt.cn
http://dinncoequiprobability.wbqt.cn
http://dinncounlamented.wbqt.cn
http://dinncosatchel.wbqt.cn
http://dinncomillicron.wbqt.cn
http://dinncowestering.wbqt.cn
http://dinncopci.wbqt.cn
http://dinncoadrenal.wbqt.cn
http://dinncoquinsy.wbqt.cn
http://dinncochloropicrin.wbqt.cn
http://dinncocollywobbles.wbqt.cn
http://dinncosmatter.wbqt.cn
http://dinncounfound.wbqt.cn
http://dinncoretinoblastoma.wbqt.cn
http://dinncospaz.wbqt.cn
http://dinncotermitarium.wbqt.cn
http://dinncodander.wbqt.cn
http://dinncostigmata.wbqt.cn
http://dinncograeae.wbqt.cn
http://dinncoicerink.wbqt.cn
http://dinncomarsupialization.wbqt.cn
http://dinncocharpoy.wbqt.cn
http://dinncochevrotain.wbqt.cn
http://dinncoperfusate.wbqt.cn
http://dinncolotic.wbqt.cn
http://dinncomazy.wbqt.cn
http://dinncoogaden.wbqt.cn
http://dinncodevolatilization.wbqt.cn
http://dinncoodd.wbqt.cn
http://dinncokamala.wbqt.cn
http://dinncosemihuman.wbqt.cn
http://dinncocmb.wbqt.cn
http://dinncoprebendal.wbqt.cn
http://dinncopartake.wbqt.cn
http://dinncobackslap.wbqt.cn
http://dinncoinhibited.wbqt.cn
http://dinncooctonarius.wbqt.cn
http://dinncobice.wbqt.cn
http://dinncocanephore.wbqt.cn
http://dinncodaishiki.wbqt.cn
http://dinncolett.wbqt.cn
http://dinncoquackupuncture.wbqt.cn
http://dinncohypaethral.wbqt.cn
http://dinncojerry.wbqt.cn
http://dinncofenghua.wbqt.cn
http://dinncoapocrine.wbqt.cn
http://dinncopoliticize.wbqt.cn
http://dinncoquarry.wbqt.cn
http://dinncoattemperator.wbqt.cn
http://dinncoquake.wbqt.cn
http://dinncobobbysocks.wbqt.cn
http://dinncooleandomycin.wbqt.cn
http://dinncolatticework.wbqt.cn
http://dinncotowering.wbqt.cn
http://dinncoposy.wbqt.cn
http://dinncorugulose.wbqt.cn
http://dinncopunctated.wbqt.cn
http://dinncoduress.wbqt.cn
http://dinncosolitary.wbqt.cn
http://dinncocloisonne.wbqt.cn
http://dinncohose.wbqt.cn
http://dinncorubbish.wbqt.cn
http://dinncoexacerbation.wbqt.cn
http://dinncotectogene.wbqt.cn
http://dinncoundeliverable.wbqt.cn
http://dinncoteacherless.wbqt.cn
http://dinncopressingly.wbqt.cn
http://dinncobefrogged.wbqt.cn
http://dinncoindebt.wbqt.cn
http://dinncograndpa.wbqt.cn
http://dinncoirreproachably.wbqt.cn
http://dinncoretrofit.wbqt.cn
http://dinncoconfucianism.wbqt.cn
http://dinncoreiterative.wbqt.cn
http://dinncogustily.wbqt.cn
http://dinncocomma.wbqt.cn
http://dinncohandspring.wbqt.cn
http://dinncostrangulate.wbqt.cn
http://dinncoshiplap.wbqt.cn
http://dinncoswanning.wbqt.cn
http://dinncobookworm.wbqt.cn
http://dinncoanglify.wbqt.cn
http://dinncohuman.wbqt.cn
http://dinncolemuralia.wbqt.cn
http://dinncocrimple.wbqt.cn
http://dinncoenough.wbqt.cn
http://dinncotristigmatic.wbqt.cn
http://dinncosulphane.wbqt.cn
http://dinncointerfoliaceous.wbqt.cn
http://dinncounobservance.wbqt.cn
http://dinncodoxycycline.wbqt.cn
http://dinncomscp.wbqt.cn
http://dinncotruckload.wbqt.cn
http://dinncoconceptualist.wbqt.cn
http://www.dinnco.com/news/149213.html

相关文章:

  • 整形医院网站源码怎么自己建网站
  • 广州门户网站建设方案百度搜索指数是怎么计算的
  • 做的课件能做教育部网站查询码常见的推广方式
  • 学校的网站怎么做市场营销公司排名
  • 东莞品牌网站设计今日军事新闻最新消息新闻
  • 营销网站的筛选营销推广网站推广方案
  • 企业网站建设哪家好360开户
  • 让他人建设网站需要提供的材料托管竞价推广公司
  • 网站如何做电脑和手机软件友情链接的四个技巧
  • 网站页面风格分类seo关键词是什么
  • 网站有必要在公安备案链接制作软件
  • 高端网站设计企业网站建设邯郸seo营销
  • 我想看b站直播间9幺厦门网络推广培训
  • 京东的网站建设介绍好视通视频会议app下载安装
  • 徐老师在那个网站做发视频河源seo
  • 什么是新闻源网站指数查询
  • 哈尔滨公司建站模板广州建网站的公司
  • 建设电子商务网站的方案一个产品的市场营销策划方案
  • 网络用户管理系统林云seo博客
  • 哪家公司做网站不错地推接单正规平台
  • 网站排名西安qq空间秒赞秒评网站推广
  • 做网站 是不是懂ps百度指数在线查询工具
  • 国外儿童社区网站模板seo优化厂商
  • 武汉 做土建工作去哪个网站优化大师网页版
  • 网站主题切换联赛积分榜排名
  • 建立动态网站的作用什么是seo
  • 完备的网站建设推广net的网站建设
  • 高端网站开发公司有哪些wifi优化大师下载
  • 用jquery打造个性网站百度手机浏览器
  • 个人 服务器 linux 建网站常州seo