一、前言

  分析完了ArrayBlockingQueue后,接着分析LinkedBlockingQueue,与ArrayBlockingQueue不相同,LinkedBlockingQueue底层采用的是链表结构,其源码也相对比较简单,下面进行正式的分析。

二、LinkedBlockingQueue数据结构

  从LinkedBlockingQueue的命名就大致知道其数据结构采用的是链表结构,通过源码也可以验证我们的猜测,其数据结构如下。

  说明:可以看到LinkedBlockingQueue采用的是单链表结构,包含了头结点和尾节点。

三、LinkedBlockingQueue源码分析

  3.1 类的继承关系  

public class LinkedBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {}

  说明:LinkedBlockingQueue继承了AbstractQueue抽象类,AbstractQueue定义了对队列的基本操作;同时实现了BlockingQueue接口,BlockingQueue表示阻塞型的队列,其对队列的操作可能会抛出异常;同时也实现了Searializable接口,表示可以被序列化。

  3.2 类的内部类

  LinkedBlockingQueue内部有一个Node类,表示结点,用于存放元素,其源码如下。  

    static class Node<E> {
// 元素
E item;
// next域
Node<E> next;
// 构造函数
Node(E x) { item = x; }
}

  说明:Node类非常简单,包含了两个域,分别用于存放元素和指示下一个结点。

  3.3 类的属性  

