producer-consumer problem in java 8

Producer-consumer Problem is one of the classic problems of multi-threading and also frequently asked in some of the interviews along with  Print Odd Even Numbers Using two threads and  Print 1,2,3 Using 3 Threads

In this blog, I will explain how to write a java program using inter-thread communication. In short using wait() and notify() methods. 



Problem Statement

There is one queue of n capacity. This queue is shared between producer and consumer. n is the capacity and for this blog, I will take n = 5. 

Producer adds a block to the queue when queue size is less than its capacity. Consumer will consume from the same queue when queue size is greater than 0. Using multi-threading, we need to write java program with thread synchronization. 

Approach


If you have gone through earlier blogs to Print Odd Even Numbers Using two threads and  Print 1,2,3 Using 3 Threads , you will notice a pattern which can be applied here as well. Take sometime to understand the pattern. 


Let us create two threads initially denoting producer and consumer. You can take the capacity from program arguments. I hardcoded this value to 5. 





When the queue is empty(), we need to produce to it. Here, producer will get a head start as queue is empty and consumer will be waiting for its turn till producer finishes producing to the queue.

Once producer finishes, consumer will start consuming and producer will be waiting till the queue is emptied. This will go on based on the condition in the while loop.


Here, the producer thread will only execute produce() method and consumer will only execute consumer() method of ProducerConsumer Object. 


Feel free to experiment as there are lot of versions of this problem. Try it out with multiple consumers and experiment with waiting logic. 

Output


producer consumer problem java using wait notify


Post a Comment