ProducerConsumer Solution using Threads in Java (original) (raw)

Producer-Consumer Solution using Threads in Java

Last Updated : 17 Mar, 2025

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.

**Problem
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.

**Solution
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.
**Recommended Reading- Multithreading in JAVA, Synchronized in JAVA, Inter-thread Communication

**Implementation of Producer-Consumer Class

_Note: It is recommended to test the below program on an offline IDE as infinite loops and sleep method may lead to it time out on any online IDE

Java `

// Java program to implement solution of producer-consumer problem. import java.util.LinkedList;

public class Geeks { public static void main(String[] args) throws InterruptedException { // Object of a class that has both produce() // and consume() methods final PC pc = new PC();

    // Create producer thread
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run()
        {
            try {
                pc.produce();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    
    // Create consumer thread
    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run()
        {
            try {
                pc.consume();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    
    // Start both threads
    t1.start();
    t2.start();
    
    // t1 finishes before t2
    t1.join();
    t2.join();
}

// This class has a list, producer (adds items to list)
// and consumer (removes items).
public static class PC {
    // Create a list shared by producer and consumer
    // Size of list is 2.
    LinkedList<Integer> list = new LinkedList<>();
    int capacity = 2;
    
    // Function called by producer thread
    public void produce() throws InterruptedException
    {
        int value = 0;
        while (true) {
            synchronized (this)
            {
                // producer thread waits while list is full
                if (list.size() == capacity) {
                    System.out.println("List is full, producer is waiting...");
                    // Signal any waiting consumer before waiting
                    notify();
                    wait();
                }
                
                // to insert the jobs in the list
                list.add(value);
                System.out.println("Producer produced-" + value);
                value++;
                
                // notifies the consumer thread that now it can start consuming
                notify();
                
                // makes the working of program easier to understand
                Thread.sleep(1000);
            }
        }
    }
    
    // Function called by consumer thread
    public void consume() throws InterruptedException
    {
        while (true) {
            synchronized (this)
            {
                // consumer thread waits while list is empty
                if (list.size() == 0) {
                    System.out.println("List is empty, consumer is waiting...");
                    // Signal any waiting producer before waiting
                    notify();
                    wait();
                }
                
                // to retrieve the first job in the list
                int val = list.removeFirst();
                System.out.println("Consumer consumed-" + val);
                
                // Wake up producer thread
                notify();
                
                // and sleep
                Thread.sleep(1000);
            }
        }
    }
}

}

`

**Output:

OutputProducerConsumerProblem

**Explanation: The above program is the code representation of the **Producer-Consumer problem. The producers add items to a shared buffer ( LinkedList), while the consumer removes and processes them. The producer waits if the buffer is full and the consumer waits if the buffer is empty. When the consumer consumes an item, it notifies the producer to add more items. Similarly, when the producer adds an item, it notifies the consumer to consume the item.

**Important Points:

**Exercise :