ArrayDeque and LinkedBlockingDeque

2020-07-20 18:34发布

问题:

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.

回答1:

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