阻塞队列
操作 | 抛出异常 | 有返回值,不抛异常 | 阻塞等待 | 超时等待 |
---|---|---|---|---|
插入 | add | offer | put | offer() |
删除 | remove | poll | take | poll() |
获取 | element | peek |
ArrayBlockingQueue
-
属性
//队列元素 final Object[] items; //下一个元素的下标(被take,poll,peek,remove) int takeIndex; //下一个元素的下标(被put,offer,add) int putIndex; //队列中的元素数 int count; //重入锁 final ReentrantLock lock; //队列不为空条件 private final Condition notEmpty; //队列不满条件 private final Condition notFull; transient Itrs itrs = null;
-
add()
public boolean add(E e) { return super.add(e); } //BlockQueue 中的方法 public boolean add(E e) { if (offer(e)) return true; else throw new IllegalStateException("Queue full"); } public boolean offer(E e) { //检查e是否为空 checkNotNull(e); final ReentrantLock lock = this.lock; lock.lock(); try { //当元素数量等于数组长度,则当前队列满 if (count == items.length) return false; else { //入队操作 enqueue(e); return true; } } finally { lock.unlock(); } }
-
put()
public void put(E e) throws InterruptedException { checkNotNull(e); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { //如果队列满,则阻塞入队锁 while (count == items.length) notFull.await(); //入队 enqueue(e); } finally { lock.unlock(); } }
-
enqueue()
private void enqueue(E x) { final Object[] items = this.items; items[putIndex] = x; //入队后,下标+1,(循环队列) if (++putIndex == items.length) putIndex = 0; count++; //唤醒notEmpty notEmpty.signal(); }
-
take()
public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { //当队列中元素为空,则阻塞出队锁 while (count == 0) notEmpty.await(); //出队 return dequeue(); } finally { lock.unlock(); } }
-
poll()
//出队操作 public E poll() { final ReentrantLock lock = this.lock; lock.lock(); try { return (count == 0) ? null : dequeue(); } finally { lock.unlock(); } }
-
dequeue()
private E dequeue() { final Object[] items = this.items; //获取需要出队的元素 E x = (E) items[takeIndex]; items[takeIndex] = null; //出队后下标+1,(循环队列) if (++takeIndex == items.length) takeIndex = 0; count--; if (itrs != null) itrs.elementDequeued(); //唤醒入队锁 notFull.signal(); return x; }
-
peek()
//只获取当前take位置的元素,不出队 public E peek() { final ReentrantLock lock = this.lock; lock.lock(); try { return itemAt(takeIndex); // null when queue is empty } finally { lock.unlock(); } }
LinkedBlockingDeque
-
属性
//头结点 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.newCondition(); //不满条件 private final Condition notFull = lock.newCondition();
-
offer()
//头插法 public boolean offerFirst(E e) { //e==null,则当前链表未初始化 if (e == null) throw new NullPointerException(); Node<E> node = new Node<E>(e); final ReentrantLock lock = this.lock; lock.lock(); try { //插入到链表头部 return linkFirst(node); } finally { lock.unlock(); } } //尾插法 public boolean offerLast(E e) { 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(); } }
-
put
public void putFirst(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 (!linkFirst(node)) notFull.await(); } finally { lock.unlock(); } }
-
poll
public E pollFirst() { final ReentrantLock lock = this.lock; lock.lock(); try { return unlinkFirst(); } finally { lock.unlock(); } }
-
take
public E takeFirst() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lock(); try { E x; while ( (x = unlinkFirst()) == null) notEmpty.await(); return x; } finally { lock.unlock(); } }