美文网首页
11.BlockingDeque

11.BlockingDeque

作者: 段段小胖砸 | 来源:发表于2021-09-07 11:28 被阅读0次

    BlockingDeque定义了一个阻塞的双端队列接口。

    public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { 
        void putFirst(E e) throws InterruptedException; 
        void putLast(E e) throws InterruptedException; 
        E takeFirst() throws InterruptedException; 
        E takeLast() throws InterruptedException; 
        // ... 
    }
    

    该接口继承了BlockingQueue接口,同时增加了对应的双端队列操作接口。该接口只有一个实现,就是LinkedBlockingDeque。
    核心数据结构是一个双向链表

    LinkedBlockingDeque

    public class LinkedBlockingDeque<E> extends AbstractQueue<E> implements BlockingDeque<E>, java.io.Serializable {
         static final class Node<E> {
             E item; 
            Node<E> prev; // 双向链表的Node 
            Node<E> next;
            Node(E x) { item = x; 
            } 
        }
        transient Node<E> first; // 队列的头和尾 
        transient Node<E> last; 
        private transient int count; // 元素个数 
        private final int capacity; // 容量 
        // 一把锁+两个条件 
        final ReentrantLock lock = new ReentrantLock(); 
        private final Condition notEmpty = lock.netCondition(); 
        private final Condition notFull = lock.newCondition();
    }
    

    相关文章

      网友评论

          本文标题:11.BlockingDeque

          本文链接:https://www.haomeiwen.com/subject/igxywltx.html