Sitemap

BlockingQueue

2 min readJun 3, 2025

--

In Java, a Blocking Queue is a type of Queue that supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element. This makes Blocking Queues highly useful in concurrent programming, especially for producer-consumer scenarios where you have threads that produce items and threads that consume items from a shared buffer.

Press enter or click to view image in full size

Implementations Of Blocking Queue

Java’s java.util.concurrent package includes several implementations of the BlockingQueue interface, each with unique features and suited for different scenarios. Here’s an overview of the primary BlockingQueue implementations:

1. ArrayBlockingQueue

Features: A bounded blocking queue backed by an array. This queue orders elements FIFO (first-in-first-out). The “fairness” property can be set, which determines whether queue accesses for threads are processed in FIFO order.

Use Case: Suitable for fixed-size data buffers where you need to control the number of elements it can hold.

2. LinkedBlockingQueue

Features: An optionally bounded blocking queue based on linked nodes. This queue orders elements of FIFO. The capacity, if unspecified, is equal to Integer.MAX_VALUE.

Get Ravi Jaisawal’s stories in your inbox

Join Medium for free to get updates from this writer.

Use Case: Suitable for scenarios where you want a FIFO queue with high throughput. The default high capacity makes it a good choice for when maximum capacity is not a concern.

3. PriorityBlockingQueue

Features: An unbounded blocking queue that uses the same ordering rules as java.util.PriorityQueue. Elements are ordered according to their natural ordering or by a Comparator provided at queue construction time.

Use Case: Useful when you need to process elements based on priority rather than the order they were added.

4. DelayQueue

Features: An unbounded blocking queue of Delayed elements, where an element can only be taken when its delay has expired. The queue is sorted so that the element at the head has the delay that will expire next.

Use Case: Ideal for scheduling tasks for execution after a certain delay.

5. SynchronousQueue

Features: A blocking queue in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa. The queue does not hold any elements; instead, it simply coordinates the handoff between producer and consumer threads.

Use Case: Suitable for handoff designs, where the presence of an element directly means there’s a thread ready to take it, effectively facilitating thread synchronization without additional buffering.

Conclusion

In summary, while BlockingQueue provides a robust solution for managing data between threads, understanding its limitations is crucial for applying it effectively in your concurrent applications. Balancing its use with other concurrency mechanisms based on your application’s specific needs is key to achieving optimal performance and correctness.

--

--

Ravi Jaisawal
Ravi Jaisawal

Written by Ravi Jaisawal

Lead Software Engineer with 12+ years of experience architecting and delivering scalable, cloud-native distributed systems and enterprise platforms.