阻塞队列(4)LinkedBlockingDeque源码详解( 二 )
/** * Pointer to first node. * Invariant: (first == null /** * Pointer to last node. * Invariant: (first == null /** Number of items in the deque */private transient int count;/** Maximum number of items in the deque */private final int capacity;/** Main lock guarding all access */final ReentrantLock lock = new ReentrantLock();/** Condition for waiting takes */private final Condition notEmpty = lock.newCondition();/** Condition for waiting puts */private final Condition notFull = lock.newCondition();
构造器/*** Creates a {@code LinkedBlockingDeque} with a capacity of* {@link Integer#MAX_VALUE}.*/public LinkedBlockingDeque() {this(Integer.MAX_VALUE);}/*** Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity.** @param capacity the capacity of this deque* @throws IllegalArgumentException if {@code capacity} is less than 1*/public LinkedBlockingDeque(int capacity) {if (capacity <= 0) throw new IllegalArgumentException();this.capacity = capacity;}/*** Creates a {@code LinkedBlockingDeque} with a capacity of* {@link Integer#MAX_VALUE}, initially containing the elements of* the given collection, added in traversal order of the* collection's iterator.** @param c the collection of elements to initially contain* @throws NullPointerException if the specified collection or any*of its elements are null*/public LinkedBlockingDeque(Collection extends E> c) {this(Integer.MAX_VALUE);final ReentrantLock lock = this.lock;lock.lock(); // Never contended, but necessary for visibilitytry {for (E e : c) {if (e == null)throw new NullPointerException();if (!linkLast(new Node
吐槽默认竟然构造成最大整数 , 真是令人费解?
初始化线程安全保证使用了 ReentrantLock 可互斥锁 , 来保证线程安全性 。
看的出来 , 如果有元素为空 , 会直接抛出异常 。
添加元素/*** @throws IllegalStateException if this deque is full* @throws NullPointerException {@inheritDoc}*/public void addFirst(E e) {if (!offerFirst(e))throw new IllegalStateException("Deque full");}/*** @throws IllegalStateException if this deque is full* @throws NullPointerException{@inheritDoc}*/public void addLast(E e) {if (!offerLast(e))throw new IllegalStateException("Deque full");}/*** @throws NullPointerException {@inheritDoc}*/public boolean offerFirst(E e) {if (e == null) throw new NullPointerException();Node
linkFirst--tt-darkmode-color: #A3A3A3;">这里使用了 Condition 类来保证队列阻塞 。
- 二十三、Python队列实现多线程(下篇)
- 突击并发编程JUC系列-阻塞队列 BlockingQueue
- Redis+NodeJS实现能处理海量数据的异步任务队列系统
- 一次 ES-APM 导致的概率性大量线程阻塞问题排查
- 一篇文章搞懂同步、异步、阻塞、非阻塞、BIO、NIO和AIO
- python爬虫高级教程:多线程队列,生产消费模式爬虫
- Redis 异步消息队列与延时队列
- 阻塞队列—LinkedBlockingQueue源码分析
- springboot + rocketmq实现简单消息队列
- SynchronousQueue 同步队列入门使用&源码详解