In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue.
- The producer’s job is to generate data, put it into the buffer, and start again.
- At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one piece at a time.
To make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.
The producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.
An inadequate solution could result in a deadlock where both processes are waiting to be awakened.
Implementation of Producer Consumer Class
- A LinkedList list – to store list of jobs in queue.
- A Variable Capacity – to check for if the list is full or not
- A mechanism to control the insertion and extraction from this list so that we do not insert into list if it is full or remove from it if it is empty.
Note: It is recommended to test the below program on a offline IDE as infinite loops and sleep method may lead to it time out on any online IDE
Producer produced-0 Producer produced-1 Consumer consumed-0 Consumer consumed-1 Producer produced-2
- In PC class (A class that has both produce and consume methods), a linked list of jobs and a capacity of the list is added to check that producer does not produce if the list is full.
- In Producer class, the value is initialized as 0.
- Also, we have an infinite outer loop to insert values in the list. Inside this loop, we have a synchronized block so that only a producer or a consumer thread runs at a time.
- An inner loop is there before adding the jobs to list that checks if the job list is full, the producer thread gives up the intrinsic lock on PC and goes on the waiting state.
- If the list is empty, the control passes to below the loop and it adds a value in the list.
- Inside, we also have an inner loop which checks if the list is empty.
- If it is empty then we make the consumer thread give up the lock on PC and passes the control to producer thread for producing more jobs.
- If the list is not empty, we go round the loop and removes an item from the list.
- Readers are advised to use if condition in place of inner loop for checking boundary conditions.
- Try to make your program produce one item and immediately after that make the consumer consume it before any other item is produced by the producer.
This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Difference Between Daemon Threads and User Threads In Java
- Killing threads in Java
- Joining Threads in Java
- Introducing Threads in Socket Programming in Java
- Green vs Native Threads and Deprecated Methods in Java
- Understanding threads on Producer Consumer Problem | Java
- Producer-Consumer solution using Semaphores in Java | Set 2
- Longest prefix matching - A Trie based solution in Java
- What are Threads in Computer Processor or CPU?
- Threads and its types in Operating System
- C++ File Writer-Reader application using Windows Threads
- Dining-Philosophers Solution Using Monitors
- Reader-Writers solution using Monitors
- Classical problems of Synchronization with Semaphore Solution
- Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution)
Improved By : Gaurav Yadav 6