LinkedBlockingDeque

双向链表

支持FIFO FILO,可以从队列的头和尾同时插入或删除操作

可选容量(防止过度膨胀),默认容量等于Integer.MAX_VALUE

image

  1. LinkedBlockingDeque继承于AbstractQueue,它本质上是一个支持FIFO和FILO的双向的队列。

  2. LinkedBlockingDeque实现了BlockingDeque接口,它支持多线程并发。当多线程竞争同一个资源时,某线程获取到该资源之后,其它线程需要阻塞等待。

  3. LinkedBlockingDeque是通过双向链表实现的。
    3.1 first是双向链表的表头。

3.2 last是双向链表的表尾。

3.3 count是LinkedBlockingDeque的实际大小,即双向链表中当前节点个数。

3.4 capacity是LinkedBlockingDeque的容量,它是在创建LinkedBlockingDeque时指定的。

3.5 lock是控制对LinkedBlockingDeque的互斥锁,当多个线程竞争同时访问LinkedBlockingDeque时,某线程获取到了互斥锁lock,其它线程则需要阻塞等待,直到该线程释放lock,其它线程才有机会获取lock从而获取cpu执行权。

3.6 notEmpty和notFull分别是“非空条件”和“未满条件”。通过它们能够更加细腻进行并发控制。

创建

public LinkedBlockingDeque(int capacity) {
    if (capacity <= 0) throw new IllegalArgumentException();
    this.capacity = capacity;
}

相关数据定义

// “双向队列”的表头
transient Node<E> first;
// “双向队列”的表尾
transient Node<E> last;
// 节点数量
private transient int count;
// 容量
private final int capacity;
// 互斥锁 , 互斥锁对应的“非空条件notEmpty”, 互斥锁对应的“未满条件notFull”
final ReentrantLock lock = new ReentrantLock();
private final Condition notEmpty = lock.newCondition();
private final Condition notFull = lock.newCondition();

节点定义

static final class Node<E> {
    E item;       // 数据
    Node<E> prev; // 前一节点
    Node<E> next; // 后一节点

    Node(E x) { item = x; }
}

添加

public boolean offer(E e){
    return offerLast(e);
}

offerLast

public boolean offerLast(){
      if (e == null) throw new NullPointerException();
    // 新建节点
    Node<E> node = new Node<E>(e);
    final ReentrantLock lock = this.lock;
    // 获取锁
    lock.lock();
    try {
        // 将“新节点”添加到双向链表的末尾
        return linkLast(node);
    } finally {
        // 释放锁
        lock.unlock();
    }
}

linkLast()


private boolean linkLast(Node<E> node) {
    // 如果“双向链表的节点数量” > “容量”,则返回false,表示插入失败。
    if (count >= capacity)
        return false;
    // 将“node添加到链表末尾”,并设置node为新的尾节点
    Node<E> l = last;
    node.prev = l;
    last = node;
    if (first == null)
        first = node;
    else
        l.next = node;
    // 将“节点数量”+1
    ++count;
    // 插入节点之后,唤醒notEmpty上的等待线程。
    notEmpty.signal();
    return true;
}

插入节点之后,唤醒notEmpty上的等待线程

删除


public E takeFirst() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    // 获取锁
    lock.lock();
    try {
        E x;
        // 若“队列为空”,则一直等待。否则,通过unlinkFirst()删除第一个节点。
        while ( (x = unlinkFirst()) == null)
            notEmpty.await();
        return x;
    } finally {
        // 释放锁
        lock.unlock();
    }
}

unlinkFirst()

private E unlinkFirst() {
    // assert lock.isHeldByCurrentThread();
    Node<E> f = first;
    if (f == null)
        return null;
    // 删除并更新“第一个节点”
    Node<E> n = f.next;
    E item = f.item;
    f.item = null;
    f.next = f; // help GC
    first = n;
    if (n == null)
        last = null;
    else
        n.prev = null;
    // 将“节点数量”-1
    --count;
    // 删除节点之后,唤醒notFull上的等待线程。
    notFull.signal();
    return item;
}

猜你喜欢

转载自blog.csdn.net/weixin_40862011/article/details/84728467