Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Compare ArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue and PriorityBlockingQueue, and explain the difference between put/take, offer/poll and add/remove.

ArrayBlockingQueue is bounded and array-backed with one lock and two conditions, and can be fair; LinkedBlockingQueue is optionally bounded, defaults to Integer.MAX_VALUE, and uses separate put and take locks; SynchronousQueue has no capacity, so every insert waits for a matching remove; PriorityBlockingQueue is unbounded and ordered by priority, so put never blocks. For the methods, put and take block, offer and poll return false or null (optionally after a timeout), and add and remove throw exceptions.

Why this question is asked

BlockingQueue is the backbone of producer-consumer code in Java and of every ThreadPoolExecutor, so the choice of implementation decides whether a system applies backpressure, runs out of memory, or hands work directly to threads. Many production incidents start with an unbounded LinkedBlockingQueue nobody noticed. Interviewers ask this comparison to see whether we know how each queue stores elements and locks, and whether we know which method to call. In this answer, we will recap what makes a queue blocking, look at each of the four implementations and how it works, compare them in a table, and then explain the three families of insert and remove methods with examples.

1A Quick Recap of Blocking Queues

A BlockingQueue is a thread-safe queue that can make threads wait. A consumer that calls take on an empty queue waits until an element arrives. A producer that calls put on a full queue waits until there is space. That waiting is what connects producers and consumers running at different speeds: if consumers are slow and the queue is bounded, producers are forced to slow down too. This is called backpressure.

Internally, waiting is built from locks and conditions. A condition is a place where a thread can sleep until another thread signals it, for example a notEmpty condition that take waits on and put signals. The four implementations differ in capacity, ordering and how they arrange these locks.

2ArrayBlockingQueue and LinkedBlockingQueue

ArrayBlockingQueue stores elements in a fixed-size circular array whose capacity we must give in the constructor. It uses a single ReentrantLock for both ends and two conditions, notEmpty and notFull. Because all operations share one lock, a producer and a consumer cannot work at the same instant, but the structure is simple, allocates nothing per element, and has predictable memory. The constructor new ArrayBlockingQueue<>(capacity, true) creates a fair queue, where waiting threads get access in arrival order; fairness avoids starvation but lowers throughput.

LinkedBlockingQueue stores elements in linked nodes and is optionally bounded. Without a capacity argument, its capacity is Integer.MAX_VALUE, which in practice means unbounded, and that is the queue behind Executors.newFixedThreadPool. It uses two locks, a putLock for the tail and a takeLock for the head, plus an atomic counter for the size, so a producer and a consumer can proceed in parallel. That often gives higher throughput under contention, at the cost of a node allocation per element.

Java
BlockingQueue<String> abq = new ArrayBlockingQueue<>(2);
abq.put("a"); abq.put("b");
abq.offer("c"); // false: full, returns at once
abq.remainingCapacity(); // 0
BlockingQueue<String> lbq = new LinkedBlockingQueue<>(); // capacity Integer.MAX_VALUE
lbq.remainingCapacity(); // 2147483647

3SynchronousQueue and PriorityBlockingQueue

SynchronousQueue has no capacity at all. It is not a buffer but a meeting point: a put waits until another thread takes the element, and a take waits until another thread puts one. size() always returns 0 and peek always returns null. offer without a timeout succeeds only if a consumer is already waiting. Executors.newCachedThreadPool uses it, so that each task is handed directly to an idle thread, and if none is waiting, the offer fails and the executor creates a new thread.

PriorityBlockingQueue is the thread-safe version of PriorityQueue: an array-based binary heap guarded by one lock, returning elements in priority order rather than insertion order. It is unbounded, so put never blocks; it only grows, until memory runs out. take blocks only when the queue is empty. Because it never pushes back on producers, using it between fast producers and slow consumers needs some other limit.

