简体   繁体   中英

When to use BlockingQueue over TransferQueue?

Difference between BlockingQueue and TransferQueue have well explained the difference between these two queues, but looks like TransferQueue is just a better version of BlockingQueue.

Is there any situation that we should use BlockingQueue over TransferQueue, or use LinkedBlockingQueue over LinkedTransferQueue?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM