Came across this excellent article on comparison benchmark on famous queue datastructure in java collection.
Problem
I ran into an issue of producer producing faster than consumer in my java LinkedBlockingQueue due to network IO on my consumer, i could not solve this problem as the difference in time over a period of time causes a significant delay.
Solution
My theoretical solution to this problem is to solve by setting a threshold on my queue and functionally drop some repetitive producer which is feasible in my application.
Priority, Delays, and Other Details
There are multiple queue classes in the java.util.concurrent package that implement the
BlockingQueue
interface SynchronousQueue
: A hand-pattern. Producer(s) block until there is a consumer available; consumer(s) block until there is an enqueued message.ArrayBlockingQueue
: Similar to theSynchronousQueue
, except the queue can contain a pre-set number of items before the queue producer(s) block on queue insertion.DelayQueue
: Elements placed in the queue are not available for removal until their delay time has expired. Items that are furthest past their expiration time are available for removal first. Calls toput()
do not block since this queue is unbounded, although calls totake()
will block until an expired message is available.LinkedBlockingQueue
: Similar to anArrayBlockingQueue
, but where queue growth is allowed. However, the queue constructor does accept an optional parameter to constrain the growth to a maximum number of items.LinkedTransferQueue
: Similar toSynchronousQueue
, except the queue is unbounded in size and calls toput()
never block. Calls totake()
, however, will block until a queued item is available for removal.PriorityBlockingQueue
: Similar toSynchronousQueue
, except the queue is unbounded in size and calls toput()
never block. Calls totake()
, however, will block until a queued item is available for removal, and those items are subject to priority-based ordering. Enqueued objects must be comparable, meaning they must implement theComparable
interface and required methods.
No comments:
Post a Comment