public class LinkedBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {
// 版本序列号
private static final long serialVersionUID = -6903933977591709194L;
// 容量
private final int capacity;
// 元素的个数
private final AtomicInteger count = new AtomicInteger();
// 头结点
transient Node<E> head;
// 尾结点
private transient Node<E> last;
// 取元素锁
private final ReentrantLock takeLock = new ReentrantLock();
// 非空条件
private final Condition notEmpty = takeLock.newCondition();
// 存元素锁
private final ReentrantLock putLock = new ReentrantLock();
// 非满条件
private final Condition notFull = putLock.newCondition();
}

  说明:可以看到LinkedBlockingQueue包含了读、写重入锁(与ArrayBlockingQueue不同,ArrayBlockingQueue只包含了一把重入锁),读写操作进行了分离,并且不同的锁有不同的Condition条件(与ArrayBlockingQueue不同,ArrayBlockingQueue是一把重入锁的两个条件)。

  3.4 类的构造函数

  1. LinkedBlockingQueue()型构造函数  

    public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}

  说明:该构造函数用于创建一个容量为 Integer.MAX_VALUE 的 LinkedBlockingQueue。

  2. LinkedBlockingQueue(int)型构造函数  

    public LinkedBlockingQueue(int capacity) {
// 初始化容量必须大于0
if (capacity <= 0) throw new IllegalArgumentException();
// 初始化容量
this.capacity = capacity;
// 初始化头结点和尾结点
last = head = new Node<E>(null);
}

  说明:该构造函数用于创建一个具有给定(固定)容量的 LinkedBlockingQueue。

  3. LinkedBlockingQueue(Collection<? extends E>)型构造函数 

    public LinkedBlockingQueue(Collection<? extends E> c) {
// 调用重载构造函数
this(Integer.MAX_VALUE);
// 存锁
final ReentrantLock putLock = this.putLock;
// 获取锁
putLock.lock(); // Never contended, but necessary for visibility
try {
int n = 0;
for (E e : c) { // 遍历c集合
if (e == null) // 元素为null,抛出异常
throw new NullPointerException();
if (n == capacity) //
throw new IllegalStateException("Queue full");
enqueue(new Node<E>(e));
++n;
}
count.set(n);
} finally {
putLock.unlock();
}
}

  说明:该构造函数用于创建一个容量是 Integer.MAX_VALUE 的 LinkedBlockingQueue,最初包含给定 collection 的元素,元素按该 collection 迭代器的遍历顺序添加。

  3.5 核心函数分析

  1. put函数  

    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>(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条件上进行等待
notFull.await();
}
// 入队列
enqueue(node);
// 更新元素个数,返回的是以前的元素个数
c = count.getAndIncrement();
if (c + 1 < capacity) // 元素个数是否小于容量
// 唤醒在notFull条件上等待的某个线程
notFull.signal();
} finally {
// 释放锁
putLock.unlock();
}
if (c == 0) // 元素个数为0,表示已有take线程在notEmpty条件上进入了等待,则需要唤醒在notEmpty条件上等待的线程
signalNotEmpty();
}

  说明:put函数用于存放元素,其流程如下。

  ① 判断元素是否为null,若是,则抛出异常,否则,进入步骤②

  ② 获取存元素锁,并上锁,如果当前线程被中断,则抛出异常,否则,进入步骤③

  ③ 判断当前队列中的元素个数是否已经达到指定容量,若是,则在notFull条件上进行等待,否则,进入步骤④

  ④ 将新生结点入队列,更新队列元素个数,若元素个数小于指定容量,则唤醒在notFull条件上等待的线程,表示可以继续存放元素。进入步骤⑤

  ⑤ 释放锁,判断结点入队列之前的元素个数是否为0,若是,则唤醒在notEmpty条件上等待的线程(表示队列中没有元素,取元素线程被阻塞了)。

  put函数中会调用到enqueue函数和signalNotEmpty函数,enqueue函数源码如下  

    private void enqueue(Node<E> node) {
// assert putLock.isHeldByCurrentThread();
// assert last.next == null;
// 更新尾结点域
last = last.next = node;
}

  说明:可以看到,enqueue函数只是更新了尾节点。signalNotEmpty函数源码如下 

    private void signalNotEmpty() {
// 取元素锁
final ReentrantLock takeLock = this.takeLock;
// 获取锁
takeLock.lock();
try {
// 唤醒在notEmpty条件上等待的某个线程
notEmpty.signal();
} finally {
// 释放锁
takeLock.unlock();
}
}

  说明:signalNotEmpty函数用于唤醒在notEmpty条件上等待的线程,其首先获取取元素锁,然后上锁,然后唤醒在notEmpty条件上等待的线程,最后释放取元素锁。

  2. offer函数 

    public boolean offer(E e) {
// 确保元素不为null
if (e == null) throw new NullPointerException();
// 获取计数器
final AtomicInteger count = this.count;
if (count.get() == capacity) // 元素个数到达指定容量
// 返回
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();
if (c + 1 < capacity) // 元素个数是否小于容量
// 唤醒在notFull条件上等待的某个线程
notFull.signal();
}
} finally {
// 释放锁
putLock.unlock();
}
if (c == 0) // 元素个数为0,则唤醒在notEmpty条件上等待的某个线程
signalNotEmpty();
return c >= 0;
}

  说明:offer函数也用于存放元素,offer函数添加元素不会抛出异常(其他的域put函数类似)。

  3. take函数

    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) { // 元素个数为0
// 在notEmpty条件上等待
notEmpty.await();
}
// 出队列
x = dequeue();
// 更新元素个数,返回的是以前的元素个数
c = count.getAndDecrement();
if (c > 1) // 元素个数大于1,则唤醒在notEmpty上等待的某个线程
notEmpty.signal();
} finally {
// 释放锁
takeLock.unlock();
}
if (c == capacity) // 元素个数到达指定容量
// 唤醒在notFull条件上等待的某个线程
signalNotFull();
// 返回
return x;
}

  说明:take函数用于获取一个元素,其与put函数相对应,其流程如下。

  ① 获取取元素锁,并上锁,如果当前线程被中断,则抛出异常,否则,进入步骤②

  ② 判断当前队列中的元素个数是否为0,若是,则在notEmpty条件上进行等待,否则,进入步骤③

  ③ 出队列,更新队列元素个数,若元素个数大于1,则唤醒在notEmpty条件上等待的线程,表示可以继续取元素。进入步骤④

  ④ 释放锁,判断结点出队列之前的元素个数是否为指定容量,若是,则唤醒在notFull条件上等待的线程(表示队列已满,存元素线程被阻塞了)。

  take函数调用到了dequeue函数和signalNotFull函数,dequeue函数源码如下  

    private E dequeue() {
// assert takeLock.isHeldByCurrentThread();
// assert head.item == null;
// 头结点
Node<E> h = head;
// 第一个结点
Node<E> first = h.next;
// 头结点的next域为自身
h.next = h; // help GC
// 更新头结点
head = first;
// 返回头结点的元素
E x = first.item;
// 头结点的item域赋值为null
first.item = null;
// 返回结点元素
return x;
}

  说明:dequeue函数的作用是将头结点更新为之前头结点的下一个结点,并且将更新后的头结点的item域设置为null。signalNotFull函数的源码如下

    private void signalNotFull() {
// 存元素锁
final ReentrantLock putLock = this.putLock;
// 获取锁
putLock.lock();
try {
// 唤醒在notFull条件上等待的某个线程
notFull.signal();
} finally {
// 释放锁
putLock.unlock();
}
}

  说明:signalNotFull函数用于唤醒在notFull条件上等待的某个线程,其首先获取存元素锁,然后上锁,然后唤醒在notFull条件上等待的线程,最后释放存元素锁。

  4. poll函数  

    public E poll() {
// 获取计数器
final AtomicInteger count = this.count;
if (count.get() == 0) // 元素个数为0
return null;
//
E x = null;
int c = -1;
// 取元素锁
final ReentrantLock takeLock = this.takeLock;
// 获取锁
takeLock.lock();
try {
if (count.get() > 0) { // 元素个数大于0
// 出队列
x = dequeue();
// 更新元素个数,返回的是以前的元素个数
c = count.getAndDecrement();
if (c > 1) // 元素个数大于1
// 唤醒在notEmpty条件上等待的某个线程
notEmpty.signal();
}
} finally {
// 释放锁
takeLock.unlock();
}
if (c == capacity) // 元素大小达到指定容量
// 唤醒在notFull条件上等待的某个线程
signalNotFull();
// 返回元素
return x;
}

  说明:poll函数也用于存放元素,poll函数添加元素不会抛出异常(其他的与take函数类似)。

  5. remove函数  

    public boolean remove(Object o) {
// 元素为null,返回false
if (o == null) return false;
// 获取存元素锁和取元素锁(不允许存或取元素)
fullyLock();
try {
for (Node<E> trail = head, p = trail.next;
p != null;
trail = p, p = p.next) { // 遍历整个链表
if (o.equals(p.item)) { // 结点的值与指定值相等
// 断开结点
unlink(p, trail);
return true;
}
}
return false;
} finally {
fullyUnlock();
}
}

  说明:remove函数的流程如下

  ① 获取读、写锁(防止此时继续出、入队列)。进入步骤②

  ② 遍历链表,寻找指定元素,若找到,则将该结点从链表中断开,有利于被GC,进入步骤③

  ③ 释放读、写锁(可以继续出、入队列)。步骤②中找到指定元素则返回true,否则,返回false。

  其中,remove函数会调用unlink函数,其源码如下  

    void unlink(Node<E> p, Node<E> trail) {
// assert isFullyLocked();
// p.next is not changed, to allow iterators that are
// traversing p to maintain their weak-consistency guarantee.
// 结点的item域赋值为null
p.item = null;
// 断开p结点
trail.next = p.next;
if (last == p) // 尾节点为p结点
// 重新赋值尾节点
last = trail;
if (count.getAndDecrement() == capacity) // 更新元素个数,返回的是以前的元素个数,若结点个数到达指定容量
// 唤醒在notFull条件上等待的某个线程
notFull.signal();
}

  说明:unlink函数用于将指定结点从链表中断开,并且更新队列元素个数,并且判断若之前队列元素的个数达到了指定容量,则会唤醒在notFull条件上等待的某个线程。

