/**
* 定长队列,新元素会把旧元素挤掉
* Created by wanghongji on 17-11-24.
*/
public class CircleArrayBlockingQueue<E> extends ArrayBlockingQueue {
private int capacity = 16;
final ReentrantLock lock;
public CircleArrayBlockingQueue(int capacity) {
super(capacity);
this.capacity = capacity;
lock = new ReentrantLock(false);
}
/**
* 队尾增加一个元素。如果队伍满了,则先删掉第一个元素
* @param value
* @return
*/
public boolean addOne(E value) {
if (value == null) {
throw new IllegalArgumentException();
}
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (this.size() == this.capacity) {
this.poll();
}
return this.add(value);
} finally {
lock.unlock();
}
}
}
定长队列,新元素会把旧元素挤掉
最新推荐文章于 2023-10-08 11:21:28 发布