I am looking for a fast queue
implementation in Java. I see that LinkedList
implements the Queue
interface, but it will only be as fast as a LinkedList
right? Is there a way to have a queue that will be faster especially for add
(I only need poll
, add
and check for empty
).
Down the line I may also need a PriorityQueue
but not yet.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If performance of a linked list was really a problem, an alternative would be to implement a "circular queue" in an array, i.e. a queue where the start and end point move as entries are added and deleted. I can give more details if you care. When I was using languages that did not have a library of collections, this was how I always implemented queues because it was easier to write than a linked list and it was faster. But with built-in collections, the effort of writing and debugging my own collection for a special case is not worth the trouble 99% of the time: When it's already written, the fact that I could write it a different way faster than I could re-write it the way Java does is pretty much an irrelevant fact. And any performance gain is likely to be too small to be worth the trouble. I sub-type existing collections to get special behavior I need now and then, but I'm hard-pressed to think of the last time that I wrote one from scratch.
Start with really simplistic rotating Queue implementation with "C/C++ like" attitude and fixed size.
And then improve it.
You may want to have a look at http://java.dzone.com/articles/gaplist-%E2%80%93-lightning-fast-list which introduces
GapList
. This new list implementation combines the strengths of bothArrayList
andLinkedList
.It therefore implements the
Deque
interface, but can also be presized like the above mentionedArrayDeque
. In addition, you also get all the possibilities of theList
interface for free.Eyeballing the source code, LinkedList is O(1) for Queue.add, Queue.poll, and Queue.peek operations.
I hope that's fast enough.
If multiple threads are going to be accessing the queue then consider using an
ArrayBlockingQueue
. Otherwise take a look atArrayDeque
. From theArrayDeque
API:Specifically an array-based queue implementation reduces the need to resize the underlying array if the existing array has sufficient capacity, thus making additions to the queue generally faster than
LinkedList
. Be aware thatArrayBlockingQueue
is a bounded implementation whereasArrayDeque
will resize as required.The flip-side is that
LinkedList
will typically provide a much more compact representation, particularly in cases where your queue grows and shrinks by a large amount. For example, if you added 10,000,000 elements to anArrayDeque
and then removed 9,999,999 elements, the underlying array would still be of length 10,000,000 whereas aLinkedList
would not suffer from this problem.In reality, for single-threaded access to a non-blocking queue I tend to favour
LinkedList
. I imagine the performance differences are so negligable you wouldn't notice the difference anyway.