Consumer-Producer Problem In Java Multithreading

Adil
Geek Culture
Published in
1 min readSep 25, 2021

In computing or java, the consumer-producer problem is multiple resources trying to access single resources with synchronized block.

What is the problem.?

Consumer-producer problem is when consumer and producer accessing buffer at the same time.

Consumer:- When Consumer trying to access empty buffer.

Producer:- When the producer trying to add data more than the buffer size.

Solution Consumer-Producer Problem With notify() method.

Now we will solve the problem with notify() method on the thread so once the producer trying to add data in the buffer and the buffer is already full so wait() method will be invoked on the thread and the producer will wait until the buffer have no space or empty.

The same process will happen on consumers if the buffer gets empty so we will call wait() method on the thread and wait until the producer does not add data on the buffer.

Read all comments for better understanding and also try to run same code on your local machine.

Consumer-Producer Problem

Github link https://github.com/smadil997/java-basic/blob/main/ThreadExampleWithRunnableNormal.java

--

--