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 ...
随机推荐
- HashSet小试牛刀
HashSet详细介绍 import java.util.HashSet; import java.util.Iterator; public class Main { public static v ...
- Sweetkang 的机器学习实验室 (1)
前言 近年来,Machine Learning 在许多领域上已然取得了可喜的成就,非常火热.就我个人来讲,有意将业余 Sport Programming 的范围扩展一下,譬如 Topcoder Mar ...
- [LeetCode 题解]: String to Interger (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Replication--Alwayson+订阅
场景: 主服务器:SubServer1 从服务器:SubServer2 监听者:RepListener 分发库:DisServer 发布数据库:RepDB1 发布服务器:RepServer1 当fai ...
- 如何获得Android设备名称(ADB命令详细介绍)
豌豆荚.360手机管家等软件可以获取android设备名称,显示在界面上,如下图: 我们自己如何来获取设备名称 呢?答案如下: 在命令行中输入“adb shell”进入shell之后,再输入“cat ...
- WinForm Column cannot be added because its CellType property is null.
在Winform DatatGridView 控件中绑定了一个模型的属性,结果在生成窗口时,发生错误,异常信息如下 : Column cannot be added because its Cell ...
- 2018OCP最新题库052新加考题及答案整理-27
27.Examine these facts about a database: 1. USERS is the database default tablespace. 2. USER1, USER ...
- 一键生成ssl自签名证书脚本
#!/bin/bash -e # * 为必改项 # * 更换为你自己的域名 CN='' # 例如: demo.rancher.com # 扩展信任IP或域名 ## 一般ssl证书只信任域名的访问请求, ...
- Mysql创建、删除用户[转]
MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束): 1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 ...
- xshell连接centos虚拟机的几点注意
我家用电脑使用联通的宽带,使用virtualbox装了centos6,连接方式使用NAT网络,还有一个是网络地址转换(NAT),不清楚区别是什么,使用xshell连接 当使用cd /etc/sysco ...