I’m working on a standard Java system with critical timing requirements for my producers (1/100s of ms matters).
I have a producer placing stuff in a blocking queue, and a single consumer later picking up that stuff and dumping it to a file. The consumer blocks when data is not available.
Obviously, blocking queue is the appropriate interface, but which actual implementation should I choose if I want to minimize the cost to the producer? I wan to play as little as possible on things like locking and allocating when I am putting stuff in the queue, and I don’t mind if the consumer has to wait a lot longer or work a lot harder.
Is there an implementation that can be faster because I only have a single consumer and single producer ?
Well, there really aren’t too many options. Let me go through the listed subclasses:
DelayQueue,LinkedBlockingDeque,PriorityBlockingQueue, andSynchronousQueueare all made for special cases requiring extra functionality; they don’t make sense in this scenario.That leaves only
ArrayBlockingQueueandLinkedBlockingQueue. If you know how to tell whether you need anArrayListor aLinkedList, you can probably answer this one yourself.Note that in
LinkedBlockingQueue, “linked nodes are dynamically created upon each insertion”; this might tend to push you towardArrayBlockingQueue.