static class Node<E> {
E item;

Node<E> next;

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

    /** 用于消费队列的锁，如操作：take,poll 等等 */
private final ReentrantLock takeLock = new ReentrantLock();

/** 用于消费时判断不为空的条件对象 */
private final Condition notEmpty = takeLock.newCondition();

/** 添加元素的锁，如操作：put，offer等等 */
private final ReentrantLock putLock = new ReentrantLock();

/** 用于添加元素时判断队列未满的条件对象 */
private final Condition notFull = putLock.newCondition();

# 队列操作

## 添加元素

### 1.offer

    public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final AtomicInteger count = this.count;
//判断队列是否已满，若队列已满，则返回false
if (count.get() == capacity)
return false;
int c = -1;
Node<E> node = new Node<E>(e);
//获取放入元素的锁
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
//队列未满，这里是double检查，是获取锁后又检查了一次队列是否已满
if (count.get() < capacity) {
//添加元素到队尾
enqueue(node);
//获取添加元素前的队列大小，并将队列大小+1
c = count.getAndIncrement();
//判断队列未满
if (c + 1 < capacity)
//则通知下一个线程继续添加元素
notFull.signal();
}
} finally {
putLock.unlock();
}
//队列中有元素，则通知消费线程可以消费
if (c == 0)
signalNotEmpty();
return c >= 0;
}

### 2.put

    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 {
//这里是double检查，是获取锁后又检查了一次队列是否已满，如果队列已满，则线程进入阻塞，直到队列中有元素出队
while (count.get() == capacity) {
notFull.await();
}
//添加元素到队尾
enqueue(node);
c = count.getAndIncrement();
//判断队列未满
if (c + 1 < capacity)
//通知其它线程添加元素
notFull.signal();
} finally {
putLock.unlock();
}
//判断队列已有元素，则通知阻塞的消费线程进行消费
if (c == 0)
signalNotEmpty();
}

### put指定超时的操作

    //指定超时时间
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) {
//当超时时间过了后，则不再继续待，返回操作false
if (nanos <= 0)
return false;
//自旋锁，计算超时时间
nanos = notFull.awaitNanos(nanos);
}
enqueue(new Node<E>(e));
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
return true;
}

  public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}

## 出队操作

### 1.take

 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)
notEmpty.signal();
} finally {
takeLock.unlock();
}
//判断队列未满，通知入队的线程进行入队操作
if (c == capacity)
signalNotFull();
return x;
}

### take指定超时时间

    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) {
//超时时间已过，则返回null，不再阻塞等待
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}

### 2.poll

    public E poll() {
final AtomicInteger count = this.count;
//队列为空，则返回null
if (count.get() == 0)
return null;
E x = null;
int c = -1;
final ReentrantLock takeLock = this.takeLock;
//获取消费锁
takeLock.lock();
try {
//double检查，再次判断队列不为空，则返回队头元素
if (count.get() > 0) {
x = dequeue();
c = count.getAndDecrement();
//队列还有元素，则通知其它消费线程进行消费操作
if (c > 1)
notEmpty.signal();
}
} finally {
takeLock.unlock();
}
//判断出队后，队列未满，则通知入队线程进行入队操作
if (c == capacity)
signalNotFull();
return x;
}

### 3.remove

poll操作，若返回null，则返回空队列异常，其操作在父类AbstractQueue中

    public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}

### 4.peek

    public E peek() {
//队列为空，则返回null
if (count.get() == 0)
return null;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
//只是返回队头元素的引用，元素不出队
if (first == null)
return null;
else
return first.item;
} finally {
takeLock.unlock();
}
}

## 其它操作

### 1.remove(object)

    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)) {                    //移除该元素，将该元素的前一个和后一个关连起来
return true;
}
}            //未找到指定元素，返回false
return false;
} finally {
fullyUnlock();
}
}

### 2.contains(object)

    public boolean contains(Object o) {
if (o == null) return false;
fullyLock();
try {
for (Node<E> p = head.next; p != null; p = p.next)
if (o.equals(p.item))
return true;
return false;
} finally {
fullyUnlock();
}
}

### 3.toArray() ，toArray(T[] a)

    public Object[] toArray() {
fullyLock();
try {
int size = count.get();
Object[] a = new Object[size];
int k = 0;
for (Node<E> p = head.next; p != null; p = p.next)
a[k++] = p.item;
return a;
} finally {
fullyUnlock();
}
}
    public <T> T[] toArray(T[] a) {
fullyLock();
try {
int size = count.get();
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance
(a.getClass().getComponentType(), size);

int k = 0;
for (Node<E> p = head.next; p != null; p = p.next)
a[k++] = (T)p.item;
if (a.length > k)
a[k] = null;
return a;
} finally {
fullyUnlock();
}
}

### 4.clear()

    public void clear() {
fullyLock();
try {
for (Node<E> p, h = head; (p = h.next) != null; h = p) {
h.next = h;
p.item = null;
}
if (count.getAndSet(0) == capacity)
notFull.signal();
} finally {
fullyUnlock();
}
}

## 入队实现

    private void enqueue(Node<E> node) {        //将原队尾元素指向新元素node，新元素的node的next为null
last = last.next = node;
}

## 出队实现

    private E dequeue() {        //取队头元素
}