intrinsicConditionQueue笔记
一, 使用conditionQueue需要注意的一些点:
一个conditionQueue被多种Predicate condition 使用是很正常的,所以当一个wait的线程被唤醒的时候,很有可能它监听的条件并没有成功,所以在wait醒后,最好再次进行条件的判断.
一定要在wait前就对条件进行判断,因为如果一个条件已经是true了,然后再进行wait,很可能会导致Missed Signals,
调用notify()比较危险,因为存在一个conditionQueue被多种Predicate condition 使用的情况,所以使用notify()可能会导致Missed Signals同样的问题,所以最好使用notifyAll() 这种的问题是,每次都notifyAll()可能会有过多的线程切换,会耗费性能
For all these reasons, when you wake up from wait you must test the condition predicate again, and go back to waiting
(or fail) if it is not yet true. Since you can wake up repeatedly without your condition predicate being true, you must
therefore always call wait from within a loop, testing the condition predicate in each iteration. The canonical form for a
condition wait is shown in Listing 14.7.
Listing 14.7. Canonical Form for Statedependent Methods.
void stateDependentMethod() throws InterruptedException {
// condition predicate must be guarded by lock
synchronized(lock) {
while (!conditionPredicate())
lock.wait();
// object is now in desired state
}
}
When using condition waits ( Object.wait or Condition.await ):
• Always have a condition predicatesome test of object state that must hold before proceeding;
• Always test the condition predicate before calling wait , and again after returning from wait ;
• Always call wait in a loop;
• Ensure that the state variables making up the condition predicate are guarded by the lock associated with the
condition queue;
• Hold the lock associated with the the condition queue when calling wait , notify , or notifyAll ; and
• Do not release the lock after checking the condition predicate but before acting on it.
Single notify can be used instead of notifyAll only when both of the following conditions hold:
• Uniform waiters. Only one condition predicate is associated with the condition queue, and each thread executes the
same logic upon returning from wait ; and
• One‐in, one‐out. A notification on the condition variable enables at most one thread to proceed.
intrinsicConditionQueue笔记的更多相关文章
- git-简单流程(学习笔记)
这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- SQL Server技术内幕笔记合集
SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- NET Core-学习笔记(三)
这里将要和大家分享的是学习总结第三篇:首先感慨一下这周跟随netcore官网学习是遇到的一些问题: a.官网的英文版教程使用的部分nuget包和我当时安装的最新包版本不一致,所以没法按照教材上给出的列 ...
- springMVC学习笔记--知识点总结1
以下是学习springmvc框架时的笔记整理: 结果跳转方式 1.设置ModelAndView,根据view的名称,和视图渲染器跳转到指定的页面. 比如jsp的视图渲染器是如下配置的: <!-- ...
- 读书笔记汇总 - SQL必知必会(第4版)
本系列记录并分享学习SQL的过程,主要内容为SQL的基础概念及练习过程. 书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL i ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
随机推荐
- Ionic 项目创建
1. Ionic介绍 Ionci 是一个强大的HTML5 应用程序框架. 可以帮助您使用Web技术,如HTML, CSS和Javascript构建原生体验的移动应用程序. Ionic主要关注外观和体验 ...
- 转-Python自然语言处理入门
Python自然语言处理入门 原文链接:http://python.jobbole.com/85094/ 分享到:20 本文由 伯乐在线 - Ree Ray 翻译,renlytime 校稿.未经许 ...
- Quick guide for converting from JAGS or BUGS to NIMBLE
Converting to NIMBLE from JAGS, OpenBUGS or WinBUGS NIMBLE is a hierarchical modeling package that u ...
- 黄聪:国内com域名转移到Godaddy详细教程(转)
原文:http://www.cnblogs.com/hsapphire/archive/2010/01/16/1649743.html 最近CCTV进行大量报道色情新闻,还举报CNNIC监管CN域名不 ...
- HDU 3746 Cyclic Nacklace (用kmp求循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Service Mesh扫盲
原文:http://www.infoq.com/cn/news/2017/12/why-service-mesh 摘要: 对 Service Mesh 的理解?它的出现最终是为了解决什么问题? Ser ...
- 我的第一个C程序
// // main.c // one // // Created by Shuang Gai on 2019/1/19. // Copyright © 2019 Shuang Gai. All ri ...
- django多对多数据库建立 以及数据的传输 并进行增加 删除 修改
多对多数据库的建立 class Host(models.Model): nid = models.AutoField(primary_key=True) #自增id hostname = models ...
- login oracle as sysdba
- Mybatis多表查询(一对一、一对多、多对多)
Mybatis的多表级联查询 . 一对一可以通过<association>实现,一对多和多对多通过<collection>实现. <discriminator> 元 ...