阻塞队列 java_Java中的阻塞队列-ArrayBlockingQueue(一)

/***加入成功返回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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值