四、示例

  下面通过一个示例来了解LinkedBlockingQueue的使用。  

package com.hust.grid.leesf.collections;

import java.util.concurrent.LinkedBlockingQueue;
class PutThread extends Thread {
private LinkedBlockingQueue<Integer> lbq;
public PutThread(LinkedBlockingQueue<Integer> lbq) {
this.lbq = lbq;
} public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("put " + i);
lbq.put(i);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} class GetThread extends Thread {
private LinkedBlockingQueue<Integer> lbq;
public GetThread(LinkedBlockingQueue<Integer> lbq) {
this.lbq = lbq;
} public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("take " + lbq.take());
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class LinkedBlockingQueueDemo {
public static void main(String[] args) {
LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>(); PutThread p1 = new PutThread(lbq);
GetThread g1 = new GetThread(lbq); p1.start();
g1.start();
}
}

  运行结果:  

put 0
take 0
put 1
take 1
put 2
take 2
put 3
take 3
put 4
take 4
put 5
take 5
put 6
take 6
put 7
take 7
put 8
take 8
put 9
take 9

  说明:示例中使用了两个线程,一个用于存元素,一个用于读元素,存和读各10次,每个线程存一个元素或者读一个元素后都会休眠100ms,可以看到结果是交替打 印,并且首先打印的肯定是put线程语句(因为若取线程先取元素,此时队列并没有元素,其会阻塞,等待存线程存入元素),并且最终程序可以正常结束。

  ① 若修改取元素线程,将存的元素的次数修改为15次(for循环的结束条件改为15即可),运行结果如下:  

put 0
take 0
put 1
take 1
put 2
take 2
put 3
take 3
put 4
take 4
put 5
take 5
put 6
take 6
put 7
take 7
put 8
take 8
put 9
take 9

  说明:运行结果与上面的运行结果相同,但是,此时程序无法正常结束,因为take方法被阻塞了,等待被唤醒。

五、总结

  LinkedBlockingQueue的源码相对比较简单,其也是通过ReentrantLock和Condition条件来保证多线程的正确访问的,并且取元素(出队列)和存元素(入队列)是采用不同的锁,进行了读写分离,有利于提高并发度。LinkedBockingQueue的分析就到这里,欢迎交流,谢谢各位园友的观看~

【JUC】JDK1.8源码分析之LinkedBlockingQueue(四)的更多相关文章

  1. 【1】【JUC】JDK1.8源码分析之ArrayBlockingQueue,LinkedBlockingQueue

    概要: ArrayBlockingQueue的内部是通过一个可重入锁ReentrantLock和两个Condition条件对象来实现阻塞 注意这两个Condition即ReentrantLock的Co ...

  2. 【JUC】JDK1.8源码分析之ArrayBlockingQueue(三)

    一.前言 在完成Map下的并发集合后,现在来分析ArrayBlockingQueue,ArrayBlockingQueue可以用作一个阻塞型队列,支持多任务并发操作,有了之前看源码的积累,再看Arra ...

  3. 【1】【JUC】JDK1.8源码分析之ReentrantLock

    概要: ReentrantLock类内部总共存在Sync.NonfairSync.FairSync三个类,NonfairSync与FairSync类继承自Sync类,Sync类继承自AbstractQ ...

  4. 【集合框架】JDK1.8源码分析HashSet && LinkedHashSet(八)

    一.前言 分析完了List的两个主要类之后,我们来分析Set接口下的类,HashSet和LinkedHashSet,其实,在分析完HashMap与LinkedHashMap之后,再来分析HashSet ...

  5. 【集合框架】JDK1.8源码分析之HashMap(一) 转载

    [集合框架]JDK1.8源码分析之HashMap(一)   一.前言 在分析jdk1.8后的HashMap源码时,发现网上好多分析都是基于之前的jdk,而Java8的HashMap对之前做了较大的优化 ...

  6. 【集合框架】JDK1.8源码分析之ArrayList详解(一)

    [集合框架]JDK1.8源码分析之ArrayList详解(一) 一. 从ArrayList字表面推测 ArrayList类的命名是由Array和List单词组合而成,Array的中文意思是数组,Lis ...

  7. 集合之TreeSet(含JDK1.8源码分析)

    一.前言 前面分析了Set接口下的hashSet和linkedHashSet,下面接着来看treeSet,treeSet的底层实现是基于treeMap的. 四个关注点在treeSet上的答案 二.tr ...

  8. 集合之LinkedHashSet(含JDK1.8源码分析)

    一.前言 上篇已经分析了Set接口下HashSet,我们发现其操作都是基于hashMap的,接下来看LinkedHashSet,其底层实现都是基于linkedHashMap的. 二.linkedHas ...

  9. 集合之HashSet(含JDK1.8源码分析)

    一.前言 我们已经分析了List接口下的ArrayList和LinkedList,以及Map接口下的HashMap.LinkedHashMap.TreeMap,接下来看的是Set接口下HashSet和 ...

随机推荐

  1. 欢迎进入MyKTV前后台点歌系统展示

    一个项目,一分收获:一个项目,一些资源.Ktv项目也是一样的,所以我想分享我的收获,让你们获得你需要的资源. 一. 那MyKTV点歌系统具体的功能有哪些呢?我们就来看看吧! 1.MyKTV前台功能: ...

  2. SQLServer中的数据库备份和还原

    更多资源:http://denghejun.github.io 备份 SQLServer中的备份,这里是T-SQL的用法,具体示例代码如下,使用也相对简单,其中TestDatabase 是指所需备份的 ...

  3. Linux新手扫盲(转载)

    一. Linux特点 1.免费/开源: 2.支持多线程/多用户: 3.安全性好: 4.对内存和文件管理优越. Linux最小只需4M ——> 嵌入式开发 二. 文件目录 Linux系统所有软硬件 ...

  4. ABP理论学习之通知系统

    返回总目录 本篇目录 介绍 订阅通知 发布通知 用户通知管理者 实时通知 通知存储 通知定义 介绍 通知(Notification)用于告知用户系统中的特定事件.ABP提供了基于实时通知基础设施的发布 ...

  5. Intro to CSS 3D transforms

    原文地址:Intro to CSS 3D transforms,本文只是翻译了其中的一部分,省去了作者写文章的原因浏览器兼容部分(已经过时) Perspective 元素需要设置需要设置perspec ...

  6. Fig 应用编排

    Fig是Docker的应用编排工具,主要用来跟 Docker 一起来构建基于 Docker 的复杂应用,Fig 通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景. 说 ...

  7. Atitit.软件开发的几大规则,法则,与原则Principle v3

    Atitit.软件开发的几大规则,法则,与原则Principle  v31.1. 修改历史22. 设计模式六大原则22.1. 设计模式六大原则(1):单一职责原则22.2. 设计模式六大原则(2):里 ...

  8. kettle资源库配置

    资源库的作用:资源库是用来保存操作步骤和相关的日志,转换,JOB 等信息.用户通过图形界面创建的的转换任务可以保存在资源库中.资源库可以是各种常见的数据库,用户通过用户名/ 密码来访问资源库中的资源, ...

  9. 关于ie11 的开发者工具

    win7旗舰系统64为,更新ie11: 新安装了ie11浏览器,安装以后发现原来可以正常使用的开发者工具不能使用,提示 Imposible use F12 Developer Tools (Excep ...

  10. 【Win10应用开发】通过拖放来打开文件

    除了可以使用XXXFilePicker来浏览文件外,其实在UWP APP中,也可以向传统Windows窗口一样,通过拖放的方式来打开文件. 处理过程和WPF的原理差不多,毕竟都是一脉相承,于是,在学习 ...