Algorithms

BlockingQueue

๐Ÿ“‹ Quick Reference

PropertyValue
TypeThread-safe queue with blocking operations
Key FeatureThreads block until operation can proceed
Common ImplsArrayBlockingQueue, LinkedBlockingQueue
Thread SafetyFully thread-safe
NullNot allowed
Best ForProducer-consumer pattern, thread pools
One-liner: Thread-safe queue where threads block on empty/full instead of failing.

๐ŸŽฎ Interactive Visualizer

Watch producers and consumers interact through a blocking queue:

Loading visualizer...
Try these operations:
  1. Add producers faster than consumers - see queue fill up
  2. Watch producer block when queue is full
  3. See consumer block when queue is empty
  4. Observe the handoff between threads

๐Ÿ”ง Key Operations

Creating

JAVA(17 lines)
Code
Loading syntax highlighter...

Blocking Operations (Wait Forever)

JAVA(5 lines)
Code
Loading syntax highlighter...

Timed Blocking Operations

JAVA(5 lines)
Code
Loading syntax highlighter...

Non-Blocking Operations

JAVA(15 lines)
Code
Loading syntax highlighter...

Bulk Operations

JAVA(8 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Operation Behavior Summary

MethodFull QueueEmpty Queue
put()BlocksN/A
offer()Returns falseN/A
offer(timeout)Blocks then falseN/A
add()Throws exceptionN/A
take()N/ABlocks
poll()N/AReturns null
poll(timeout)N/ABlocks then null
remove()N/AThrows exception

โœ… When to Use BlockingQueue

Good Use Cases

  • Producer-consumer - decouple production from consumption
  • Thread pools - task queue for worker threads
  • Backpressure - bounded queue limits producer speed
  • Pipeline stages - connect processing stages
  • Event handling - async event processing

Choose Implementation By Need

ImplementationUse When
ArrayBlockingQueueFixed capacity, fairness needed
LinkedBlockingQueueHigh throughput, flexible capacity
PriorityBlockingQueuePriority-based processing
DelayQueueDelayed/scheduled tasks
SynchronousQueueDirect handoff, no buffering

๐Ÿงฉ Common Patterns

Basic Producer-Consumer

JAVA(31 lines)
Code
Loading syntax highlighter...

Multiple Consumers (Work Stealing)

JAVA(18 lines)
Code
Loading syntax highlighter...

Graceful Shutdown with Poison Pill

JAVA(14 lines)
Code
Loading syntax highlighter...

Timeout-Based Consumer

JAVA(9 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Ignoring InterruptedException

JAVA(14 lines)
Code
Loading syntax highlighter...

2. Using Unbounded Queue with Fast Producer

JAVA(6 lines)
Code
Loading syntax highlighter...

3. Blocking in Main Thread

JAVA(8 lines)
Code
Loading syntax highlighter...

4. Wrong Method for Use Case

JAVA(13 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: What's the difference between put() and offer()?
"

put() blocks until space is available - use for producer-consumer where blocking is desired. offer() returns false immediately if full - use when you need non-blocking behavior or want to handle backpressure differently.

Q: Why use BlockingQueue instead of synchronized + wait/notify?
"

BlockingQueue encapsulates the synchronization logic, handles edge cases correctly, and provides a cleaner API. It's less error-prone than manual wait/notify and better tested.

Q: ArrayBlockingQueue vs LinkedBlockingQueue?
"

Array is fixed-size with better memory locality. Linked can grow (if unbounded) and may have higher throughput in high-contention scenarios due to separate head/tail locks.

Q: What's SynchronousQueue?
"

A queue with zero capacity - put() blocks until another thread calls take(). Used for direct handoff between threads, like in cached thread pools where tasks are handed directly to waiting threads.


๐Ÿ“š Series Navigation


Visualizer: BlockingQueue from @tomaszjarosz/react-visualizers