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 ...
随机推荐
- PLSQL在64位 win7/WIN8上运行(绿色版plsql、无需安装oracle客户端)
一.准备需要的文件: 1.plsql文件绿色压缩包 2.oracle客户端文件绿色压缩包 二.设置PLSQL 运行plsql,不登陆进入plsql点“工具”-“首选项” 按照如图设置“ oracle主 ...
- Python 中单例模式 4种方法
假设我们在一个与数据库交互的系统中,需要进行数据库操作,那么我们就有必要了解单例模式,单例模式使得我们不用每次进行数据库的操作时都去链接数据库. 我将循环渐进,由浅入深的写一下单例模式示例. 实例1: ...
- 第十届蓝桥杯 试题 E: 迷宫
试题 E: 迷宫 本题总分:15 分 [问题描述] 下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方. 010000 000100 001001 110000 迷 ...
- Spark系列(二) Spark Shell各种操作及详细说明
并行化scala集合(Parallelize) //加载数据1~10 val num=sc.parallelize(1 to 10) //每个数据项乘以2,注意 _*2记为一个函数(fun) val ...
- 利用spring的ApplicationListener实现springmvc容器的初始化加载
1.我们在使用springmvc进行配置的时候一般初始化都是在web.xml里面进行的,但是自己在使用的时候经常会测试一些数据,这样就只有加载spring-mvc.xml的配置文件来实现.为了更方便的 ...
- pythonNet day04
本地套接字 作用:用于本地不同程序间的进行数据传输 本地套接字的创建流程 1.创建套接字对象 sockfd = socket(AF_UNIX,SOCK_STREAM) 2.绑定本地套接字文件,如果文件 ...
- Vue 获取数据、事件对象、todolist
vue中在方法里获取data里的msg:this.msg 在微信小程序里this.data.msg 改变data里的msg:this.msg="改变后的msg" 可以通过list. ...
- django-csrf_exempt
from django.views.decorators.csrf import csrf_exempt @csrf_exempt # 前端ajax请求时需要验证,否则403def fun(reque ...
- Sublime Text 3 安装+注册+汉化
参考:https://www.cnblogs.com/h--d/p/7555119.html
- yii mailer 扩展发送邮件
// 将mailer扩张放到 yii 的extension目录下 $message = 'Hello World!'; $mailer = Yii::createComponent('applicat ...