- What scenarios is it better to use an ArrayBlockingQueue and when is it better to use a LinkedBlockingQueue?
- If LinkedBlockingQueue default capacity is equal to MAX Integer, is it really helpful to use it as BlockingQueue with default capacity?
相关问题
- 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
ArrayBlockingQueue<E>
andLinkedBlockingQueue<E>
are common implementations of theBlockingQueue<E>
interface.ArrayBlockingQueue
is backed byarray
andQueue
impose orders asFIFO
. head of the queue is the oldest element in terms of time and tail of the queue is youngest element.ArrayBlockingQueue
is also fixed size bounded buffer on the other handLinkedBlockingQueue
is an optionally bounded queue built on top of Linked nodes.The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion because if capacity is unspecified, than it is equal to
Integer.MAX_VALUE
.Read more From here.
Benchmark: http://www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html
Adding an element to ArrayBlockingQueue is supposed to be faster since it means only setting a reference to an element of the backing Object array, while adding an element to LinkedBlockingQueue means creating a Node and setting its item, prev and next fields. Besides, when we remove an element from LinkedBlockingQueue the removed Node becomes garbage which may influence app's performance.
As for memory consumption ArrayBlockingQueue always holds an Object array with full capacity even when empty. On the other hand one element in LinkedBlockingQueue is a Node with an Object with 3 Object fields.
ArrayBlockingQueue :
ArrayBlockingQueue is a bounded, blocking queue that stores the elements internally in an array. That it is bounded means that it cannot store unlimited amounts of elements. There is an upper bound on the number of elements it can store at the same time. You set the upper bound at instantiation time, and after that it cannot be changed.
LinkedBlockingQueue
The LinkedBlockingQueue keeps the elements internally in a linked structure (linked nodes). This linked structure can optionally have an upper bound if desired. If no upper bound is specified, Integer.MAX_VALUE is used as the upper bound.
Similarity
The ArrayBlockingQueue/LinkedBlockingQueue stores the elements internally in FIFO (First In, First Out) order. The head of the queue is the element which has been in queue the longest time, and the tail of the queue is the element which has been in the queue the shortest time.
Differences
Two Lock Queue algorithm is being used by LinkedBlockingQueue Implementation.Thus LinkedBlockingQueue's take and put can work concurrently, but this is not the case with ArrayBlockingQueue. The reason for using a single lock in ArrayBlockingQueue is ,ArrayBlockingQueue has to avoid overwriting entries so that it needs to know where the start and the end is. A LinkedBlockQueue doesn't need to know this as it lets the GC worry about cleaning up Nodes in the queue.
ArrayBlockingQueue
is backed by an array that size will never change after creation. Setting the capacity toInteger.MAX_VALUE
would create a big array with high costs in space.ArrayBlockingQueue
is always bounded.LinkedBlockingQueue
creates nodes dynamically until thecapacity
is reached. This is by defaultInteger.MAX_VALUE
. Using such a big capacity has no extra costs in space.LinkedBlockingQueue
is optionally bounded.