LinkedBlockingQueue put vs offer

Scientist picture Scientist · Oct 17, 2013 · Viewed 11.3k times · Source

I have a linked blocking queue in which I am performing insertion and removal operations.

I need to know which one is better put or offer in case of linked blocking queue.

Performance parameters are CPU utilization, memory and overall throughput.

Application usage is Real time system where there can be multiple incoming requests and less threads to handle where we will need to insert element in queue.

I read Java docs of put and offer there was not much difference in internal application.

Answer

codingenious picture codingenious · Oct 17, 2013

Actually, you can't compare performance between these two, the offer method is to just offer to the queue and it does not wait or waits for the time specified, but the put method waits infinitely long until space is available, so their usage is different.

Use put where you cannot afford to loose an item, keeping in mind it will hold up your call stack, otherwise use offer.