部分摘抄自
阻塞队列
在JDK中,LinkedList或ArrayList就是队列。但是实际使用者并不多。
阻塞队列与我们平常接触的普通队列(LinkedList或ArrayList等)的最大不同点,在于阻塞队列支出阻塞添加和阻塞删除方法。
阻塞添加
所谓的阻塞添加是指当阻塞队列元素已满时,队列会阻塞加入元素的线程,直队列元素不满时才重新唤醒线程执行元素加入操作。阻塞删除
阻塞删除是指在队列元素为空时,删除队列元素的线程将被阻塞,直到队列不为空再执行删除操作(一般都会返回被删除的元素)
ArrayBlockingQueue
首先我们看一下如何使用
private final static ArrayBlockingQueue<Apple> mAbq= new ArrayBlockingQueue<>(1);
//生产者代码
Apple apple = new Apple();
mAbq.put(apple);
//消费者代码
Apple apple = mAbq.take();
System.out.println("消费Apple="+apple);
在构造方法中
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
//首先初始化本对象用的锁。如果设置为公平锁,则先lock的线程先获取到锁。
//公平锁会带来性能损失
//synchronized是非公平锁,ReentrantLock在默认的情况下也是非公平锁
lock = new ReentrantLock(fair);
//Conditon中的await()对应Object的wait();
//Condition中的signal()对应Object的notify();
//Condition中的signalAll()对应Object的notifyAll()。
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
首先,我们先看看阻塞的put和take方法。首先,是对
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();//加锁,可以被别的线程打断
try {
while (count == items.length)
notFull.await();//如果队列已经满了,则等待(调用await就等于释放了锁)
enqueue(e);
} finally {
lock.unlock();//多说一句,lock一定要记得在finally中unlock。和JVM支持的synchronized关键字不同,一旦发生异常而你没有unlock,则锁一直会被锁死。
}
}
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)//如果队列为空,则等待
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
出对入队都是将数组当成循环链表一样操作
private void enqueue(E x) {
// assert lock.getHoldCount() == 1;
// assert items[putIndex] == null;
final Object[] items = this.items;
items[putIndex] = x;
if (++putIndex == items.length)
putIndex = 0;
count++;
notEmpty.signal();
}
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
items[takeIndex] = null;
if (++takeIndex == items.length)
takeIndex = 0;
count--;
if (itrs != null)
itrs.elementDequeued();
notFull.signal();
return x;
}
LinkedBlockingQueue
唯一的区别是出兑入队换成了对链表的操作。
private void enqueue(Node<E> node) {
// assert putLock.isHeldByCurrentThread();
// assert last.next == null;
last = last.next = node;
}
private E dequeue() {
// assert takeLock.isHeldByCurrentThread();
// assert head.item == null;
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;
}
二者异同
1.队列大小有所不同,ArrayBlockingQueue是有界的初始化必须指定大小,而LinkedBlockingQueue可以是有界的也可以是无界的(Integer.MAX_VALUE),对于后者而言,当添加速度大于移除速度时,在无界的情况下,可能会造成内存溢出等问题。
2.数据存储容器不同,ArrayBlockingQueue采用的是数组作为数据存储容器,而LinkedBlockingQueue采用的则是以Node节点作为连接对象的链表。
3.由于ArrayBlockingQueue采用的是数组的存储容器,因此在插入或删除元素时不会产生或销毁任何额外的对象实例,而LinkedBlockingQueue则会生成一个额外的Node对象。这可能在长时间内需要高效并发地处理大批量数据的时,对于GC可能存在较大影响。
4.两者的实现队列添加或移除的锁不一样,ArrayBlockingQueue实现的队列中的锁是没有分离的,即添加操作和移除操作采用的同一个ReenterLock锁,而LinkedBlockingQueue实现的队列中的锁是分离的,其添加采用的是putLock,移除采用的则是takeLock,这样能大大提高队列的吞吐量,也意味着在高并发的情况下生产者和消费者可以并行地操作队列中的数据,以此来提高整个队列的并发性能。