The following sections of Java code are only a sketch and are not complete. Plea
ID: 3597254 • Letter: T
Question
The following sections of Java code are only a sketch and are not complete. Please complete the program in a way that implements the solution to the Producer-Consumer problem.
Implement the shared buffer as an array of type char. The Producer and Consumer objects must each display when they have accessed the shared buffer. You may simplify the problem by defining a buffer with a single text character. Please include comments to clarify implementation, and please show the output.
https://pastebin.com/LZR411pg
The producer is defined with the following class, which inherits the Java class Thread
https://pastebin.com/T24jccXZ
In a similar manner, the consumer is defined by the following class, which inherits (extends) the Java class Thread
https://pastebin.com/YiYiNkUx
the main class defines method main, which creates and intiializes the semaphore objects, creates the two thread objects (producer and consumer objects), and starts execution of the thread objects.
https://pastebin.com/E66JfG9f
Explanation / Answer
// Java program to implement solution of producer
// consumer problem.
import java.util.LinkedList;
// class ProducerConsumer definition
public class ProducerConsumerDriver
{
//main method definition
public static void main(String[] args) throws InterruptedException
{
// ProducteConsumer class object declared
final ProducteConsumer pc = new ProducteConsumer();
// Create a thread for producer method to run using anonymous class
Thread pr = new Thread(new Runnable()
{
//Overrides run method
public void run()
{
//try block begins
try
{
//Calls the producer method
pc.produceMethod();
}//End of try block
//Catch block to handle InterruptedException exception
catch(InterruptedException e)
{
e.printStackTrace();
}//End of catch block
}//End of run() method
});//End of anonymous class
// Create a thread for consumer method to run using anonymous class
Thread co = new Thread(new Runnable()
{
//Overrides run method
public void run()
{
//try block begins
try
{
//Calls the consumer method
pc.consumeMethod();
}//End of try block
//Catch block to handle InterruptedException exception
catch(InterruptedException e)
{
e.printStackTrace();
}//End of catch block
}//End of run() method
});//End of anonymous class
// Start both threads for producer and consumer
pr.start();
co.start();
// pr finishes before pc
pr.join();
co.join();
}//End of main method
// This class has a list, producer (adds items to list and consumer (removes items from the list).
public static class ProducteConsumer
{
// Create a list shared by producer and consumer
LinkedList<Integer> myList = new LinkedList<Integer>();
// Size of list is 2.
int capacity = 2;
// Function called by producer thread
public void produceMethod() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
// Producer thread waits while list is full
while (myList.size()==capacity)
wait();
//Displays the value produced
System.out.println("Producer produced -> " + value);
// Insert the data in the list
myList.add(value++);
// Notifies the consumer thread that now it can start consuming
notify();
// sleeps for 1000 microsecond
Thread.sleep(1000);
}//end of synchronized (this)
}//End of while
}//End of produceMethod()
// Function called by consumer thread
public void consumeMethod() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// Consumer thread waits while list is empty
while (myList.size()==0)
wait();
//Extracts the first data from the list
int data = myList.removeFirst();
//Displays the data consumed
System.out.println("Consumer consumed: " + data);
// Wake up producer thread
notify();
// sleep for 1000 microseconds
Thread.sleep(1000);
}//End of synchronized (this)
}//End of while
}//End of method
}//End of class ProducteConsumer
}//End of class ProducteConsumerDriver
Sample Run:
Producer produced -> 0
Consumer consumed: 0
Producer produced -> 1
Consumer consumed: 1
Producer produced -> 2
Consumer consumed: 2
Producer produced -> 3
Producer produced -> 4
Consumer consumed: 3
Consumer consumed: 4
Producer produced -> 5
Producer produced -> 6
Consumer consumed: 5
Consumer consumed: 6
Producer produced -> 7
Producer produced -> 8
Consumer consumed: 7
Consumer consumed: 8
Producer produced -> 9