Understanding threads on Producer Consumer Problem | Java

Thread is a part of execution i.e., it is an independent path of execution in a program. A program can have more than one thread which rises the concept of multithreading. We must use java.lang.Thread class in order to use a thread to perform a specific task. In this article, let’s see the implementation of the threads through a program.

Whenever a program is executed, the JVM first checks for the existence of the “Main” method. If the method is present, it by default creates a thread and this thread is called as the “main thread” because it is responsible for the execution of the statements that are present in the main method. A thread can be in multiple states which are discussed in this article.

There are two ways of creating a thread. They are:

  1. By creating an object for Thread class.
  2. By using Runnable Interface.

Thread creation by extending the Thread class: We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside the run() method. We create an object of the thread class and call start() method to start the execution of a thread. Start() invokes the run() method on the thread object. Let’s see an example to find the factorial using the tread:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the factorial
// of a number by the implementation
// of threads using thread class
  
class ThreadImplement extends Thread {
    static int fact = 1;
  
    // Overriding the run method
    // to find the factorial of
    // a number 5
    public void run()
    {
  
        // Loop to compute the factorial
        for (int i = 1; i <= 5; i++)
            fact = fact * i;
        System.out.println(fact);
    }
}
  
// Class to create a thread and
// compute the factorial
public class GFG {
    public static void main(String[] args)
    {
        // Creating an object of the
        // thread class
        Thread t1
            = new Thread(new ThreadImplement());
  
        // Computing the above class
        t1.start();
    }
}

chevron_right


Output:



120

Thread creation by implementing the Runnable Interface: The thread class implements runnable interface and the runnable interface contains only run() method. There are nearly 37 methods present in the thread class but we commonly use 22. All the tasks that need to be executed by the thread must be placed inside run() method i.e., we must override run() method. In order to start a thread, we must use start() method. After starting a thread, this thread will execute the statements that are present in the run() method. Lets implement the same factorial program using the runnable interface:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the factorial
// of a number by the implementation
// of threads using runnable interface
  
class ThreadImplement implements Runnable {
    static int fact = 1;
  
    // Overriding the run method
    // to find the factorial of
    // a number 5
    public void run()
    {
        // Loop to compute the factorial
        for (int i = 1; i <= 5; i++)
            fact = fact * i;
        System.out.println(fact);
    }
}
  
// Class to create a thread and
// compute the factorial
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating an object of the
        // thread class
        Thread t1
            = new Thread(new ThreadImplement());
  
        // Computing the above class
        t1.start();
    }
}

chevron_right


Output:

120

Multi-Threading in Java: 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.

In this problem, we need two threads, Thread t1 (produces the data) and Thread t2 (consumes the data). However, both the threads shouldn’t run simultaneously.

Below is the implementation of the producer-consumer problem:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement the
// producer consumer's problem
  
import java.lang.Thread;
  
// Producer class which extends the
// thread
class Producer extends Thread {
  
    // Creating a string buffer
    StringBuffer buffer;
    boolean dp = false;
  
    // Initializing the string buffer
    Producer()
    {
        buffer = new StringBuffer(4);
    }
  
