Concurrency in Java: Producer-Consumer Problem

  • 4.7/5
  • 3816
  • Jul 20, 2024

Problem

The producer-consumer problem (bounded buffer problem) describes two processes, the producer and the consumer.

Both Producer and Consumer share a common memory buffer of a certain size.

The producer produces the data in the buffer, and the consumer consumes the data from the buffer.

The task is to make sure that the producer won't try to add data to the buffer if it's full and the consumer won't try to remove data from an empty buffer.

Solution

If the buffer is empty, the consumer is to go to sleep. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.

If the buffer is full, the producer is to either go to sleep or discard data. The next time the consumer consumes (removes) an item from the buffer, it notifies the producer, who starts to fill the buffer again.

Implementation

Producer-consumer problems can be solved using Java concurrency constructs such as wait() and notify().

The idea is to create a "Task" class containing both the "data buffer (LinkedList)" and the "consume() and produce()" methods.

Make sure to mark the consume() and produce() methods with the "synchronized" keyword. This will make sure that only one thread is able to execute either of the two consume() or produce() methods on the same object.

Now, the process is simple: if the buffer is empty, make the "consumer" thread wait, and if the buffer is full, make the "producer" thread wait.

...

Produced: true, Queue size: 6
Produced: true, Queue size: 7
Produced: true, Queue size: 8
Produced: true, Queue size: 9
Produced: true, Queue size: 10
Consumed: Something !!!, Queue size: 9
Consumed: Something !!!, Queue size: 8
Consumed: Something !!!, Queue size: 7
Consumed: Something !!!, Queue size: 6
Consumed: Something !!!, Queue size: 5
Consumed: Something !!!, Queue size: 4
Produced: true, Queue size: 5
Produced: true, Queue size: 6
Produced: true, Queue size: 7
Produced: true, Queue size: 8
Produced: true, Queue size: 9
Produced: true, Queue size: 10
Consumed: Something !!!, Queue size: 9
Consumed: Something !!!, Queue size: 8
Consumed: Something !!!, Queue size: 7
Consumed: Something !!!, Queue size: 6
Consumed: Something !!!, Queue size: 5
Consumed: Something !!!, Queue size: 4
Consumed: Something !!!, Queue size: 3
Consumed: Something !!!, Queue size: 2
Consumed: Something !!!, Queue size: 1
Consumed: Something !!!, Queue size: 0
Produced: true, Queue size: 1
....
....

Related: Producer-consumer problem using BlockingQueue

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