1 . (实现但不完全是 JDK 实现。它用于维护元素之间的链接)LinkedBlockingQueue
LinkedList
LinkedList
static inner class Node
Constructor for LinkedBlockingQueue
public LinkedBlockingQueue(int capacity)
{
if (capacity < = 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node< E >(null); // Maintains a underlying linkedlist. ( Use when size is not known )
}
Node
用于维护链接的类
static class Node<E> {
E item;
Node<E> next;
Node(E x) { item = x; }
}
2 . ( 阵列实现 )ArrayBlockingQueue
的构造函数ArrayBlockingQueue
public ArrayBlockingQueue(int capacity, boolean fair)
{
if (capacity < = 0)
throw new IllegalArgumentException();
this.items = new Object[capacity]; // Maintains a underlying array
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
和 之间的最大区别从构造函数中可以明显看出,一个具有 的基础数据结构,而另一个具有 。ArrayBlockingQueue
LinkedBlockingQueue
Array
LinkedList
ArrayBlockingQueue
使用单锁双条件算法,是“双锁队列”算法的变体,它有2个锁2个条件(takeLock,putLock)LinkedBlockingQueue
到目前为止,我对这两个实现进行了比较 回到原始问题,在这个doug Lea谈论的DynamicArrayBlockingQueue的并发邮件列表中也提出了类似的问题,这是Dawid Kurzyniec提供的实现。