    // Overriding the run method
    public void run()
    {
        synchronized (buffer)
        {
  
            // Adding the data into the
            // buffer
            for (int i = 0; i < 4; i++) {
                try {
                    buffer.append(i);
                    System.out.println("Produced " + i);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
  
            // Notifying the buffer
            System.out.println("Buffer is FUll");
            buffer.notify();
        }
    }
}
  
// Consumer class which extends
// the thread
class Consumer extends Thread {
  
    // Creating the object of the
    // producer class
    Producer p;
  
    // Assigning the object of the
    // producer class
    Consumer(Producer temp)
    {
        p = temp;
    }
  
    // Overriding the run method
    public void run()
    {
  
        // Controlling the access of the
        // buffer to the shared producer
        synchronized (p.buffer)
        {
            try {
                p.buffer.wait();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
  
            // Printing the values of the string buffer
            // and consuming the buffer
            for (int i = 0; i < 4; i++) {
                System.out.print(p.buffer.charAt(i) + " ");
            }
            System.out.println("\nBuffer is Empty");
        }
    }
}
  
// Main class to implement the
// Producer Consumer problem
class GFG {
    public static void main(String args[])
    {
        // Initially, there needs to be some data
        // in order to consume the data. So,
        // Producer object is created first
        Producer p = new Producer();
  
        // Sending this producer object
        // into the consumer
        Consumer c = new Consumer(p);
        Thread t1 = new Thread(p);
        Thread t2 = new Thread(c);
  
        // Since from the producer object,
        // we have already produced the data.
        // So, we start by consuming it.
        t2.start();
        t1.start();
    }
}

chevron_right


Output:

Produced 0
Produced 1
Produced 2
Produced 3
Buffer is FUll
0 1 2 3 
Buffer is Empty

Producer Consumer’s Problem without Synchronization: The above code is inefficient because the CPU resources are not being utilized in an efficient way. The threads are waiting for the buffer in the waiting state. Instead of this, we can utilize these threads in an efficient way by terminating them and creating them again. That is:

  • We create a thread to produce the data.
  • Once the buffer is full we will terminate that thread.
  • Create another thread to consume the data(at this point producer thread is dead).
  • Once the buffer is empty, consumer thread terminates and producer thread gets created and produces the data (at this point consumer thread is dead).

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement the
// producer consumer's problem
// without using synchronization
  
import java.lang.Thread;
  
// Producer class which extends the
// thread
class Producer extends Thread {
  
    // Creating a string buffer
    StringBuffer buffer;
  
    // Initializing the string buffer
    Producer()
    {
        buffer = new StringBuffer(4);
    }
  
    // Overriding the run method
    public void run()
    {
  
        // Loop to add data into the
        // buffer
        for (int i = 0; i < 4; i++) {
            try {
                buffer.append(i);
                System.out.println("Produced " + i);
            }
            catch (Exception e) {
  
                // Exception is returned when
                // the buffer is not accessible
                e.printStackTrace();
            }
        }
        System.out.println("Buffer is FUll");
  
        // Creating a consumer object after
        // execution of the above method.
        // Here, this keyword refers to
        // the current object of the
        // producer. This object is passed
        // into the consumer to access the
        // created buffer
        Consumer c = new Consumer(this);
    }
}
  
// Consumer class which extends
// the thread
class Consumer extends Thread {
    Producer p;
    Thread t2;
  
    // Constructor to get the
    // producer object
    Consumer(Producer temp)
    {
        p = temp;
  
        // Creating a new thread for
        // the object
        t2 = new Thread(this);
        t2.start();
    }
  
    // Overriding the run method
    public void run()
    {
        try {
  
            // Printing the string buffer and
            // consuming it
            for (int i = 0; i < 4; i++) {
                System.out.print(p.buffer.charAt(i) + " ");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\nBuffer is Empty");
    }
}
  
// Main class to implement the
// Producer Consumer problem
class Efficiency {
    public static void main(String args[])
    {
        // Producer object is created and
        // passed into the thread.
        Producer p = new Producer();
        Thread t1 = new Thread(p);
  
        // Here, instead of the same
        // thread waiting, a new thread
        // is created in the constructor
        // of the consumer class
        t1.start();
    }
}

chevron_right


Output:

Produced 0
Produced 1
Produced 2
Produced 3
Buffer is FUll
0 1 2 3 
Buffer is Empty

Modified Producer Consumer’s Problem: The above approach can further be improved because the same buffer is being used by the producer and the consumer. So, instead of using multiple threads, use one thread such that initially, the buffer is empty and thread that was created acts as a producer. Once the buffer is full, this thread acts as a consumer and consumes the data. However, we need to avoid deadlock.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement the
// producer consumer's problem
// using single thread
import java.lang.Thread;
  
// Producer class which extends the
// thread
class Producer extends Thread {
  
    // Creating a string buffer
    StringBuffer buffer;
  
    // Variable to avoid the deadlock
    boolean dp = false;
    Thread t1;
    Producer()
    {
        // Initializing the buffer
        buffer = new StringBuffer(4);
  
        // Creating a new thread with
        // the current object
        t1 = new Thread(this);
        t1.start();
    }
  
    // Overriding the run method
    public void run()
    {
  
        // Loop to produce the
        // data and add it to
        // the buffer
        for (int i = 0; i < 4; i++) {
            try {
                buffer.append(i);
                System.out.println("Produced " + i);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("Buffer is FUll");
  
        // Creating a consumer object
        // by passing the current
        // producer object
        Consumer c = new Consumer(this);
  
        // Reinitializing the thread
        // with the new value of the
        // consumer object
        t1 = new Thread(c);
        t1.start();
    }
}
  
// Consumer class which extends
// the thread
class Consumer extends Thread {
    Producer p;
  
    // Constructor to initialize
    // with the producer object
    Consumer(Producer temp)
    {
        p = temp;
    }
  
    // Overriding the run method
    public void run()
    {
        try {
  
            // Loop to print the buffer and
            // consume the values
            for (int i = 0; i < 4; i++) {
                System.out.print(p.buffer.charAt(i) + " ");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\nBuffer is Empty");
    }
}
  
// Main class to implement the
// Producer Consumer problem
class GFG {
    public static void main(String args[])
    {
  
        // Creating the object of the
        // producer
        Producer p = new Producer();
    }
}

chevron_right


Output:

Produced 0
Produced 1
Produced 2
Produced 3
Buffer is FUll
0 1 2 3 
Buffer is Empty

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.