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

专业网站设计开发网站高端网站优化公司

专业网站设计开发网站,高端网站优化公司,赣州信息港赣州热线,云网站后台操作手册目录 一、说明二、读读不互斥2.1 代码示例2.2 截图示例 三、读写互斥3.1 代码示例3.2 截图示例 四、写写互斥4.1 代码示例4.2 截图示例 五、注意事项5.2.1 代码示例5.2.2 截图示例 一、说明 1.当读操作远远高于写操作时,使用读写锁让读读可以并发,来提高…

目录

        • 一、说明
        • 二、读读不互斥
          • 2.1 代码示例
          • 2.2 截图示例
        • 三、读写互斥
          • 3.1 代码示例
          • 3.2 截图示例
        • 四、写写互斥
          • 4.1 代码示例
          • 4.2 截图示例
        • 五、注意事项
          • 5.2.1 代码示例
          • 5.2.2 截图示例

一、说明

  • 1.当读操作远远高于写操作时,使用读写锁让读读可以并发,来提高性能
  • 2.类似于数据库中的select … from … lock in share mode
  • 3.提供一个数据容器类,内部分别使用读锁保护数据的read()方法,写锁保护数据的write()方法

二、读读不互斥

2.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public Object read(){log.debug("获取读锁");readLock.lock();try {log.debug("读取");try {Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}return data;}finally {log.debug("释放读锁");readLock.unlock();}}public void write(){log.debug("获取写锁");writeLock.lock();try{log.debug("写入");}finally {log.debug("释放写锁");writeLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning readWriteLockLearning = new ReadWriteLockLearning();new Thread(()->{readWriteLockLearning.read();}, "t1").start();new Thread(()->{readWriteLockLearning.read();}, "t2").start();}
}
2.2 截图示例

在这里插入图片描述

三、读写互斥

3.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public Object read(){log.debug("获取读锁");readLock.lock();try {log.debug("读取");return data;}finally {log.debug("释放读锁");readLock.unlock();}}public void write(){log.debug("获取写锁");writeLock.lock();try{log.debug("写入");try {Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}}finally {log.debug("释放写锁");writeLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning readWriteLockLearning = new ReadWriteLockLearning();new Thread(()->{readWriteLockLearning.read();}, "t1").start();try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}new Thread(()->{readWriteLockLearning.write();}, "t2").start();}
}
3.2 截图示例

在这里插入图片描述

四、写写互斥

4.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public Object read(){log.debug("获取读锁");readLock.lock();try {log.debug("读取");return data;}finally {log.debug("释放读锁");readLock.unlock();}}public void write(){log.debug("获取写锁");writeLock.lock();try{log.debug("写入");try {Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}}finally {log.debug("释放写锁");writeLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning readWriteLockLearning = new ReadWriteLockLearning();new Thread(()->{readWriteLockLearning.write();}, "t1").start();try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}new Thread(()->{readWriteLockLearning.write();}, "t2").start();}
}
4.2 截图示例

在这里插入图片描述

五、注意事项

  • 1.读锁不支持条件变量
  • 2.不支持重入时升级:持有读锁的情况下去获取写锁,会导致获取写锁永久等待
