Producer Consumer Problem Solution Using Wait and Notify
Posted By : Navin Purohit | 16-Jan-2018
Producer-Consumer problem solution using wait and notify in Multithreading
In multi-threaded programming, we frequently encountered a producer-consumer problem which is also known as a bounded-buffer problem. If we do not implement it correctly, it creates mess in our application. We need to take care that consumer not consumed before producer produced items, in such scenarios it hard to debug and reproduce the anomalies.
In this blog, I will solve the producer-consumer problem using wait and notify. Before we jump into solution first understand the problem of producer-consumer.
In producer-consumer problem, producer won’t try to add data into buffer once it full and the consumer won’t consume after buffer empty. The consumer will know when data entered into the buffer for consuming and producer must stop producing when consumption rate is slow and the buffer is filled.
To solve producer consumer problem, We need to take care few things like producer stop producing by either go to sleep or discard data. And when consumer consumes the item from the buffer, a notification goes to the producer that it can start producing again. In the same way, the consumer goes to sleep when the buffer is empty and when producer produces
1. In ProducerConsumer class, a shared LinkedList which have a capacity of 2 in which producer add items and consumer consume it.
2. In producer method above, the value is initialized as 0. Inside this method, we have an infinite outer loop which contains synchronized block which ensures that at a time only producer or consumer thread runs. The inner loop checks the capacity of a list, if list if full then it gives up the lock on the synchronized block and goes to
3. In consumer method above, we again have an infinite outer loop which checks/consume a value from the list. Same as producer method, inner loop checks if the list is empty. If a list is empty then it releases lock and tells producer that please producer more items. And if the list is not empty then it removes
4. We use notify() to tell other waiting thread that now it can take the lock and start processing. Sleep() method used here just for better understanding.
In my next blog, we will see the solution