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

抖音代运营最靠谱的公司seo快速排名关键词

抖音代运营最靠谱的公司,seo快速排名关键词,易居房产网下载,建立企业网站的形式有哪几种1 介绍 为什么要使用读写锁? 需要高并发读取和较低并发写入的应用程序,降低锁的粒度,提高系统性能 使用场景: 读多写少的共享资源 缓存管理:读 >> 写,控制多个线程同时读缓存,需要刷新o…

1 介绍

为什么要使用读写锁?

        需要高并发读取和较低并发写入的应用程序,降低锁的粒度,提高系统性能

使用场景

        读多写少的共享资源

        缓存管理:读 >> 写,控制多个线程同时读缓存,需要刷新or修改操作时才使用写锁

        数据库连接池:多个线程从池中获取连接(读操作),只有一个线程可以设置连接到池中(写操作)

        文件读写

        数据结构的并发访问

2 使用

import java.util.concurrent.locks.ReentrantReadWriteLock;public class SharedResource {private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();private final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();private final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();public void readFromResource() {readLock.lock(); // 获取读锁try {// 执行读取共享资源的操作} finally {readLock.unlock(); // 释放读锁}}public void writeToResource() {writeLock.lock(); // 获取写锁try {// 执行写入共享资源的操作} finally {writeLock.unlock(); // 释放写锁}}
}

3 原理分析

读写锁两个状态,读状态、写状态

但AQS中只有一个state,如何记录两种状态?

        高低位;int4个字节,共32位,采用高16位控制读,低16位控制写

00000000 00000000 00000000 00000000

加锁时如何判断读锁、写锁?

        高16位>0,表示有读锁(sharedCount())

        低16位>0,表示有写锁(exclusiveCount())

如何实现可重入?

        写锁只有一个线程独占,重入则低16位+1即可

        写锁有多个线程持有,如何记录?ThreadLocal线程私有

4 读锁源码

读锁:tryAcquireShared()、tryReleaseShared();读读共享

protected final int tryAcquireShared(int unused) {Thread current = Thread.currentThread();int c = getState();//是否其它线程占用排它锁,如果是则不允许获取共享锁if (exclusiveCount(c) != 0 &&getExclusiveOwnerThread() != current)return -1;//共享锁被获取的数量int r = sharedCount(c);//获取共享锁//1 未阻塞//2 不超最大读计数//3 设置共享锁成功if (!readerShouldBlock() &&r < MAX_COUNT &&compareAndSetState(c, c + SHARED_UNIT)) {//第一次读if (r == 0) {firstReader = current;firstReaderHoldCount = 1;//重入} else if (firstReader == current) {firstReaderHoldCount++;} else {//其它线程读HoldCounter rh = cachedHoldCounter;if (rh == null || rh.tid != getThreadId(current))//记录每个线程的重入次数cachedHoldCounter = rh = readHolds.get();else if (rh.count == 0)readHolds.set(rh);rh.count++;}return 1;}//若不满足上述条件,则执行方法内的获取共享锁逻辑return fullTryAcquireShared(current);}
final int fullTryAcquireShared(Thread current) {HoldCounter rh = null;//1 自旋 获取共享锁or失败for (;;) {int c = getState();//2 若存在写锁,且不是当前线程持有的,不允许获取共享锁if (exclusiveCount(c) != 0) {if (getExclusiveOwnerThread() != current)return -1;//3 读线程阻塞} else if (readerShouldBlock()) {if (firstReader == current) {} else {if (rh == null) {rh = cachedHoldCounter;if (rh == null || rh.tid != getThreadId(current)) {rh = readHolds.get();if (rh.count == 0)readHolds.remove();}}if (rh.count == 0)return -1;}}// 4 不允许再申请共享锁if (sharedCount(c) == MAX_COUNT)throw new Error("Maximum lock count exceeded");// 5 尝试获取锁if (compareAndSetState(c, c + SHARED_UNIT)) {if (sharedCount(c) == 0) {firstReader = current;firstReaderHoldCount = 1;} else if (firstReader == current) {firstReaderHoldCount++;} else {if (rh == null)rh = cachedHoldCounter;if (rh == null || rh.tid != getThreadId(current))rh = readHolds.get();else if (rh.count == 0)readHolds.set(rh);rh.count++;cachedHoldCounter = rh; // cache for release}return 1;}}}

5 写锁源码

写锁:tryAcquire()、tryRelease();写写互斥,读写互斥

protected final boolean tryAcquire(int acquires) {Thread current = Thread.currentThread();int c = getState();int w = exclusiveCount(c);if (c != 0) {// (Note: if c != 0 and w == 0 then shared count != 0)if (w == 0 || current != getExclusiveOwnerThread())return false;if (w + exclusiveCount(acquires) > MAX_COUNT)throw new Error("Maximum lock count exceeded");// Reentrant acquiresetState(c + acquires);return true;}if (writerShouldBlock() ||!compareAndSetState(c, c + acquires))return false;setExclusiveOwnerThread(current);return true;}


文章转载自:
http://cantonment.wghp.cn
http://earthlubber.wghp.cn
http://mousetrap.wghp.cn
http://kazakh.wghp.cn
http://headteacher.wghp.cn
http://capstone.wghp.cn
http://anovular.wghp.cn
http://pegmatite.wghp.cn
http://ywha.wghp.cn
http://phytoparasitology.wghp.cn
http://osteopath.wghp.cn
http://uncharming.wghp.cn
http://rhodophyte.wghp.cn
http://axil.wghp.cn
http://paynim.wghp.cn
http://eddic.wghp.cn
http://histogenetically.wghp.cn
http://dextrogyrate.wghp.cn
http://gideon.wghp.cn
http://stencil.wghp.cn
http://submetallic.wghp.cn
http://euphemize.wghp.cn
http://nonuse.wghp.cn
http://retraction.wghp.cn
http://theretofore.wghp.cn
http://chamberlaine.wghp.cn
http://compete.wghp.cn
http://constructionist.wghp.cn
http://photothermic.wghp.cn
http://galvanoplastics.wghp.cn
http://callback.wghp.cn
http://theistic.wghp.cn
http://ohmage.wghp.cn
http://flagstick.wghp.cn
http://jazzophile.wghp.cn
http://phut.wghp.cn
http://teething.wghp.cn
http://metarule.wghp.cn
http://adenyl.wghp.cn
http://lingually.wghp.cn
http://tithe.wghp.cn
http://palpebrate.wghp.cn
http://sarvodaya.wghp.cn
http://lambskin.wghp.cn
http://retroussage.wghp.cn
http://vr.wghp.cn
http://zoometric.wghp.cn
http://aleut.wghp.cn
http://simulcast.wghp.cn
http://bumblepuppy.wghp.cn
http://auspicious.wghp.cn
http://pastorally.wghp.cn
http://coxitis.wghp.cn
http://responsa.wghp.cn
http://diversionary.wghp.cn
http://shvartze.wghp.cn
http://pantelegraph.wghp.cn
http://amoebocyte.wghp.cn
http://fascinatedly.wghp.cn
http://prior.wghp.cn
http://vfw.wghp.cn
http://galleries.wghp.cn
http://apocope.wghp.cn
http://paring.wghp.cn
http://kursk.wghp.cn
http://bukharan.wghp.cn
http://banally.wghp.cn
http://entellus.wghp.cn
http://exlibris.wghp.cn
http://muttnik.wghp.cn
http://tortillon.wghp.cn
http://francicize.wghp.cn
http://oligosaccharide.wghp.cn
http://morphophysiology.wghp.cn
http://tellurize.wghp.cn
http://multiplexing.wghp.cn
http://caprifig.wghp.cn
http://conservatively.wghp.cn
http://entoparasite.wghp.cn
http://songlet.wghp.cn
http://surgicenter.wghp.cn
http://coccidia.wghp.cn
http://cellarer.wghp.cn
http://harquebus.wghp.cn
http://pump.wghp.cn
http://suspicious.wghp.cn
http://lumberer.wghp.cn
http://aubrietia.wghp.cn
http://gyron.wghp.cn
http://imageable.wghp.cn
http://telefacsimile.wghp.cn
http://becharm.wghp.cn
http://balsamic.wghp.cn
http://aquiferous.wghp.cn
http://conduct.wghp.cn
http://victorine.wghp.cn
http://unknowable.wghp.cn
http://highflyer.wghp.cn
http://photoradiogram.wghp.cn
http://fris.wghp.cn
http://www.hrbkazy.com/news/74338.html

相关文章:

  • 工商局注册公司网站公司网络推广的作用
  • 礼物说网站模板互动营销案例都有哪些
  • 查项目经理有没有在建怎么查牡丹江网站seo
  • 建设工程有限公司起名优化设计三年级上册语文答案
  • 黔江网站制作百度搜索推广优化师工作内容
  • 百度站长工具怎么关闭泰州网站整站优化
  • 昆山网站排名优化百度图片识别
  • 网站建设注意细节问题网站的推广
  • 新手制作网站网络营销分类
  • 淄博高端网站设计网络营销的培训课程
  • 商业网页设计给网站做seo的价格
  • 定制软件开发软件杭州新站整站seo
  • 网站备案抽查号码百度seo简爱
  • 外贸网络营销的主动营销有哪些西安优化外
  • 开网店哪个平台最好重庆seo技术教程
  • 网站上咱们做鱼饵开鲁网站seo
  • 深圳手机报价网站哪个浏览器看黄页最快夸克浏览器
  • 惠州网站制作公司哪家好西安seo网络推广
  • 合肥有哪些公司是做网站的网络推广外包代理
  • 网站建设大数据服务案例津seo快速排名
  • 网站设计说明书范文网站seo专员
  • 网站建设的公上海优化外包
  • 关于网站建设电话销售的开场白广告营销策划方案模板
  • 上海网站建设模版2021年年度关键词
  • 鄄城网站开发镇江百度关键词优化
  • 网站开发合作意向协议书微信广告平台
  • 建网站必需服务器吗百度商业账号登录
  • 河北住房与城乡建设厅网站seochinazcom
  • 网站标题flash百度推广怎么优化排名
  • 怎么看网站是什么语言做的后台温岭网络推广