condition实现原理
|
void await() throws InterruptedException
|
进入等待,直到被通知或中断
|
|
void awaitUninterruptibly()
|
进入等待,直到被通知,不响应中断
|
|
long awaitNanos(long nanosTimeout) throws InterruptedException
|
等待xxx纳秒
|
|
boolean awaitUntil(Date deadline) throws InterruptedException
|
等待,直到某个时间点
|
|
void signal()
|
唤醒
|
|
void signalAll()
|
唤醒所有等待在condition上的线程,会从等待队列挨个signal()
|
public class BoundedQueue<T> {
private Object[] items;
//添加的下标,删除的下标和数组当前数量
private int addIndex, removeIndex, count;
private Lock lock = new ReentrantLock();
private Condition empty = lock.newCondition();
private Condition full = lock.newCondition();
//构造方法
public BoundedQueue(int size){
items = new Object[size];
}
//添加元素,如果数组满,则添加线程进入等待,直到有空位
public void add(T t) throws InterruptedException{
lock.lock();
try {
while (count == items.length) //改成if会如何
full.await();
items[addIndex] = t;
if(++addIndex == items.length)
addIndex = 0;
++count;
empty.signal();
}finally {
lock.unlock();
}
}
//从头部删除一个元素,如果数组空,则删除线程进入等待状态,直到添加新元素
public T remove() throws InterruptedException{
lock.lock();
try{
while (count == 0)
empty.await();
Object x = items[removeIndex];
if(++removeIndex == items.length)
removeIndex = 0;
--count;
full.signal();
return (T)x;
}finally {
lock.unlock();
}
}
}


public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter(); //当前线程加入等待队列
int savedState = fullyRelease(node); //释放同步状态,也就是释放锁
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null) // clean up if cancelled
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}

public final void signal() {
if (!isHeldExclusively()) //是否获取了锁(重入锁中是直接 return 独占锁线程==当前线程)
throw new IllegalMonitorStateException();
Node first = firstWaiter; //等待队列头节点
if (first != null)
doSignal(first);
}
private void doSignal(Node first) {
do {
if ( (firstWaiter = first.nextWaiter) == null) //等待队列首节点的后继节点为空,说明只有一个节点,那么尾节点也置空
lastWaiter = null;
first.nextWaiter = null;
} while (!transferForSignal(first) && (first = firstWaiter) != null); // 等待队列首节点唤醒失败,则唤醒下个节点
}
final boolean transferForSignal(Node node) {// 将节点加入同步队列
if (!compareAndSetWaitStatus(node, Node.CONDITION, 0)) // 设置节点状态为0----(等待队列状态只能为-2,-3,用-2进行cas设置失败,说明是-3)
return false;
Node p = enq(node); // 放入同步队列尾部
int ws = p.waitStatus;
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
LockSupport.unpark(node.thread);
return true;
}

condition实现原理的更多相关文章
- AbstractQueuedSynchronizer 原理分析 - Condition 实现原理
1. 简介 Condition是一个接口,AbstractQueuedSynchronizer 中的ConditionObject内部类实现了这个接口.Condition声明了一组等待/通知的方法,这 ...
- MySQL Index Condition Pushdown 原理与解析
Index Condition Pushdown(ICP)是针对mysql使用索引从表中检索行数据时的一种优化方法. 原理: 在没有ICP特性之前,存储引擎根据索引去基表查找并将数据返回给mysq ...
- lesson5:Condition的原理分析及demo展示
Condition 将 Object 监视器方法(wait,notify,和notifyAll)分解成截然不同的对象,以便通过将这些对象与任意Lock实现组合使用,为每个对象提供多个等待 set(wa ...
- 这一次搞懂SpringBoot核心原理(自动配置、事件驱动、Condition)
@ 目录 前言 正文 启动原理 事件驱动 自动配置原理 Condition注解原理 总结 前言 SpringBoot是Spring的包装,通过自动配置使得SpringBoot可以做到开箱即用,上手成本 ...
- [置顶] SPL讲解(6)--Condition篇
SmartPersistenceLayer 2.0 之 Condition篇 原理 强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...
- Java并发之Condition
在使用Lock之前,我们使用的最多的同步方式应该是synchronized关键字来实现同步方式了.配合Object的wait().notify()系列方法可以实现等待/通知模式.Condition接口 ...
- 深入理解Java并发之synchronized实现原理
深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型(@Annotation) 深入理解Java类加载器(ClassLoader) 深入 ...
- JAVA CAS原理深度分析 volatile,偏向锁,轻量级锁
JAVA CAS原理深度分析 http://blog.csdn.net/hsuxu/article/details/9467651 偏向锁,轻量级锁 https://blog.csdn.net/zqz ...
- Condition接口
<Java并发编程艺术>读书笔记 Condition介绍 任意一个Java对象,都拥有一组监视器方法(定义在java.lang.Object中),主要包括wait().wait(long ...
随机推荐
- SPOJ - AMR11A(DP)
Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did ...
- 洛谷 P2596 [ZJOI2006]书架 (splay)
题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本.由于这些 ...
- Centos操作系统配置VIP以及网络
打开虚拟机的网络配置文件 vi /etc/sysconfig/network-script/ifc*** (具体文件根据情况自行填写) 编辑这个文件添加入如下配置 IPADDR=ip地址 (添加VIP ...
- java-03 变量与运算符
1.java中的变量与常量 1.1 变量的定义: 变量,顾名思义就是会变的量,这种思想来源于数学,指的是一个不确定的量或者随时会改变的量. 在我们进行编程的过程中,有一些东西是会随着实际情况而发生变化 ...
- [LeetCode 题解]: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- C#生成二维码(加源码)
使用工具: Visual Studio(VS) 2013 第一步: 要用到一个类:QRCodeEncoder 这个类要添加一个动态库:ThoughtWorks.QRCode.dll(项目中有带) 然后 ...
- jQuery为DOM动态追加事件
处理一个列表绑定,因为是一个展示项目,没有使用复杂的插件,直接jsrender写了个模板,但是后面有一个操作按钮,去查看数据详情,想到了jquery使用on进行事件委托,然后就开搞 最初是这样写的: ...
- Maven项目编译时报错缺少tools.jar
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default ...
- BZOJ2430 chocolate
有一个显然的想法是因为最后要花分成n*m个小块,所以每条边一定是要被切开的. 所以直接排序就可以了qwq,按照代价从大到小切一定是最优的. #include<iostream> #incl ...
- “全栈2019”Java第二十章:按位与、按位或、异或、反码、位运算
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...