Java
SynchronousQueue<String> sq = new SynchronousQueue<>();
sq.offer("x"); // false: no consumer is waiting
sq.size(); // 0, always
Thread.ofVirtual().start(() -> { // a consumer waits
try { System.out.println(sq.take()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
});
sq.put("y"); // returns once the consumer has taken "y"; prints y
PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue<>();
pbq.put(5); pbq.put(1); pbq.put(3); // never blocks
pbq.take(); // 1
The four implementations side by side
QueueCapacityLockingOrder
ArrayBlockingQueuebounded, fixed at creationone lock, two conditions, optional fairnessFIFO
LinkedBlockingQueueoptional; default Integer.MAX_VALUEseparate put and take locksFIFO
SynchronousQueuezero: direct handoffCAS-based handoff, no buffernone
PriorityBlockingQueueunboundedone lockpriority (heap)

4Put/take, offer/poll and add/remove

BlockingQueue gives us four ways to insert and remove, and they differ only in what happens when the operation cannot proceed right now. put and take block: they wait as long as necessary. offer and poll return a special value at once: offer returns false when the queue is full and poll returns null when it is empty. The overloads offer(e, timeout, unit) and poll(timeout, unit) wait up to the timeout and then return false or null. add and remove, inherited from Queue and Collection, throw exceptions: add throws IllegalStateException when the queue is full, and remove throws NoSuchElementException when it is empty.

The choice depends on what the caller should do when the queue cannot accept or supply work. A background worker usually calls take and sleeps until there is work. A request thread should not wait forever, so it calls offer with a short timeout and returns an error to the client if the queue stays full. add is rarely the right choice for a bounded queue, because a full queue is a normal situation, not a bug.

Java
BlockingQueue<String> q = new ArrayBlockingQueue<>(1);
q.add("a"); // true
q.offer("b"); // false: full
q.offer("b", 100, TimeUnit.MILLISECONDS); // false after waiting 100 ms
q.add("b"); // IllegalStateException: Queue full
q.take(); // "a"
q.poll(); // null: empty
q.poll(100, TimeUnit.MILLISECONDS); // null after waiting 100 ms
q.remove(); // NoSuchElementException
q.take(); // blocks until another thread puts
What each method does when it cannot proceed
BehaviourInsertRemoveExamine
Throws an exceptionadd(e)remove()element()
Returns a special valueoffer(e): falsepoll(): nullpeek(): null
Blocksput(e)take()
Times outoffer(e, time, unit)poll(time, unit)

5Choosing Between Them

For a buffer between producers and consumers, the default choice is a bounded queue, usually ArrayBlockingQueue with a capacity derived from how much waiting we can tolerate, or a LinkedBlockingQueue given an explicit capacity when two-lock throughput matters. SynchronousQueue fits when we want no buffering at all, so that a producer only proceeds when a consumer is ready. PriorityBlockingQueue fits when order by priority matters more than bounding, and then we limit producers some other way.

All four reject null elements, because poll uses null to signal an empty queue. And all are safe for any number of producer and consumer threads, but compound actions, such as checking size and then offering, are not atomic, so code should rely on the return values of offer and poll rather than on size().

6Follow-up questions

Why does LinkedBlockingQueue use two locks?

Because producers only touch the tail and consumers only touch the head, two locks let one producer and one consumer work at the same time. The shared element count is an AtomicInteger, so neither side needs the other's lock to know whether the queue is empty or full. ArrayBlockingQueue keeps one lock because both ends share one array and its indexes.

What does the fairness flag of ArrayBlockingQueue change?

With fairness enabled, the internal lock is a fair ReentrantLock, so blocked threads acquire it roughly in the order they started waiting. That prevents a thread from waiting indefinitely, but it reduces throughput, because the lock cannot be handed to whichever thread is ready first. The default is non-fair.

Is size() reliable on these queues?

It is accurate at the moment it is computed, but other threads may change the queue immediately afterwards, so decisions based on it are racy. For SynchronousQueue it is always 0. Monitoring queue depth with size() is fine; using it to decide whether to insert is not.

7Conclusion

In this answer, we compared the four main BlockingQueue implementations. ArrayBlockingQueue is a bounded circular array with one lock, two conditions and optional fairness; LinkedBlockingQueue is linked, optionally bounded with a default of Integer.MAX_VALUE, and uses separate put and take locks; SynchronousQueue has no capacity and hands each element directly to a waiting consumer; PriorityBlockingQueue is an unbounded heap whose put never blocks. We also saw that put and take block, offer and poll return false or null, optionally after a timeout, and add and remove throw exceptions.

What to say in the interviewAbout 90 seconds aloud

Open with

ArrayBlockingQueue is bounded: we give a capacity, and it stores elements in a fixed circular array, guarded by a single ReentrantLock with two conditions, notEmpty and notFull. It can be created fair, so waiting threads are served in order, at some cost in throughput.

Then, if they want more

LinkedBlockingQueue uses linked nodes and is optionally bounded; if we do not pass a capacity it is Integer.MAX_VALUE, effectively unbounded, which is why newFixedThreadPool can run out of memory. It has separate put and take locks with an atomic count, so a producer and a consumer can work in parallel.

SynchronousQueue has no capacity at all: each put waits for a take and vice versa, so it is a direct handoff, which newCachedThreadPool uses to give each task to an idle thread or create one. PriorityBlockingQueue is an unbounded heap ordered by priority, so put never blocks and only take waits, when it is empty.

For the methods, there are three families: put and take block until they can proceed; offer and poll return false or null immediately, or after waiting for a timeout if we use the timed versions; and add and remove throw IllegalStateException or NoSuchElementException.

In practice I use take in workers, offer with a timeout from request threads so they never hang, and I prefer bounded queues so slow consumers create backpressure instead of memory growth.

About this answer

Last updated
12 November 2026
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 "Compare ArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue and...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/3bb20964b1/