private final int capacity; // 容量(最大节点个数),默认Integer.MAX_VALUE private final AtomicInteger count = new AtomicInteger(); // 当前节点个数 transient Node<E> head; // 队列头结点 private transient Node<E> last; // 队列尾节点 private final ReentrantLock takeLock = new ReentrantLock(); // 头结点锁(取) private final Condition notEmpty = takeLock.newCondition(); // 等待队列非空 private final ReentrantLock putLock = new ReentrantLock(); // 尾结点锁(置) private final Condition notFull = putLock.newCondition(); // 等待队列非满 public LinkedBlockingQueue() { this(Integer.MAX_VALUE); } public LinkedBlockingQueue(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; last = head = new Node<E>(null); // 头节点不存放元素 } public LinkedBlockingQueue(Collection<? extends E> c) { this(Integer.MAX_VALUE); final ReentrantLock putLock = this.putLock; putLock.lock(); // 尾结点加锁 try { int n = 0; for (E e : c) { if (e == null) throw new NullPointerException(); if (n == capacity) // 队列满 throw new IllegalStateException("Queue full"); enqueue(new Node<E>(e)); ++n; } count.set(n); } finally { putLock.unlock(); // 释放锁 } }
static class Node<E> { // 节点 E item; Node<E> next; Node(E x) { item = x; } } private void enqueue(Node<E> node) { last = last.next = node; } private void signalNotEmpty() { // 在获取takeLock状态下,唤醒等待get的线程 final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { notEmpty.signal(); } finally { takeLock.unlock(); } } public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); // 加锁 try { while (count.get() == capacity) { // 队列满 notFull.await(); } enqueue(node); c = count.getAndIncrement(); // 必须是原子操做 if (c + 1 < capacity) // put后,队列非满 notFull.signal(); // 唤醒后续等待put的线程(若是存在) } finally { putLock.unlock(); // 释放锁 } if (c == 0) // put前,队列空 signalNotEmpty(); // 唤醒首个等待get的节点(若是存在) } public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); long nanos = unit.toNanos(timeout); int c = -1; final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); // 加锁 try { while (count.get() == capacity) { // 队列满 if (nanos <= 0) // 超时(awaitNanos可能已被signal,但在SyncQueue中排队等锁时超时,见ReentrantLock) return false; nanos = notFull.awaitNanos(nanos); // 等待(在nanos时间内) } enqueue(new Node<E>(e)); c = count.getAndIncrement(); if (c + 1 < capacity) // offer后,队列非满 notFull.signal(); // 唤醒后续等待offer的线程(若是存在) } finally { putLock.unlock(); // 释放锁 } if (c == 0) // offer前,队列为空 signalNotEmpty(); // 唤醒首个等待get的线程(若是存在) return true; }
private E dequeue() { Node<E> h = head; Node<E> first = h.next; h.next = h; // help GC head = first; E x = first.item; first.item = null; return x; } private void signalNotFull() { // 在获取putLock锁状态下,唤醒等待put的线程 final ReentrantLock putLock = this.putLock; putLock.lock(); try { notFull.signal(); } finally { putLock.unlock(); } } public E take() throws InterruptedException { E x; int c = -1; final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); // 加锁 try { while (count.get() == 0) { // 队列空 notEmpty.await(); // 等待 } x = dequeue(); c = count.getAndDecrement(); if (c > 1) // get后,队列非空 notEmpty.signal(); // 唤醒后续等待get线程(若是存在) } finally { takeLock.unlock(); // 释放锁 } if (c == capacity) // get前,队列已满 signalNotFull(); // 唤醒首个等待put的线程(若是存在) return x; } public E poll(long timeout, TimeUnit unit) throws InterruptedException { E x = null; int c = -1; long nanos = unit.toNanos(timeout); final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); // 加锁 try { while (count.get() == 0) { // 队列空 if (nanos <= 0) // 超时 return null; nanos = notEmpty.awaitNanos(nanos); // 等待poll } x = dequeue(); c = count.getAndDecrement(); if (c > 1) // poll后,队列非空 notEmpty.signal(); // 唤醒后续等待poll的线程(若是存在) } finally { takeLock.unlock(); // 释放锁 } if (c == capacity) // poll前,队列已满 signalNotFull(); // 唤醒首个等待put的线程(若是存在) return x; }
void fullyLock() { // 加全锁(remove、contains、toArray、clear、Itr) putLock.lock(); takeLock.lock(); } void fullyUnlock() { // 释放全锁 takeLock.unlock(); putLock.unlock(); } void unlink(Node<E> p, Node<E> trail) { p.item = null; trail.next = p.next; if (last == p) last = trail; if (count.getAndDecrement() == capacity) // 移除节点前,队列已满 notFull.signal(); // 唤醒等待put/offer的线程 } public boolean remove(Object o) { // 删除节点 if (o == null) return false; fullyLock(); // 加全锁 try { for (Node<E> trail = head, p = trail.next; p != null; trail = p, p = p.next) { if (o.equals(p.item)) { unlink(p, trail); return true; } } return false; } finally { fullyUnlock(); // 释放全锁 } }