030_java.util.concurrent.LinkedBlockingDeque

继承结构

image.png
LinkedBlockingDeque 继承AbstractQueue,实现接口BlockingDeque,而BlockingDeque又继承接口BlockingQueue,BlockingDeque接口提供了一系列的以First和Last结尾的方法,如addFirst、addLast、peekFirst、peekLast。

重要字段

// 双向链表的表头
transient Node<E> first;

// 双向链表的表尾
transient Node<E> last;

// 大小,双向链表中当前节点个数
private transient int count;

// 容量,在创建LinkedBlockingDeque时指定的
private final int capacity;

final ReentrantLock lock = new ReentrantLock();

private final Condition notEmpty = lock.newCondition();

private final Condition notFull = lock.newCondition();

通过上面的Lock可以看出,LinkedBlockingDeque底层实现机制与LinkedBlockingQueue一样,依然是通过互斥锁ReentrantLock 来实现,notEmpty 、notFull 两个Condition做协调生产者、消费者问题。
Node类的定义如下,增加节点前后指针,构成链表。

static final class Node<E> {
    E item;

    Node<E> prev;

    Node<E> next;

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

重要方法

LinkedBlockingDeque 的add、put、offer、take、peek、poll系列方法都是通过调用XXXFirst,XXXLast方法。所以这里就仅以putFirst、putLast、pollFirst、pollLast分析下。

入队方法

// 将指定的元素插入此双端队列的开头,必要时将一直等待可用空间。
public void putFirst(E e) throws InterruptedException {
    // check null
    if (e == null) throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    // 获取锁
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        // 先获取锁,然后调用linkFirst方法入列,最后释放锁。
        while (!linkFirst(node))
        // 在notFull条件上等待,直到被唤醒或中断
        notFull.await();
    } finally {
        // 释放锁
        lock.unlock();
    }
}

private boolean linkFirst(Node<E> node) {
    // 超出容量
    if (count >= capacity)
        return false;

    // 首节点
    Node<E> f = first;
    // 新节点的next指向原first
    node.next = f;
    // 设置node为新的first
    first = node;

    // 没有尾节点,设置node为尾节点
    if (last == null)
        last = node;
    // 有尾节点,那就将之前first的pre指向新增node
    else
        f.prev = node;
    ++count;
    // 唤醒notEmpty
    notEmpty.signal();
    return true;
}

// 将指定的元素插入此双端队列的末尾,必要时将一直等待可用空间。
public void putLast(E e) throws InterruptedException {
    if (e == null) throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        while (!linkLast(node))
            notFull.await();
    } finally {
        lock.unlock();
    }
}

private boolean linkLast(Node<E> node) {
    if (count >= capacity)
        return false;
    // 尾节点
    Node<E> l = last;

    // 将Node的前驱指向原本的last
    node.prev = l;

    // 将node设置为last
    last = node;
    // 首节点为null,则设置node为first
    if (first == null)
        first = node;
    else
    //非null,说明之前的last有值,就将之前的last的next指向node
        l.next = node;
    ++count;
    notEmpty.signal();
    return true;
}

出队方法

// 获取并移除此双端队列的第一个元素, 如果此双端队列为空,则返回 null。
public E pollFirst() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        // 调用unlinkFirst移除队列首元素
        return unlinkFirst();
    } finally {
        lock.unlock();
    }
}

private E unlinkFirst() {
    // 首节点
    Node<E> f = first;

    // 空队列,直接返回null
    if (f == null)
        return null;

    // first.next
    Node<E> n = f.next;

    // 节点item
    E item = f.item;

    // 移除掉first ==> first = first.next
    f.item = null;
    f.next = f; // help GC
    first = n;

    // 移除后为空队列,仅有一个节点
    if (n == null)
        last = null;
    else
        // n的pre原来指向之前的first,现在n变为first了,pre指向null
        n.prev = null;
    --count;
    notFull.signal();
    return item;
}

// 获取并移除此双端队列的最后一个元素;如果此双端队列为空,则返回 null。
public E pollLast() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        // 调用unlinkLast移除尾结点
        return unlinkLast();
    } finally {
        lock.unlock();
    }
}
private E unlinkLast() {
    // assert lock.isHeldByCurrentThread();
    Node<E> l = last;
    if (l == null)
        return null;
    Node<E> p = l.prev;
    E item = l.item;
    l.item = null;
    l.prev = l; // help GC
    last = p;
    if (p == null)
        first = null;
    else
        p.next = null;
    --count;
    notFull.signal();
    return item;
}

总结

LinkedBlockingDeque可以说是在LinkedBlockingQueue的基础上增加了头尾的操作能力,其大部分方法都是通过linkFirst、linkLast、unlinkFirst、unlinkLast这四个方法来实现的。其他都蛮好理解的,问题不大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值