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 ...
随机推荐
- CentOS 6.5 下HeartBeat的安装与配置
CentOS 6.5 下HeartBeat的安装与配置 参考网站: http://wenku.baidu.com/link?url=BvqJatdx1m12PLil-7YA1zkM0yUOEO8OnN ...
- 去掉桌面SVN
1新建记事本 2for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn" 3重命名 删除S ...
- 使用maven构建基本的web项目结构
由于当前公司在组织进行项目基本结构的整理,将以前通过eclipse/ ant 方式构建的项目向maven上迁移,于是便进行maven项目方面的调研. 对于maven项目,基本的结构已经在标准文件中: ...
- 七.jQuery源码解析之.toArray()
toArray()是将jQuery对象转换成数组 从源码中可以看到,这些常见的方法,都是直接从原生的 javascript中"借鉴"过来的.为什么这么说呢? 225行中,在运行时, ...
- Nginx实战入门教程
Nginx 简介 Nginx是一个高性能的http和反向代理服务器,它看起来好像不太符合英文单词的拼写习惯,因为Nginx是由名为 伊戈尔·赛索耶夫 的俄罗斯人开发的.Nginx主要特点为占用内存小, ...
- c# 类间关系
一.依赖关系 简单的理解,依赖就是一个类A使用到了另一个类B,而这种使用关系是具有偶然性的.临时性的.非常弱的,但是类B的变化会影响到类A.比如某人要过河,需要借用一条船,此时人与船之间的关 ...
- MPI 并行奇偶交换排序 + 集合通信函数 Sendrecv() Sendvecv_replace()
▶ <并行程序设计导论>第三章的例子程序 ● 代码 #include <stdio.h> #include <mpi.h> #include <stdlib. ...
- 206. Reverse Linked List + 92. Reverse Linked List II
▶ 关于单链表翻转的两个问题. ▶ 206. 翻转整个单链表. ● 自己的代码,9 ms,使用了递归. class Solution { public: ListNode* reverseList(L ...
- [Z] Windbg以及vs debug使用
Windbg 一篇中国人写的质量非常高的Windbg文章:篇中国人写的质量非常高的Windbg文章: http://www.yiiyee.cn/Blog/windbg/ code project上的W ...
- 「小程序JAVA实战」小程序视频展示页开发(52)
转自:https://idig8.com/2018/09/22/xiaochengxujavashizhanxiaochengxushipinzhanshiyekaifa51/ 这次说下,小程序的视频 ...