Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

What does TransferQueue add over BlockingQueue, and how does LinkedTransferQueue differ from SynchronousQueue?

TransferQueue adds transfer(e), which blocks until a consumer has actually received the element, and tryTransfer, which hands the element over only if a consumer is waiting, immediately or within a timeout, and otherwise returns false without queuing it. It still supports put and offer, which enqueue without waiting, so one queue can act as both a buffer and a handoff. LinkedTransferQueue is unbounded and implemented with a non-blocking dual queue of data and request nodes, while SynchronousQueue only does handoff, with no buffering at all.

Why this question is asked

Most queues decouple producers from consumers completely: the producer adds an item and moves on, never knowing when or whether it was taken. Sometimes the producer needs to know, for example to avoid running ahead of consumers or to measure that work was really picked up. TransferQueue, added in Java 7, gives the producer that choice per element. Interviewers ask about it to see whether we understand handoff versus buffering, and how it relates to SynchronousQueue. In this answer, we will recap the difference between buffering and handoff, go through the TransferQueue methods with code, look at how LinkedTransferQueue works inside, compare it with SynchronousQueue, and see when transfer is useful.

1A Quick Recap: Buffering Versus Handoff

A buffering queue stores elements until someone takes them. put on a LinkedBlockingQueue returns as soon as the element is stored, so the producer continues even if no consumer exists. A handoff passes an element directly from one thread to another: the producer waits until a consumer arrives to take it, like handing a parcel to a person instead of leaving it in a mailbox.

BlockingQueue implementations each pick one model: most buffer, and SynchronousQueue only hands off. TransferQueue extends BlockingQueue and lets the producer choose per element, which is its whole point.

2The Methods TransferQueue Adds

transfer(e) puts the element in the queue and then waits until a consumer has received it through take or poll. If a consumer is already waiting, the element goes straight to it and transfer returns at once. tryTransfer(e) never waits: if a consumer is waiting, it hands the element over and returns true; otherwise it returns false and the element is not added to the queue. tryTransfer(e, timeout, unit) waits up to the timeout for a consumer to receive it; if time runs out, it returns false and the element is removed again, as if it had never been offered. hasWaitingConsumer() and getWaitingConsumerCount() report whether consumers are currently blocked waiting.

Java
TransferQueue<String> q = new LinkedTransferQueue<>();
System.out.println(q.tryTransfer("a")); // false: no consumer waiting, and "a" is NOT queued
System.out.println(q.size()); // 0
q.put("b"); // ordinary buffering: returns at once
System.out.println(q.size()); // 1
Thread consumer = Thread.ofVirtual().start(() -> {
try {
System.out.println(q.take()); // b
Thread.sleep(500);
System.out.println(q.take()); // c
} catch (InterruptedException e) { Thread.currentThread().interrupt(); }
});
q.transfer("c"); // waits about 500 ms, until the consumer takes "c"
System.out.println("c was received"); // printed after "c"
Step throughBuffered put and waiting transfer on one queue
ProducerLinkedTransferQueueConsumer
put("b"): stored, returns at once
transfer("c"): stored, producer waits
take()
"b"
take() after 500 ms
"c"
transfer returns: "c" was received

7 messages in order. Play them, or step through them one at a time.

Notice that transfer respects queue order. "b" was already buffered, so the consumer takes it first, and transfer("c") keeps waiting until "c" itself is taken. Notice also that put and offer still work exactly as in any unbounded queue, so the same object is a buffer for elements added with put and a handoff for elements added with transfer.

3How LinkedTransferQueue Works Inside

LinkedTransferQueue is the JDK's implementation. It is unbounded and built as a dual queue: a linked list whose nodes are either data nodes, holding an element a producer offered, or request nodes, representing a consumer waiting for an element. At any moment the list holds only one kind of unmatched node. When a producer arrives and finds a waiting consumer's request node at the head, it matches with it, filling in the element and waking that consumer. When a consumer arrives and finds a data node, it takes the element. If there is nothing to match, the arriving thread appends its own node.

Matching is done with compare-and-set operations on the nodes rather than a lock, so producers and consumers do not block each other while queuing. A thread that must wait, such as a consumer on an empty queue or a producer in transfer, first spins briefly in case a partner arrives in the next few microseconds, and then parks. put and offer add a data node and return immediately; transfer adds the same kind of node but then waits until a consumer has matched it.