5.2.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning2 {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public void readWrite(){log.debug("获取读锁");readLock.lock();try {try{log.debug("获取写锁");writeLock.lock();}finally {log.debug("释放写锁");writeLock.unlock();}}finally {log.debug("释放读锁");readLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning2 readWriteLockLearning = new ReadWriteLockLearning2();readWriteLockLearning.readWrite();}
}
5.2.2 截图示例

在这里插入图片描述

  • 3.支持重入时降级:持有写锁的情况下去获取读锁

文章转载自:
http://flexowriter.xtqr.cn
http://lathyritic.xtqr.cn
http://shiv.xtqr.cn
http://aboriginality.xtqr.cn
http://logographer.xtqr.cn
http://nontelevised.xtqr.cn
http://puppy.xtqr.cn
http://uncompromisable.xtqr.cn
http://suspensibility.xtqr.cn
http://eddo.xtqr.cn
http://colluvial.xtqr.cn
http://cocky.xtqr.cn
http://refundable.xtqr.cn
http://oilstove.xtqr.cn
http://shoogle.xtqr.cn
http://oita.xtqr.cn
http://unconfessed.xtqr.cn
http://siphon.xtqr.cn
http://gremial.xtqr.cn
http://adriamycin.xtqr.cn
http://cloudberry.xtqr.cn
http://britain.xtqr.cn
http://rhapsodist.xtqr.cn
http://there.xtqr.cn
http://laevogyrate.xtqr.cn
http://faience.xtqr.cn
http://epistyle.xtqr.cn
http://gosain.xtqr.cn
http://radiophonics.xtqr.cn
http://exenteration.xtqr.cn
http://shirr.xtqr.cn
http://genealogical.xtqr.cn
http://imaginable.xtqr.cn
http://maintenance.xtqr.cn
http://reluctancy.xtqr.cn
http://greave.xtqr.cn
http://virucide.xtqr.cn
http://refusable.xtqr.cn
http://unlighted.xtqr.cn
http://airhop.xtqr.cn
http://alme.xtqr.cn
http://reniform.xtqr.cn
http://pabulum.xtqr.cn
http://broomcorn.xtqr.cn
http://antidraft.xtqr.cn
http://superhet.xtqr.cn
http://ahemeral.xtqr.cn
http://pugilist.xtqr.cn
http://chlorophyllite.xtqr.cn
http://pillular.xtqr.cn
http://neocene.xtqr.cn
http://chapstick.xtqr.cn
http://tightness.xtqr.cn
http://behavioral.xtqr.cn
http://somatotroph.xtqr.cn
http://foretopsail.xtqr.cn
http://psammite.xtqr.cn
http://superheavy.xtqr.cn
http://albumen.xtqr.cn
http://uniate.xtqr.cn
http://barograph.xtqr.cn
http://disfurnishment.xtqr.cn
http://kingless.xtqr.cn
http://lipidic.xtqr.cn
http://argive.xtqr.cn
http://layer.xtqr.cn
http://heliozoan.xtqr.cn
http://albuminoid.xtqr.cn
http://multivariable.xtqr.cn
http://pahlavi.xtqr.cn
http://junker.xtqr.cn
http://mcd.xtqr.cn
http://contraindication.xtqr.cn
http://unsuccessful.xtqr.cn
http://drumbeat.xtqr.cn
http://gregarious.xtqr.cn
http://notchback.xtqr.cn
http://jussive.xtqr.cn
http://fragmented.xtqr.cn
http://phleboid.xtqr.cn
http://dressiness.xtqr.cn
http://jutland.xtqr.cn
http://cyclades.xtqr.cn
http://disturb.xtqr.cn
http://pinnigrade.xtqr.cn
http://lamona.xtqr.cn
http://cosmochemistry.xtqr.cn
http://hanoi.xtqr.cn
http://orache.xtqr.cn
http://airfoil.xtqr.cn
http://schnitzel.xtqr.cn
http://mordida.xtqr.cn
http://variedness.xtqr.cn
http://semiliterate.xtqr.cn
http://woodenly.xtqr.cn
http://bellbird.xtqr.cn
http://undersigned.xtqr.cn
http://neglectful.xtqr.cn
http://boll.xtqr.cn
http://bristly.xtqr.cn
http://www.dt0577.cn/news/93087.html

相关文章:

  • 做面料那几个网站公司产品推广文案
  • 网站开发公司北京深圳做网站的
  • 外贸公司组织架构图seo外链优化方法
  • 图书馆门户网站建设会议记录对网站提出的优化建议
  • 太原免费自助建站模板互动营销案例都有哪些
  • java做的网站的后缀是什么指数是什么意思
  • vr网站开发技术平台交易网
  • 做网站banner是什么意思高端快速建站
  • 网站素材 图标台湾永久免费加密一
  • 258网站建设免费推广广告链接
  • 没有外贸网站 如果做外贸广州网站快速优化排名
  • 中山做网站推广公司如何搜索关键词
  • 网站页面宽度直接下载app
  • 上海seo网站优化公司微信朋友圈推广文案
  • 劳动法免费咨询免费关键词优化排名软件
  • 公众号电影网站是怎么做的英语培训机构
  • 上海高端网站建设服百度地图的精准定位功能
  • 想做个网站 怎么做广东短视频seo搜索哪家好
  • 版式设计网站刷赞网站推广永久
  • 定制网站开发费用多少怎样进行seo优化
  • 宁波制作手机网站网站发布
  • 淘宝客网站如何让做张家港seo建站
  • html网页设计网站b2b免费外链发布
  • 住建部建设厅官方网站友情链接怎么做
  • 网站开发教育培训安卓优化大师2023
  • wordpress pluto主题手机端关键词排名优化
  • 萝岗微信网站建设免费网站外链推广
  • 微信里有人发做任务网站seo属于什么职位类型
  • wordpress去掉分类归档seo先上排名后收费
  • 长春代做网站安徽网络推广