Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this part, you are going to implement the producer-consumer problem using thr

ID: 3816196 • Letter: I

Question

In this part, you are going to implement the producer-consumer problem using threads. After implementing, let the program run until at least 500 items are produced by the producer and at least 500 items are consumed by the consumer. You are going to create 2 producer threads and 1 consumer threads in the program. Repeat the experiment for the following settings: Make the queue size 10 Make the queue size 20 Make the queue size 30 For each setting, count the number of times the producer and consumer goes to sleep because of empty queue or full queue. Print the total number of times each goes to sleep at the end of the experiment. Include this in your report.

Explanation / Answer

// Program for Producer Consumer Problem with input as queue capacity and max items as 500

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class ProducerConsumerTest {
  
   static int producerWait = 0;
   static int consumerWait = 0;
   public static void main(String[] args) throws InterruptedException, NumberFormatException, IOException {
      
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter queue capacity : ");
       final int queueCapacity = Integer.parseInt(bufferedReader.readLine());
      
       final ProducerConsumer pc = new ProducerConsumer();

       // Create producer 1 thread
       Thread producer1 = new Thread(new Runnable() {
           @Override
           public void run() {
               try {
                   pc.produce(queueCapacity);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       });

       // Create producer 2 thread
       Thread producer2 = new Thread(new Runnable() {
           @Override
           public void run() {
               try {
                   pc.produce(queueCapacity);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       });

       // Create consumer 1 thread
       Thread consumer1 = new Thread(new Runnable() {
           @Override
           public void run() {
               try {
                   pc.consume();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       });

       // Start Producer and Consumer threads
       producer1.start();
       producer2.start();
       consumer1.start();

       producer1.join();
       producer2.join();
       consumer1.join();
         
       System.out.println("Producer wait : "+producerWait);
       System.out.println("Consumer wait : "+consumerWait);
       System.out.println("Total Wait : "+(producerWait+consumerWait));
   }
  
   // This class has a list, producer (produces/adds items to the list) and consumer (consumes/removes items from list).
   public static class ProducerConsumer {
       // Create a list shared by producer and consumer.
       LinkedList<Integer> queue = new LinkedList<>();

       // Function called by producer thread
       static int itemsProduced = 1;
       public void produce(int capacity) throws InterruptedException {
           while (itemsProduced < 500) {
               synchronized (this) {
                   // Producer waits till list is full
                   while (queue.size() == capacity) {
                       System.out.println("Producer waits because of full queue");
                       producerWait++;
                       wait();
                   }

                   System.out.println("Producer produced-" + itemsProduced);

                   // Produces/Inserts the item in the list
                   queue.add(itemsProduced++);

                   // Notifies the consumer thread that it can start consuming
                   notify();
               }
           }
       }

       public void consume() throws InterruptedException {
           int itemsConsumed = 1;
           while (itemsConsumed <= 500) {
               synchronized (this) {
                   // Consumer waits till list is empty
                   while (queue.isEmpty()) {
                       System.out.println("Consumer waits because of empty queue");
                       consumerWait++;
                       wait();
                   }

                   // Consume/Retrieve the first job in the list
                   itemsConsumed++;
                   int val = queue.removeFirst();

                   System.out.println("Consumer consumed-" + val);

                   // Wake up producer thread
                   notify();
               }
           }
       }
   }
}

Sample Output :

For queue capacity 10 :

Producer wait : 54
Consumer wait : 51
Total Wait : 105

For 20:

Producer wait : 25
Consumer wait : 25
Total Wait : 50

For 30:

Producer wait : 18
Consumer wait : 18
Total Wait : 36

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote