Using TransferQueue could slow down throughput, especially if the processing time for each work unit in producer / consumer varies. Consider an example where producer / consumer average speed is the same - say your producer is consistently taking 5 seconds per item but the consumer takes either 0 seconds (discards it) or 10 seconds processing.
If you used TransferQueue
and transfer
/ take
the overall processing time would take longer as the producer might have to wait 5-10 seconds for consumer to take
the next step. Similarly, consumer may wait 5 seconds for producer to supply next item. Average time for producer-consumer exceeds 5 seconds per item.
With BlockingQueue
put
/ take
with suitable buffer queue length the producer can keep busier, adding to queue if one particular consumer task takes 10 seconds, and consumer catches up later if handling several quick to process items. Average time for producer-consumer should be close to 5 seconds per item.
However, if your app needs to know when consumer is processing an item, use TransferQueue
.