Concurrency in Java: Producer-consumer problem using BlockingQueue

  • 4.4/5
  • 5560
  • Jul 20, 2024

The java.util.concurrent.BlockingQueue is a queue that additionally 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.

A BlockingQueue may be capacity-bound and does not accept null elements.

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.

BlockingQueue methods

BlockingQueue methods come in four forms for handling operations that cannot be satisfied immediately but may be satisfied at some point in the future:

1) Throws exception
2) Special value (null or false)
3) Blocks
4) Times out

                                                                                               
Throws exceptionSpecial valueBlocksTimes out
Insertadd(e)offer(e)put(e)offer(e, time, unit)
Removeremove()poll()take()poll(time, unit)
Examineelement()peek()not applicablenot applicable

Producer-Consumer using BlockingQueue

A BlockingQueue can be safely used with multiple producers and multiple consumers.

...
...
Produced: Thread-1, RemainingCapacity: 3
Produced: Thread-1, RemainingCapacity: 8
Consumed: Thread-3, RemainingCapacity: 8
Consumed: Thread-2, RemainingCapacity: 8
Consumed: Thread-2, RemainingCapacity: 10
Produced: Thread-0, RemainingCapacity: 8
Produced: Thread-0, RemainingCapacity: 9
Produced: Thread-0, RemainingCapacity: 8
Consumed: Thread-2, RemainingCapacity: 9
Consumed: Thread-2, RemainingCapacity: 9
Consumed: Thread-2, RemainingCapacity: 10
Consumed: Thread-3, RemainingCapacity: 9
Produced: Thread-1, RemainingCapacity: 7
Produced: Thread-0, RemainingCapacity: 8
Produced: Thread-1, RemainingCapacity: 9
Produced: Thread-1, RemainingCapacity: 8
Produced: Thread-1, RemainingCapacity: 7
Produced: Thread-1, RemainingCapacity: 6
...
...

To know more about Producer-Consumer Problem visit: Producer-Consumer Problem.

Index
Modern Java - What’s new in Java 9 to Java 17

32 min

What is differences between JDK, JVM and JRE ?

2 min

What is ClassLoader in Java ?

2 min

Object Oriented Programming (OOPs) Concept

17 min

Concurrency in Java: Creating and Starting a Thread

12 min

Concurrency in Java: Interrupting and Joining Threads

5 min

Concurrency in Java: Race condition, critical section, and atomic operations

13 min

Concurrency in Java: Reentrant, Read/Write and Stamped Locks

11 min

Concurrency in Java: "synchronized" and "volatile" keywords

10 min

Concurrency in Java: using wait(), notify() and notifyAll()

6 min

Concurrency in Java: What is "Semaphore" and its use?

2 min

Concurrency in Java: CompletableFuture and its use

18 min

Concurrency in Java: Producer-consumer problem using BlockingQueue

2 min

Concurrency in Java: Producer-Consumer Problem

2 min

Concurrency in Java: Thread pools, ExecutorService & Future

14 min

Java 8 Lambdas, Functional Interface & "static" and "default" methods

28 min

Method Reference in Java (Instance, Static, and Constructor Reference)

9 min

What’s new in Java 21: A Tour of its Most Exciting Features

14 min

Java Memory Leaks & Heap Dumps (Capturing & Analysis)

9 min

Memory footprint of the JVM (Heap & Non-Heap Memory)

15 min