What happens when a thread arrives
Arriving callQueue holds waiting consumersQueue holds data or is empty
put(e) / offer(e)match: hand e to a consumerappend data node, return
transfer(e)match: hand e over, returnappend data node, wait until taken
tryTransfer(e)match: return truereturn false, nothing stored
take()append request node, waittake the first element
The middle column for take applies when the queue already holds other waiting consumers.

4How It Differs From SynchronousQueue

SynchronousQueue only does handoff. It has no capacity, so put always waits for a consumer, offer without a timeout succeeds only if a consumer is already waiting, and size() is always 0. There is no way to buffer an element in it. It is used, for example, by Executors.newCachedThreadPool, where each task should go to an idle thread or cause a new thread to be created, never wait in a queue.

LinkedTransferQueue can do everything SynchronousQueue does, through transfer and tryTransfer, and can also buffer through put. So it is more general: it behaves like a SynchronousQueue for elements we transfer and like an unbounded LinkedBlockingQueue for elements we put. The two share a lot of design, and in recent JDKs SynchronousQueue reuses much of LinkedTransferQueue's machinery, but check your version if the exact implementation matters.

5When transfer Is Useful

transfer is useful when a producer must know its item was taken. A common case is flow control: a producer that calls transfer can never get ahead of its consumers, because each call waits for a taker, so the queue never grows beyond what consumers are actively handling. tryTransfer supports an adaptive pattern: try to hand work to an idle consumer, and if none is waiting, do something else, such as processing it in the current thread, buffering it with put, or rejecting it.

Because LinkedTransferQueue is unbounded, put alone offers no backpressure, so we either use transfer for flow control or limit producers another way. It is also a good general-purpose concurrent queue under high contention, since its non-blocking design scales well with many producers and consumers.

Java
// Hand work to an idle worker if one is waiting, otherwise run it here
void submit(Runnable task) {
if (!workQueue.tryTransfer(task)) {
task.run(); // no idle worker: the caller does the work, natural backpressure
}
}

6Follow-up questions

Does transfer return when the element is processed?

No. It returns when a consumer has received the element, meaning take or poll returned it. What the consumer does next, including failing, is not covered. If the producer needs to know about completion, it needs a separate signal, such as a CompletableFuture carried in the element.

Is LinkedTransferQueue bounded?

No, it is unbounded; remainingCapacity always returns Integer.MAX_VALUE and put never blocks. If we rely only on put, a slow consumer lets the queue grow without limit, just like an unbounded LinkedBlockingQueue.

Can tryTransfer with a timeout leave the element in the queue?

No. If the timeout expires before a consumer receives the element, tryTransfer removes it and returns false. The element is either received by a consumer or not in the queue at all, so the producer always knows which happened.

7Conclusion

In this answer, we saw that TransferQueue adds transfer, which waits until a consumer receives the element, and tryTransfer, which hands the element over only if a consumer is waiting, immediately or within a timeout, and otherwise leaves nothing queued. put and offer still buffer without waiting, so one queue can be both a buffer and a handoff. LinkedTransferQueue implements it as an unbounded, non-blocking dual queue of data and request nodes, while SynchronousQueue only hands off and never buffers. transfer is useful when the producer must know its item was taken, for example for flow control.

What to say in the interviewAbout 100 seconds aloud

Open with

TransferQueue extends BlockingQueue with a way for the producer to wait until a consumer actually receives an element. transfer(e) adds the element and blocks until some consumer has taken it with take or poll; if a consumer is already waiting, it hands the element over directly and returns at once. tryTransfer(e) is the non-blocking version: if a consumer is waiting it hands the element over and returns true, otherwise it returns false and nothing is queued.

Then, if they want more

The timed tryTransfer waits up to the timeout and, if no consumer took it, removes the element and returns false. At the same time it still supports put and offer, which just enqueue and return, so the same queue can act as a buffer for some elements and a handoff for others. LinkedTransferQueue is the implementation.

It is unbounded and built as a dual queue: nodes are either data nodes from producers or request nodes from waiting consumers, and arriving threads match with the opposite kind using compare-and-set rather than locks, spinning briefly before parking. SynchronousQueue, in contrast, only does handoff: it has no capacity, put always waits for a consumer, and size is always zero.

So LinkedTransferQueue is more general; it behaves like a SynchronousQueue for transferred elements and like an unbounded linked queue for put. I would use transfer when the producer must know the item was taken, for example to keep producers from getting ahead of consumers, or tryTransfer to give work to an idle consumer and otherwise handle it myself.

About this answer

Last updated
12 November 2026
Versions it names
Java 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does TransferQueue add over BlockingQueue, and how does LinkedTransferQueue differ...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/2f5785ce67/