Java:ArrayBlockingQueue与LinkedBlockingQueue [英] Java: ArrayBlockingQueue vs. LinkedBlockingQueue

查看:171
本文介绍了Java:ArrayBlockingQueue与LinkedBlockingQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为,在大多数情况下,ArrayBlockingQueue的性能将优于LinkedBlockingQueue.但是,当数组中始终有足够的空间时就是这种情况……如果空间变满,它是否会表现得这么好不是很可预测的,因为它将阻塞试图将数据推入队列的线程.

I think that, in most cases, the ArrayBlockingQueue will perform better than the LinkedBlockingQueue. However, that is the case when there is always enough room in the array... If it gets full, it's not very predictable whether it will perform so well, since it will block the thread that's trying to push data into the queue...

所以,我的问题是:BlockingQueue有中间立场吗?是说ArrayListBlockingQueue还是BucketListBlockingQueue?类似于数组列表,以便队列可以动态增加容量,同时仍可以从使用数组最终存储数据中获得合理的好处?

So, my question is: Is there any middle-ground implementation of BlockingQueue? Say, an ArrayListBlockingQueue or a BucketListBlockingQueue? Something like a list of arrays, so that the queue can increase in capacity dynamically, while still having a reasonable benefit from using array to ultimately store data?

推荐答案

1. LinkedBlockingQueue(LinkedList实现,但LinkedList的JDK实现并不完全是JDK实现,它使用静态内部类Node 来维护元素之间的链接)

1 . LinkedBlockingQueue ( LinkedList Implementation but Not Exactly JDK Implementation of LinkedList It uses static inner class Node to maintain Links between elements )

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 )
}

用于维护链接的节点类

static class Node<E> {
    E item;
    Node<E> next;
    Node(E x) { item = x; }
}

2. ArrayBlockingQueue(数组实现)

2 . ArrayBlockingQueue ( Array Implementation )

ArrayBlockingQueue的构造函数

Constructor for 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 .

IMHO Biggest Difference between ArrayBlockingQueue and LinkedBlockingQueue is clear from constructor one has underlying data structure Array and other linkedList.

ArrayBlockingQueue使用单锁双重条件算法和LinkedBlockingQueue是两个锁队列"算法的变体,它具有2个锁2个条件(takeLock,putLock)

ArrayBlockingQueue uses single-lock double condition algorithm and LinkedBlockingQueue is variant of the "two lock queue" algorithm and it has 2 locks 2 conditions ( takeLock , putLock)

到目前为止,我对这两种实现进行了比较.回到原始问题,在由Dawid Kurzyniec提供的实现

Till now I gave comparison between these 2 implementations Coming back to original question , Similar question was asked in concurrency mailing list in this doug Lea talks about DynamicArrayBlockingQueue which is implementation provided by Dawid Kurzyniec.

这篇关于Java:ArrayBlockingQueue与LinkedBlockingQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