/***加入成功返回true,否则返回false
**/public booleanoffer(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();
}
}/*** 如果队列已满的话,就会等待*/public void put(E e) throwsInterruptedException {
checkNotNull(e);final ReentrantLock lock = this.lock;
lock.lockInterruptibly();//和lock()方法的区别是让它在阻塞时也可抛出异常跳出
try{while (count ==items.length)
notFull.await();//这里就是阻塞了,要注意。如果运行到这里,那么它会释放上面的锁,一直等到notify
enqueue(e);
}finally{
lock.unlock();
}
}/*** 带有超时时间的插入方法,unit表示是按秒、分、时哪一种*/public boolean offer(E e, longtimeout, TimeUnit unit)throwsInterruptedException {
checkNotNull(e);long nanos =unit.toNanos(timeout);final ReentrantLock lock = this.lock;
lock.lockInterruptibly();try{while (count ==items.length) {if (nanos <= 0)return false;
nanos= notFull.awaitNanos(nanos);//带有超时等待的阻塞方法
}
enqueue(e);//入队
return true;
}finally{
lock.unlock();
}
}//实现的方法,如果当前队列为空,返回null
publicE poll() {final ReentrantLock lock = this.lock;
lock.lock();try{return (count == 0) ? null: dequeue();
}finally{
lock.unlock();
}
}//实现的方法,如果当前队列为空,一直阻塞
public E take() throwsInterruptedException {final ReentrantLock lock = this.lock;
lock.lockInterruptibly();try{while (count == 0)
notEmpty.await();//队列为空,阻塞方法
returndequeue();
}finally{
lock.unlock();
}
}//带有超时时间的取元素方法,否则返回Null
public E poll(long timeout, TimeUnit unit) throwsInterruptedException {long nanos =unit.toNanos(timeout);final ReentrantLock lock = this.lock;
lock.lockInterruptibly();try{while (count == 0) {if (nanos <= 0)return null;
nanos= notEmpty.awaitNanos(nanos);//超时等待
}return dequeue();//取得元素
} finally{
lock.unlock();
}
}//只是看一个队列最前面的元素,取出是不删除队列中的原来元素。队列为空时返回null
publicE peek() {final ReentrantLock lock = this.lock;
lock.lock();try{return itemAt(takeIndex); //队列为空时返回null
} finally{
lock.unlock();
}
}/*** 返回队列当前元素个数
**/public intsize() {final ReentrantLock lock = this.lock;
lock.lock();try{returncount;
}finally{
lock.unlock();
}
}/*** 返回当前队列再放入多少个元素就满队*/public intremainingCapacity() {final ReentrantLock lock = this.lock;
lock.lock();try{return items.length -count;
}finally{
lock.unlock();
}
}/*** 从队列中删除一个元素的方法。删除成功返回true,否则返回false*/public booleanremove(Object o) {if (o == null) return false;final Object[] items = this.items;final ReentrantLock lock = this.lock;
lock.lock();try{if (count > 0) {final int putIndex = this.putIndex;int i =takeIndex;do{if(o.equals(items[i])) {
removeAt(i);//真正删除的方法
return true;
}if (++i ==items.length)
i= 0;
}while (i != putIndex);//一直不断的循环取出来做判断
}return false;
}finally{
lock.unlock();
}
}/*** 是否包含一个元素*/public booleancontains(Object o) {if (o == null) return false;final Object[] items = this.items;final ReentrantLock lock = this.lock;
lock.lock();try{if (count > 0) {final int putIndex = this.putIndex;int i =takeIndex;do{if(o.equals(items[i]))return true;if (++i ==items.length)
i= 0;
}while (i !=putIndex);
}return false;
}finally{
lock.unlock();
}
}/*** 清空队列
**/public voidclear() {final Object[] items = this.items;final ReentrantLock lock = this.lock;
lock.lock();try{int k =count;if (k > 0) {final int putIndex = this.putIndex;int i =takeIndex;do{
items[i]= null;if (++i ==items.length)
i= 0;
}while (i !=putIndex);
takeIndex=putIndex;
count= 0;if (itrs != null)
itrs.queueIsEmpty();for (; k > 0 && lock.hasWaiters(notFull); k--)
notFull.signal();
}
}finally{
lock.unlock();
}
}/*** 取出所有元素到集合*/public int drainTo(Collection super E>c) {returndrainTo(c, Integer.MAX_VALUE);
}/*** 取出所有元素到集合*/public int drainTo(Collection super E> c, intmaxElements) {
checkNotNull(c);if (c == this)throw newIllegalArgumentException();if (maxElements <= 0)return 0;final Object[] items = this.items;final ReentrantLock lock = this.lock;
lock.lock();try{int n =Math.min(maxElements, count);int take =takeIndex;int i = 0;try{while (i
@SuppressWarnings("unchecked")
E x=(E) items[take];
c.add(x);
items[take]= null;if (++take ==items.length)
take= 0;
i++;
}returnn;
}finally{//Restore invariants even if c.add() threw
if (i > 0) {
count-=i;
takeIndex=take;if (itrs != null) {if (count == 0)
itrs.queueIsEmpty();else if (i >take)
itrs.takeIndexWrapped();
}for (; i > 0 && lock.hasWaiters(notFull); i--)
notFull.signal();
}
}
}finally{
lock.unlock();
}
}