Lock和Condition在JDK中LinkedBlockingQueue的应用
Lock和Condition在JDK中LinkedBlockingQueue的应用,核心源码注释解析如下:
import java.util.concurrent.LinkedBlockingQueue.Node;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock; /**
* LinkedBlockingQueue核心方法源码分析
*
*
*/
public class LinkedBlockingQueue { /** Current number of elements */
//使用AtomicInteger的原因是:LinkedBlockingQueue的take和put使用的是两把锁。所以需要对count进行同步。
//同时count使用AtomicInteger可以解决take和put的冲突操作
private final AtomicInteger count = new AtomicInteger(0); /** Lock held by take, poll, etc */
private final ReentrantLock takeLock = new ReentrantLock(); /** Wait queue for waiting takes */
private final Condition notEmpty = takeLock.newCondition(); //绑定takeLock /** Lock held by put, offer, etc */
private final ReentrantLock putLock = new ReentrantLock(); /** Wait queue for waiting puts */
private final Condition notFull = putLock.newCondition();//绑定putLock /**
* Inserts the specified element at the tail of this queue, waiting if
* necessary for space to become available.
*
* @throws InterruptedException
* {@inheritDoc}
* @throws NullPointerException
* {@inheritDoc}
*/
public void put(E e) throws InterruptedException {
if (e == null)
throw new NullPointerException();
// Note: convention in all put/take/etc is to preset local var
// holding count negative to indicate failure unless set.
int c = -1;
Node<E> node = new Node(e);
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
putLock.lockInterruptibly();
try {
/*
* Note that count is used in wait guard even though it is not
* protected by lock. This works because count can only decrease at
* this point (all other puts are shut out by lock), and we (or some
* other waiting put) are signalled if it ever changes from
* capacity. Similarly for all other uses of count in other wait
* guards.
*/
while (count.get() == capacity) {
notFull.await(); //绑定putLock
}
enqueue(node);
// 此处 c =size - 1(size为容器实际大小)
c = count.getAndIncrement();
if (c + 1 < capacity) //c+1 =size-1+1 =size ,如果c + 1 =size < capacity 的话
notFull.signal(); //唤醒其他生产者生产数据
} finally {
putLock.unlock();
}
if (c == 0) //c=size-1==0,就是size==1?如果size=1代表还有元素,通知消费者生产数据
signalNotEmpty(); //notEmpty.signal();
} /**
* Signals a waiting take. Called only from put/offer (which do not
* otherwise ordinarily lock takeLock.)
*/
private void signalNotEmpty() {
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
notEmpty.signal(); //绑定takeLock
} finally {
takeLock.unlock();
}
} public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
notEmpty.await(); //绑定takeLock
}
x = dequeue();
// 有点绕:获取当前count的值,然后减1。
c = count.getAndDecrement();
//而此时由于x = dequeue()代码消费了一个数据,所以c=size+1
if (c > 1)//当前c=size+1>1的话,则就是size>0.所以此时就是有元素的,唤醒其他消费者消费数据
notEmpty.signal(); //绑定takeLock
} finally {
takeLock.unlock();
}
if (c == capacity) //c = size +1 ==capacity 所以size =capacity-1,则容器还没有满,所以通知生产者生产数据
signalNotFull(); // 调用notFull.signal();
return x;
} /**
* Signals a waiting put. Called only from take/poll.
*/
private void signalNotFull() {
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
notFull.signal(); //绑定putLock
} finally {
putLock.unlock();
}
}
}
Lock和Condition在JDK中LinkedBlockingQueue的应用的更多相关文章
- Lock和Condition在JDK中ArrayBlockingQueue的应用
ArrayBlockingQueue的实现思路简单描述,ArrayBlockingQueue的底对于互斥访问使用的一个锁.细节参考源码take和put方法: import java.util.conc ...
- Java中的线程--Lock和Condition实现线程同步通信
随着学习的深入,我接触了更多之前没有接触到的知识,对线程间的同步通信有了更多的认识,之前已经学习过synchronized 实现线程间同步通信,今天来学习更多的--Lock,GO!!! 一.初时Loc ...
- 线程高级应用-心得5-java5线程并发库中Lock和Condition实现线程同步通讯
1.Lock相关知识介绍 好比我同时种了几块地的麦子,然后就等待收割.收割时,则是哪块先熟了,先收割哪块. 下面举一个面试题的例子来引出Lock缓存读写锁的案例,一个load()和get()方法返回值 ...
- Java并发(10)- 简单聊聊JDK中的七大阻塞队列
引言 JDK中除了上文提到的各种并发容器,还提供了丰富的阻塞队列.阻塞队列统一实现了BlockingQueue接口,BlockingQueue接口在java.util包Queue接口的基础上提供了pu ...
- 【Java线程】Lock、Condition
http://www.infoq.com/cn/articles/java-memory-model-5 深入理解Java内存模型(五)——锁 http://www.ibm.com/develope ...
- 【Java线程】锁机制:synchronized、Lock、Condition
http://www.infoq.com/cn/articles/java-memory-model-5 深入理解Java内存模型(五)——锁 http://www.ibm.com/develope ...
- 并发之lock的condition接口
13.死磕Java并发-----J.U.C之Condition 12.Condition使用总结 11.Java并发编程系列之十七:Condition接口 === 13.死磕Java并发-----J. ...
- Lock和Condition
1 什么是可重入锁 可重入锁是说一个线程在已经获取了该锁的情况下,还可以再次获取该锁. 主要的应用场景: 可重入锁指的是在一个线程中可以多次获取同一把锁,比如:一个线程在执行一个带锁的方法,该方法中又 ...
- 【Java线程】锁机制:synchronized、Lock、Condition(转)
原文地址 1.synchronized 把代码块声明为 synchronized,有两个重要后果,通常是指该代码具有 原子性(atomicity)和 可见性(visibility). 1.1 原子性 ...
随机推荐
- [IOI2014] 假期
Description 有\(N(N\leq 10^5)\)个排列在一条线上的城市,每个城市有\(val_i\)个景点.每天你可以选择在当前城市\(i\)游览景点,或者前往城市\(i-1\)或城市\( ...
- [转]group by 后使用 rollup 子句总结
group by 后使用 rollup 子句总结 一.如何理解group by 后带 rollup 子句所产生的效果 group by 后带 rollup 子句的功能可以理解为:先按一定的规则产生多种 ...
- 【Mysql】MySQL event 计划任务
一.查看event是否开启 show variables like '%sche%'; set global event_scheduler =1; 二. -- 设置时区并设置计划事件调度器开启,也可 ...
- 【shell编程】1、shell编程简介
Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释 ...
- API网关【gateway 】- 3
最近在公司进行API网关重写,公司内采用serverMesh进行服务注册,调用,这里结合之前学习对API网关服务进行简单的总结与分析. 由于采用了大量的nginx相关的东西,所以在此记录一下: 在ng ...
- API接口规范V1.0——制定好规范,才好合作开发
返回码规范: 统一六位 000000 表示成功! 参数相关返回码预留100000-199999:系统相关返回码预留200000-299999:数据中心310000-319999后续项目以此类推,后续根 ...
- Redis的五种数据类型的简单介绍和使用
1.准备工作: 1.1在Linux下安装Redis https://www.cnblogs.com/dddyyy/p/9763098.html 1.2启动Redis 先把root/redis的red ...
- 自封装node 的简单增删改查
1 首先引入的上篇node 链接mysql 里面的js var connect = require('./nodemysql.js'); 2 定义常量 const customerSQL = { qu ...
- 【读书笔记】iOS-优化iOS Web应用
一,代码优化: 代码优化是任何优化技术的第一步,因为归根结底网页上的一切都是构建在代码之上的.优秀的代码可以节省宽带,减少渲染延迟,以及提高页面的可读性和长远的可维护性.下面列出了一些在Web应用中编 ...
- 开源IDE code blocks黑色主题
操作系统:Fedora26 IDE版本: Code Blocks16.01 配置文件路径为: ~/.config/codeblocks 而不像一些教程写的在用户根目录下或者在软件安装目录 请将de ...