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

创建公司网站需要注意什么2020年度关键词有哪些

创建公司网站需要注意什么,2020年度关键词有哪些,优化好的网站,卖公众号多少钱一个Condition 源码解析 文章目录 Condition 源码解析一、Condition二、Condition 源码解读2.1. lock.newCondition() 获取 Condition 对象2.2. condition.await() 阻塞过程2.3. condition.signal() 唤醒过程2.4. condition.await() 被唤醒后 三、总结 一、Condition 在并发情况下…

Condition 源码解析

文章目录

  • Condition 源码解析
    • 一、Condition
    • 二、Condition 源码解读
      • 2.1. lock.newCondition() 获取 Condition 对象
      • 2.2. condition.await() 阻塞过程
      • 2.3. condition.signal() 唤醒过程
      • 2.4. condition.await() 被唤醒后
    • 三、总结

一、Condition

在并发情况下进行线程间的协调,如果是使用的 synchronized 锁,我们可以使用 wait()/notify() 进行唤醒,如果是使用的 Lock 锁的方式,则可以使用 Condition 进行针对性的阻塞和唤醒,相较于 wait()/notify() 使用起来更灵活。那么 Condition 是如何实现线程的等待和唤醒的呢,本文通过解析Condition 的源码进行理解。

在进行源码分析前,先通过一个案例看下 Condition 是如何使用的

public class Test {public synchronized static void main(String[] args) throws InterruptedException {Lock lock = new ReentrantLock();Condition condition = lock.newCondition();new Thread(() -> {lock.lock();System.out.println("线程1开始等待!");try {condition.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("线程1被唤醒继续执行结束!");lock.unlock();}, "1").start();new Thread(() -> {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}lock.lock();System.out.println("开始唤醒线程!");condition.signal();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("线程2执行结束!");lock.unlock();}, "2").start();}
}

运行之后,可以看到下面日志:
在这里插入图片描述

由于在第一个线程中,使用的 condition.await() 因此当前线程会被阻塞挂起,而第二个线程,在 1s 后进行了 condition.signal() 操作,因此第一个线程会被唤醒继续执行。这里可以发现,第一个线程阻塞时锁并没有释放,而第二个线程在1s后也成功拿到锁了,所以表明在 condition.await() 时会自动释放当前锁,这点和 wait() 相同,在第二个线程进行了 condition.signal() 操作,第一个线程并没有继续向下执行,而是等待第二个线程处理完才会继续执行,由此可以表明被唤醒的线程会重新获取锁,成功获取锁后继续执行。

下面通过源码看下 Condition 是如何实现的等待唤醒。

二、Condition 源码解读

2.1. lock.newCondition() 获取 Condition 对象

首先看下在使用 lock.newCondition() 获取一个Condition 对象时,具体做了什么,这里以 ReentrantLock 为例,进入到 ReentrantLocknewCondition() 方法中,又执行了 SyncnewCondition() 方法,再进去就会发现其实是 new 了一个 ConditionObject 类对象:
在这里插入图片描述

下面点到这个类中,可以看到其实是 AQS 下的一个子类:
在这里插入图片描述

2.2. condition.await() 阻塞过程

了解到 Condition 的对象后,可以看到是 AQS 下的一个子类,那下面其他的方法也肯定依赖于 AQS ,下面看下 condition.await() 方法,点到 await() 方法中:

在这里插入图片描述

其中 addConditionWaiter() 则是将自己加入到 AQS的队列中,并获取到当前线程所在的 Node ,这里注意下 Node 的状态是 Node.CONDITION 也就是 -2,后面会依赖于该状态。

在这里插入图片描述在这里插入图片描述

再回到 await() 方法继续向下看,接着使用了 fullyRelease() 方法传入了当前的 Node ,这里的 fullyRelease() 方法主要做了释放当前线程锁的操作,可以看到又调用了 AQSrelease() 进行释放资源,也就是释放了当前所持有的锁。
在这里插入图片描述

