ArrayBlockingQueue 和LinkedBlockingQueue 代码解析(JDK8)
在使用线程池的时候,需要指定BlockingQueue 常用的一般有ArrayBlockingQueue和LinkedBlockingQueue
有一天被问到有什么区别没回答上来,因此从代码的层面解析一下
1 ArrayBlockingQueue
顾名思义,就是用Array来实现的queue Blockqing 则说明是线程安全的
public class ArrayBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable {
private static final long serialVersionUID = -817911632652898426L;
final Object[] items;
int takeIndex;
int putIndex;
int count;
final ReentrantLock lock;
private final Condition notEmpty;
private final Condition notFull;
}
items 存储数据的数组
takeIndex 取数据时数组的下标
putIndex 放数据时的下标
count 数据的数量
lock 使用ReentrantLock 来保证线程安全
notEmpty 非空信号量,用来进行取数据时的信号量
notFull 非满信号量,在写数据时数据满时的等待信号量
1 构造函数
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];//指定数组大小
lock = new ReentrantLock(fair); //根据参数确定lock是否为公平锁,默认为false
notEmpty = lock.newCondition(); //新建两个lock的信号量
notFull = lock.newCondition();
}
2 写数据
研究代码发现 put add offer三个方法都调用了enqueue方法,ArrayBlockingQueue 将对数组的实际操作在jdk8抽象了出来,相对于jdk7进行了一定优化
/**
* Inserts element at current put position, advances, and signals.
* Call only when holding lock.
*/
//该方法只有在对象获取到锁之后才能调用
private void enqueue(E x) {
// assert lock.getHoldCount() == 1;
// assert items[putIndex] == null;
final Object[] items = this.items; //获取数组对象
items[putIndex] = x; //putIndex 默认值为0
if (++putIndex == items.length) //在putIndex到达数组尾部时,重新指向数组第一个位置
putIndex = 0;
count++; //数组元素+1
notEmpty.signal(); //非空信号发送
}
(1) offer
offer方法 尝试插入数据,在数组满时返回false,正常插入 返回true
public boolean offer(E e) {
checkNotNull(e); //校验数据是否为null
final ReentrantLock lock = this.lock; //获取对象锁
lock.lock(); //对当前对象加锁
try {
if (count == items.length) //如果数组满,返回false
return false;
else {
enqueue(e); //数组没满,插入数据,返回true
return true;
}
} finally {
lock.unlock(); //释放锁
}
}
(2) add
ArrayBlockingQueue 调用了父类AbstractQueue的add方法,
在插入成功时返回true,在插入失败(数组满)时,抛出异常
AbstractQueue 的add方法调用了offer()方法,所以add是offer的功能升级版
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
(3) put
put方法 在进行数据插入时,会尝试获取锁并相应异常,同时,在数组满时,会一致等待,直到数组有了空闲空间
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();//尝试获取锁并相应异常
try {
while (count == items.length) //数组满,
notFull.await(); //等待非满信号
enqueue(e);
} finally {
lock.unlock(); //在数据正常插入或者其他线程抛出异常后,解锁
}
}
(4) offer(E e, long timeout, TimeUnit unit)
ArrayBlockingQueue 还提供了一种超时配置的方法,在数组数据满超过timeout后返回fasle
public boolean offer(E e, long timeout, TimeUnit unit)
throws InterruptedException {
checkNotNull(e);
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length) {
if (nanos <= 0)
return false;
nanos = notFull.awaitNanos(nanos); //condition超时后 返回-1
}
enqueue(e);
return true;
} finally {
lock.unlock();
}
}
3 取数据
和写数据一样,取数据jdk8也进行了一定优化 统一调用dequeue方法
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex]; //获取最老数据
items[takeIndex] = null; //最老数据位置置空
if (++takeIndex == items.length) //下标到达最后 置零
takeIndex = 0;
count--;
if (itrs != null) //itrl目前没看到初始化的位置 ,暂时不清楚有什么用
itrs.elementDequeued();
notFull.signal();
return x;
}
(1) poll(E e, long timeout, TimeUnit unit)
很简单 列表为空返回null否则放回对应数据
public E poll() {
final ReentrantLock lock = this.lock;
lock.lock(); //加锁
try {
return (count == 0) ? null : dequeue(); //列表为空返回null否则放回对应数据
} finally {
lock.unlock(); //解锁
}
}
(2) take(E e, long timeout, TimeUnit unit)
尝试加锁,在数组为空时一直等待,直到有新数据或者被外部中断
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
(3) peek(E e, long timeout, TimeUnit unit)
返回最老数据,但是不弹出数据,仅获取数据。在数组为空时返回null
因此一条数据可以重复peek多次
public E peek() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return itemAt(takeIndex); // null when queue is empty
} finally {
lock.unlock();
}
}
final E itemAt(int i) {
return (E) items[i];
}
(4) poll(long timeout, TimeUnit unit)(E e, long timeout, TimeUnit unit)
也提供了等待超过timeout 返回null的poll方法
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos); //如果超时awaitNanos 返回-1 ,最后返回null
}
return dequeue();
} finally {
lock.unlock();
}
}
2 LinkedBlockingQueue
顾名思义,就是使用链表来存储的线程安全的队列
public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable {
private static final long serialVersionUID = -6903933977591709194L;
private final int capacity;
private final AtomicInteger count;
transient LinkedBlockingQueue.Node<E> head;
private transient LinkedBlockingQueue.Node<E> last;
private final ReentrantLock takeLock;
private final Condition notEmpty;
private final ReentrantLock putLock;
private final Condition notFull;
}
capacity 链表的最大长度,默认为Integer.MAX_VALUE
count 元素数量
head 头节点
last 尾节点
takeLock 取数据锁
notEmpty 非空信号量
putLock 写数据锁
notFull 非满信号量
LinkedBlockingQueue 采用了读写锁分离,因此在短时间内产生大量读写操作时,
比arrayBlockingQueue性能更加优秀
1 构造函数
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity; //设置最大长度
last = head = new Node<E>(null); //
}
2写数据
LinkedBlockingQueue同样提供了三个函数 put offer add
同样提供了enqueue方法,该方法仅在获取到putLock 后执行
private void enqueue(Node<E> node) {
// assert putLock.isHeldByCurrentThread();
// assert last.next == null;
last = last.next = node; //在尾节点添加数据
}
(1) offer(E e)
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final AtomicInteger count = this.count; //获取元素数量
if (count.get() == capacity) //如果链表长度到达上限,返回null
return false;
int c = -1;
Node<E> node = new Node<E>(e); //创建新节点
final ReentrantLock putLock = this.putLock;
putLock.lock(); //写锁加锁
try {
if (count.get() < capacity) { //如果没有到达链表上限
enqueue(node); //新增节点
c = count.getAndIncrement(); //获取元素数量并将count+1(c=count,count++),
//读写锁分离,链表数量可能有减少
if (c + 1 < capacity) //如果链表数量没有达到上限,非满信号量通知
notFull.signal();
}
} finally {
putLock.unlock(); //解锁
}
if (c == 0) //如果链表原来的数量为0
signalNotEmpty(); //非空信号量通知
return c >= 0; //返回插入结果 成功返回true,失败返回fasle
}
private void signalNotEmpty() {
final ReentrantLock takeLock = this.takeLock; //获取读锁
takeLock.lock(); //读锁加锁,防止数据被读取
try {
notEmpty.signal(); //非空信号量通知
} finally {
takeLock.unlock(); //读锁解锁
}
}
(2) add(E e)
和ArrayBlockingQueue一样,直接调用offer方法,在新增成功后返回true,在新增失败后直接抛出异常
(3) put(E e)
put操作 和offer操作基本一致,只不过在链表满时进行等待,知道链表节点减少
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock; //获取写锁
final AtomicInteger count = this.count;
putLock.lockInterruptibly(); //对写锁加锁并相应异常
try {
while (count.get() == capacity) { //如果节点数量到达上限
notFull.await(); //等待非满信号量的通知
}
enqueue(node); //在数据被弹出后,插入新节点
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock(); //释放写锁
}
if (c == 0)
signalNotEmpty();
}
(4) offer(E e, long timeout, TimeUnit unit)
和ArrayBlockingQueue一样,如果链表ch长度到达上限,就等待timeout ,超时后直接返回fasle
3 读取数据
上dequeue
private E dequeue() {
// assert takeLock.isHeldByCurrentThread();
// assert head.item == null;
Node<E> h = head; // 获取头节点
Node<E> first = h.next; //first设置为新的头节点
h.next = h; // help GC //需要移除的节点next指向自己帮助gc
head = first; //head 置为新的头节点
E x = first.item; //获取返回值得item
first.item = null; //first item设置为null
return x; //返回item
}
(1) poll(E e, long timeout, TimeUnit unit)
public E poll() {
final AtomicInteger count = this.count; //获取数量
if (count.get() == 0) //如果链表节点数量为空 返回null
return null;
E x = null;
int c = -1;
final ReentrantLock takeLock = this.takeLock; //获取读锁并加锁
takeLock.lock();
try {
if (count.get() > 0) {
x = dequeue(); //获取数据
c = count.getAndDecrement(); //数量-1
if (c > 1) //剩余节点>1
notEmpty.signal(); //非空信号通知
}
} finally {
takeLock.unlock(); //读锁解锁
}
if (c == capacity) //可能有线程在等在非满信号,-1前数量=限定长度
signalNotFull();
return x;
}
private void signalNotFull() {
final ReentrantLock putLock = this.putLock; //获取写锁并加锁
putLock.lock();
try {
notFull.signal(); //非满信号通知
} finally {
putLock.unlock(); //写锁解锁
}
}
(2) peek(E e)
存在返回数据,不存在返回null,链表节点不便,仅获取数据
public E peek() {
if (count.get() == 0)
return null;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
Node<E> first = head.next;
if (first == null)
return null;
else
return first.item;
} finally {
takeLock.unlock();
}
}
(3) take(E e)
链表到达最大长度。等待,可以被异常中断
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();
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
(4) poll(long timeout, TimeUnit unit)
等待超过timeout 返回null
3 两者的区别
1 Linked读写锁分离,在短时间内发生大量读写交替操作时性能高
2 Array在读写操作时不需要维护额外节点,空间较少
3 Array使用int count Linked使用AtomicInteger ,
因此:Array使用唯一Lock来保证count强一致性,Linked使用Atomic来保证count的准确性

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Java编程基础阶段笔记 day 07 面向对象编程(上)
面向对象编程 笔记Notes 面向对象三条学习主线 面向过程 VS 面向对象 类和对象 创建对象例子 面向对象的内存分析 类的属性:成员变量 成员变量 VS 局部变量 类的方法 方法的重载 可变个数形参 面向对象:封装性 访问权限修饰符 构造方法(构造器) 给属性赋值的方法 UML类图 this 关键字 面向对象学习主线 类及类的成员:属性,方法,构造器,代码块,内部类 面向对象的三大特性:封装性,继承性,多态性 其他关键字:this,super,interface,final,static...... 面向过程 vs 面向对象 面向过程:强调的是功能行为 面向对象 :强调具备了功能的对象 类和对象的区别 类:抽象的(汽车设计模板) 对象:具体的,类的实例(根据模板造出的汽车) 类的成员:属性和方法 属性 = field = 成员变量 (成员)方法 = 函数 = method 面向对象的例子 1.创建一个类,并在类中提供必要的属性和方法 2.由类派生出对象。(创建对象) 3.调用对象中的属性和方法。(对象名.属性名/方法名) //创建一个类 class Person{ //属性 S...
-
下一篇
虚拟机在java堆中对象分配、布局和访问的过程
虚拟机在java堆中对象分配、布局和访问的过程 一、 对象的创建 从java程序,new指令开始 类加载 类加载通过后,内存分配 对象所需内存的大小在类加载完成后就可以完全确定,为对象分配空间的任务等于把一块确定大小的内存从Java堆中划分出来。 两种方法: 指针碰撞 假如,java堆中内存是绝对规整的,所有用过的内存和空闲的内存分为两部分,中间放一个指针作为分界点的指示器,那分配内存 空闲列表 假如,java堆中的内存并不是规整的,已使用的内存和空间的内存相互交错,如此虚拟机就必须维护一个列表,记录上哪些内存块是可用的, 分配方式的选择取决于:java堆是否规整;Java堆是否规整取决于:所采用的垃圾收集器是否带有压缩整理功能。 实例 在使用Serial、ParNew等带有Compact过程的收集器时,系统采用分配算法为:指针碰撞; 使用CMS,基于 Mark-Sweep算法的收集器时,系统采用分配算法为:空闲列表。 对象创建的线程安全问题 问题描述 对象创建在虚拟机中的行为非常频繁,即使只是修改一个指针所指向的位置,在并发情况下也并不是线程安全的,有可能出现正在给对象A分配 内存,...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- MySQL数据库在高并发下的优化方案
- SpringBoot2配置默认Tomcat设置,开启更多高级功能