Difference between SynchronousQueue vs TransferQueue

concurrency, java, producer-consumer, queue

Solution

As mentioned in this post by Alex Miller

TransferQueue is more generic and useful than SynchronousQueue however as it allows you to flexibly decide whether to use normal BlockingQueue semantics or a guaranteed hand-off. In the case where items are already in the queue, calling transfer will guarantee that all existing queue items will be processed before the transferred item.

SynchronousQueue implementation uses dual queues (for waiting producers and waiting consumers) and protects both queues with a single lock. The LinkedTransferQueue implementation uses CAS operations to form a nonblocking implementation and that is at the heart of avoiding serialization bottlenecks.

Problem

What is the difference between these two implementations? In which cases should be used one over another?

Original source