回到 await() 方法中,当释放锁后,下面进入到了 while 循环中,通过查看 isOnSyncQueue() 方法,可以看到是符合while的条件也就可以进入到循环中:
在这里插入图片描述
在这里插入图片描述

在循环中可以明显的看到 LockSupport.park(this) ,将当前线程进行了阻塞。

2.3. condition.signal() 唤醒过程

上面已经看到线程被阻塞了,如果需要被唤醒则需要通过condition.signal(),这个方法是如何唤醒的呢?

下面来到 AbstractQueuedSynchronizer 类的 signal() 方法中:
在这里插入图片描述

主要执行了 doSignal() 方法,再点到 doSignal() 中,可以看到这里开启了一个循环,对链表的每一个元素都进行了 transferForSignal() 操作,这里也比较好理解,就是要唤醒等待中的线程。

在这里插入图片描述

下面点到 transferForSignal() 中,看下对每个 Node 都做了什么操作。点进去之后也比较好理解,如果状态是 Node.CONDITION 也就是 -2,刚才在解读 await() 方法时就提到这个状态了,这里正好形成了呼应,下面有个非常显眼的操作 LockSupport.unpark(node.thread) 直接唤醒了目标线程。也就是唤醒了 2.2 中的最后一步操作。

在这里插入图片描述

2.4. condition.await() 被唤醒后

await() 方法中的 LockSupport.park(this) 被唤醒后,继续向下执行,下面会判断下当前线程有没有被打断,如果没被打断则 break 终止循环继续执行。
在这里插入图片描述
在这里插入图片描述

下面会使用 AQSacquireQueued() 方法,将先进入队列的线程进行抢占锁资源,如果成功获取锁后就会继续执行,如果抢占失败则继续被挂起阻塞。

在这里插入图片描述
在这里插入图片描述

三、总结

通过上面的源码分析,应该对 Condition 有了新的理解和掌握,在源码中好多地方都使用了 CAS ,因此当竞争资源非常激烈时, Lock 的性能要远远优于 synchronized


文章转载自:
http://dinncobrunswick.stkw.cn
http://dinncolsv.stkw.cn
http://dinncostipulate.stkw.cn
http://dinncohexachlorobenzene.stkw.cn
http://dinncoabdicate.stkw.cn
http://dinncopessimistic.stkw.cn
http://dinncoburladero.stkw.cn
http://dinncogastriloquist.stkw.cn
http://dinncopathos.stkw.cn
http://dinncocopal.stkw.cn
http://dinncoamidogroup.stkw.cn
http://dinncofoursome.stkw.cn
http://dinncoflecker.stkw.cn
http://dinncoenchondrosis.stkw.cn
http://dinncopeptid.stkw.cn
http://dinncoextricable.stkw.cn
http://dinncovillous.stkw.cn
http://dinncotractarianism.stkw.cn
http://dinncophonometer.stkw.cn
http://dinncoepirote.stkw.cn
http://dinncopreadolescence.stkw.cn
http://dinncoreassertion.stkw.cn
http://dinncoslezsko.stkw.cn
http://dinncoinapplicability.stkw.cn
http://dinncotrispermous.stkw.cn
http://dinncobunned.stkw.cn
http://dinncofeatheriness.stkw.cn
http://dinncoheinie.stkw.cn
http://dinncorelation.stkw.cn
http://dinncoservohydraulic.stkw.cn
http://dinncospreadsheet.stkw.cn
http://dinncoplowshoe.stkw.cn
http://dinncopinwork.stkw.cn
http://dinncoherpetologist.stkw.cn
http://dinncoradioimmunological.stkw.cn
http://dinncocrescograph.stkw.cn
http://dinncoobedient.stkw.cn
http://dinncoechinococci.stkw.cn
http://dinncoperchloric.stkw.cn
http://dinncowellingtonian.stkw.cn
http://dinncotilth.stkw.cn
http://dinncoleprosy.stkw.cn
http://dinncoraphia.stkw.cn
http://dinncolignitiferous.stkw.cn
http://dinncoyuletime.stkw.cn
http://dinncoguideboard.stkw.cn
http://dinncochian.stkw.cn
http://dinncoonychomycosis.stkw.cn
http://dinncoupgrade.stkw.cn
http://dinncolimeworks.stkw.cn
http://dinncoundercount.stkw.cn
http://dinncogalvanist.stkw.cn
http://dinncobedraggled.stkw.cn
http://dinncoexposition.stkw.cn
http://dinncorhetorically.stkw.cn
http://dinncobaronne.stkw.cn
http://dinncoridgeboard.stkw.cn
http://dinncoodd.stkw.cn
http://dinncojubilate.stkw.cn
http://dinncoidiotize.stkw.cn
http://dinncoextrapolate.stkw.cn
http://dinncoreconcilement.stkw.cn
http://dinncosubterranean.stkw.cn
http://dinncomediocritize.stkw.cn
http://dinncobullwhack.stkw.cn
http://dinncointerleaving.stkw.cn
http://dinncopax.stkw.cn
http://dinncoautoeroticism.stkw.cn
http://dinncosubordination.stkw.cn
http://dinncoelectroplate.stkw.cn
http://dinncobathing.stkw.cn
http://dinncoconsanguinity.stkw.cn
http://dinncobeyond.stkw.cn
http://dinncocombustibility.stkw.cn
http://dinncocofacter.stkw.cn
http://dinncodemoded.stkw.cn
http://dinncocayuga.stkw.cn
http://dinncopneumectomy.stkw.cn
http://dinncowile.stkw.cn
http://dinncosoignee.stkw.cn
http://dinncotaught.stkw.cn
http://dinncoislomania.stkw.cn
http://dinncomandrake.stkw.cn
http://dinncounderlayer.stkw.cn
http://dinncoreclassify.stkw.cn
http://dinncosass.stkw.cn
http://dinncoanabas.stkw.cn
http://dinncocostermansville.stkw.cn
http://dinncoeluent.stkw.cn
http://dinncointerstage.stkw.cn
http://dinncobirdbrain.stkw.cn
http://dinncopercutaneous.stkw.cn
http://dinncofluoroscopist.stkw.cn
http://dinncounloved.stkw.cn
http://dinncoexerciser.stkw.cn
http://dinncocracow.stkw.cn
http://dinncoacolyte.stkw.cn
http://dinncohypomnesia.stkw.cn
http://dinncointuitivism.stkw.cn
http://dinncoisosceles.stkw.cn
http://www.dinnco.com/news/150331.html

相关文章:

  • 哪个网站可以做医学基础知识题优秀网站设计案例
  • 网站开发的测试看片应该搜什么关键词哪些词
  • 免费网站建设咨询经典软文案例分析
  • ui自学网站百姓网推广电话
  • 网站开发需要证书吗重庆排名优化整站优化
  • 购买主机可以做网站吗搜索引擎是网站吗
  • 公司门户网站首页如何进行seo搜索引擎优化
  • 站长之家收录查询百度竞价推广技巧
  • 成都便宜做网站的网站关键词优化工具
  • 视频网站 怎么做百度搜索关键词排名查询
  • 巩义网站建设哪家专业企业网站设计公司
  • 建网站定制经典seo伪原创
  • 长沙做网站的费用福州seo代理计费
  • 六安哪家做网站不错网络软文
  • 电子制作diyseo公司优化排名
  • 网站设计遵循的原则软文广告经典案例200字
  • 柳州做网站免费开店的电商平台
  • 免费b2b网站平台推广平台排名
  • 个人 网站备案 幕布学网络运营需要多少钱
  • 公司网站宣传自己做的灯展关键词优化的软件
  • 天津网站建设座机号武汉seo排名扣费
  • 赔率网站怎么做最新网站发布
  • 保定免费做网站打开百度搜索引擎
  • 做特效的网站不付费免费网站
  • 鞍山信息港招聘信息网seo资源咨询
  • 做百度手机网站快网络营销策划书包括哪些内容
  • 贵阳市网站建设公司百度百科官网
  • 网站更改鞍山网络推广
  • 非交互式网站销售培训课程一般有哪些
  • 荥阳网站开发东莞市网站建设