ArrayDeque 和 LinkedBlockingDeque
只是想知道为什么他们制作了一个 LinkedBlockingDeque
而相同的非并发对应物是一个 ArrayDeque
它支持一个可调整大小的数组.
Just wondering why they made a LinkedBlockingDeque
while the same non concurrent counterpart is an ArrayDeque
which is backed on a resizable array.
LinkedBlockingQueue
使用一组节点,例如 LinkedList
(即使没有实现 List
).
LinkedBlockingQueue
use a set of nodes like a LinkedList
(even though not implementing List
).
我知道使用 ArrayBlockingQueue
的可能性,但如果想使用 ArrayBlockingDeque
怎么办?为什么没有这个选项?
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?
提前致谢.
推荐答案
这可能不是一个正确的问题 w.r.t stackoverflow.但我想谈谈这些实现.
This May not be a proper question w.r.t stackoverflow. But i would Like to say something about these implementations.
-> 首先我们需要回答为什么我们为某个接口提供不同的实现.假设我们有一个接口 A 并且有两个实现让我们说 B 和 C.现在假设这些实现通过它们的实现提供相同的功能.但是 B 的性能比 C 好.然后我们应该删除实现,除了两个原因
-> 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.
例如:
HashMap 和 LinkedHashMap ->如果我需要有序键,我将使用 LinkedHashMap,否则我将使用 HashMap(以获得一点性能提升).
HashMap and LinkedHashMap -> If i need ordered Keys i will use LinkedHashMap otherwise I will Use HashMap (for a little performance gain).
ArrayBlockingQueue 与 LinkedBlockingQueue如果我需要有界队列,我将使用 ArrayBlockingQueue,否则我将使用 LinkedBlockingQueue.
ArrayBlockingQueue vs LinkedBlockingQueue if i need Bounded Queue i will use ArrayBlockingQueue otherwise i will use LinkedBlockingQueue.
现在你的问题为什么存在 LinkedBlockingDeque 时没有 ArrayBlockingDeque.
首先让我们看看为什么存在ArrayDeque.
First Let see why ArrayDeque exist.
来自 ArrayDeque 的 Java 文档.
From Java docs of ArrayDeque.
- 这个类可能比 快
- {@link Stack} 用作堆栈时,比 {@link LinkedList} 快
用作队列时.
- 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.
还要注意 ArrayDeque 没有容量限制.它还有两个用于头和尾的指针,用于 LinkedList 实现.
Also note ArrayDeque doesn’t have Capacity Restriction. Also it has Two pointers for head and tail as use use in LinkedList implementation.
因此如果会有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.
因此得出的结论是没有 ArrayBlockingDeque 实现,因为什么都没有额外的这个实现可以提供超过 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 :)
相关文章