问题描述
只是想知道为什么他们做了 LinkedBlockingDeque
,而同时非并发对应的是 ArrayDeque
调整大小的数组。
LinkedBlockingQueue
使用一组节点,如 LinkedList
(即使没有实现 List
)。
我知道可能使用 ArrayBlockingQueue
但是如果想使用 ArrayBlockingDeque
,怎么办?
为什么没有这样的选项?
提前感谢。
这可能不是一个正确的问题wrt stackoverflow。
但是我想对这些实现说些什么。
- >第一件事我们需要回答为什么我们给某个接口的不同实现。
假设我们有一个接口A,有两个实现,让我们说B和C.
现在假设这些实现通过它们的实现提供相同的功能。
但是B比C更好的性能。
然后我们应该除去两个原因除去实现。
1。向后兼容性 - 标记为已弃用。
2.有一个特定的场景,我们不能使用B实现。
例如:
HashMap和LinkedHashMap - >
如果我需要订购密钥我会使用LinkedHashMap否则我会使用HashMap(一点性能增益)。
ArrayBlockingQueue vs LinkedBlockingQueue
如果我需要有界队列我会使用ArrayBlockingQueue否则我会使用LinkedBlockingQueue。
为什么在LinkedBlockingDeque存在时没有ArrayBlockingDeque
。首先让我们看看为什么ArrayDeque存在。
从ArrayDeque的Java文档。
- 此类可能比
- {@ link堆栈}作为堆栈使用时,并且比{@link LinkedList}
-
用作队列时更快。
另请注意ArrayDeque没有容量限制。
它还有两个头和尾的指针,用于LinkedList实现中。
因此,如果有ArrayBlockingDeque 。
1。将不会有容量限制,我们通常
从ArrayBlockingQueue获取。
2.与LinkedBlockingDeque中的相同,将有警卫访问尾和头指针
,因此没有显着的性能
获得超过LinkedBlockingDeque。
所以总结出没有ArrayBlockingDeque实现,因为没有什么
这个实现可以提供额外overBlockingDeque。如果你可以证明有
的东西那么是的,实现需要有:) :)
Just wondering why they made a LinkedBlockingDeque
while the same non concurrent counterpart is an ArrayDeque
which is backed on a resizable array.
LinkedBlockingQueue
use a set of nodes like a LinkedList
(even though not implementing List
).
I am aware of the possibility to use an ArrayBlockingQueue
but what if one wanted to use an ArrayBlockingDeque
? Why is there not such an option?
Thanks in advance.
This May not be a proper question w.r.t stackoverflow.But i would Like to say something about these implementations.
-> First thing We need to answer why we give Different implementations for a certain Interface. Suppose we have a interface A and There are two implementations of that Let say B and C. Now suppose these implementations offer same functionality through their implementation. But B has a better performance than C. Then we should remove the Implementation except for two reason
1. Backward Compatibility - marking as deprecated.
2. There is a specific scenario where we cannot use B implementation.
For example:
HashMap and LinkedHashMap -> If i need ordered Keys i will use LinkedHashMap otherwise I will Use HashMap (for a little performance gain).
ArrayBlockingQueue vs LinkedBlockingQueue if i need Bounded Queue i will use ArrayBlockingQueue otherwise i will use LinkedBlockingQueue.
Now Your question Why there is no ArrayBlockingDeque while LinkedBlockingDeque exist.
First Let see why ArrayDeque exist.
From Java docs of ArrayDeque.
- This class is likely to be faster than
- {@link Stack} when used as a stack, and faster than {@link LinkedList}
when used as a queue.
Also note ArrayDeque doesn’t have Capacity Restriction. Also it has Two pointers for head and tail as use use in LinkedList implementation.
Therefore if there would have been ArrayBlockingDeque.
1. There would have been no Capacity Restriction, which we normally
get from ArrayBlockingQueue.
2. There would have guards to access to tail and head pointers
same as in LinkedBlockingDeque and therefore no significant performance
gain over LinkedBlockingDeque.
So to conclude there is no ArrayBlockingDeque implementation because there in nothing extra this implementation could provide over LinkedBlockingDeque. If you can prove there is something then yes, implementation need to be there :)
这篇关于ArrayDeque和LinkedBlockingDeque的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!