LinkedBlockingQueue vs. ConcurrentLinkedQueue
Why and when do we use Blocking Queues and Non Blocking Queues with working with streams and media? Unbounded or not? What is when the best choice?
Once upon a time, in a galaxy far, far away, there was a beautiful young Queue... :-)
Following operations are allowed e.g.:
bqueue.take() to take form a stream...
What are the advantages/disadvantages of unbounded queue?
With an unbounded queue, a producers can get far ahead of the consumers. Pay attention for OutOfMemoryError! How can we signal a producer that the queue is full?
ASPECTS:
https://www.geeksforgeeks.org/linkedblockingqueue-class-in-java/
https://www.geeksforgeeks.org/java-8-linkedblockingqueue-spliterator-method-with-examples/
The LinkedBlockingQueue is an optionally-bounded blocking queue based on linked nodes. It means that the LinkedBlockingQueue can be bounded, if its capacity is given, else the LinkedBlockingQueue will be unbounded. The capacity can be given as a parameter to the constructor of LinkedBlockingQueue.
This queue orders elements FIFO (first-in-first-out). It means that the head of this queue is the oldest element of the elements present in this queue. The tail of this queue is the newest element of the elements of this queue. The newly inserted elements are always inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
https://www.javacodex.com/Concurrency/ConcurrentLinkedQueue-Example
ConcurrentLinkedQueue is an unbounded thread-safe queue based on linked nodes. This queue orders elements as a FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
A ConcurrentLinkedQueue is a good choice when many threads share access to a common collection. Like most other concurrent collection implementations, this class does not permit the use of null elements.
Useful sites to learn it deeper:
https://www.javacodex.com/Concurrency/
https://dzone.com/articles/a-specialized-high-performance-concurrent-queue
bqueue.take() to take form a stream...
What are the advantages/disadvantages of unbounded queue?
With an unbounded queue, a producers can get far ahead of the consumers. Pay attention for OutOfMemoryError! How can we signal a producer that the queue is full?
ASPECTS:
- speed
- concurrency
- time to access last element
- blocking
- unbounded
LinkedBlockingQueue
A LinkedBlockingQueue is a good choice for many pools (depending on the pool management strategy):https://www.geeksforgeeks.org/linkedblockingqueue-class-in-java/
https://www.geeksforgeeks.org/java-8-linkedblockingqueue-spliterator-method-with-examples/
The LinkedBlockingQueue is an optionally-bounded blocking queue based on linked nodes. It means that the LinkedBlockingQueue can be bounded, if its capacity is given, else the LinkedBlockingQueue will be unbounded. The capacity can be given as a parameter to the constructor of LinkedBlockingQueue.
This queue orders elements FIFO (first-in-first-out). It means that the head of this queue is the oldest element of the elements present in this queue. The tail of this queue is the newest element of the elements of this queue. The newly inserted elements are always inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
ConcurrentLinkedQueue
The ConcurrentLinkedQueue is good as well in many cases:https://www.javacodex.com/Concurrency/ConcurrentLinkedQueue-Example
ConcurrentLinkedQueue is an unbounded thread-safe queue based on linked nodes. This queue orders elements as a FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
A ConcurrentLinkedQueue is a good choice when many threads share access to a common collection. Like most other concurrent collection implementations, this class does not permit the use of null elements.
A limited capacity of BlockingQueue
(https://www.javacodex.com/Concurrency/BlockingQueueExample) is also helpful if you want to throttle some sort of request.Useful sites to learn it deeper:
https://www.javacodex.com/Concurrency/
https://dzone.com/articles/a-specialized-high-performance-concurrent-queue
No comments:
Post a Comment