In: Computer Science
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer that satisfies the FIFO. There should be exactly one instance of the buffer. The producer and the consumers must reference the same buffer. 2. The producer is responsible for producing data items to be added to the buffer. If the buffer is full, the producer must wait for a consumer to consume at least one item before it can add a new item to the buffer. The producer is required to produce a given number of items. The item that the producer adds to the buffer is a random uppercase letter 'A' to 'Z', inclusive. When a producer successfully inserts an item in the buffer it should print the location of insertion and time when insertion occurs with nanosecond resolution, using this format (spaces matter): Producer inserted 'S' at index 0 at 323187855 ns 3. The consumer is responsible for consuming elements, generated by the producer, from the buffer. If the buffer is empty, the consumer must wait for the producer to add an item to the buffer. When a consumer successfully removes an item from the buffer it should print the location of removal and time when removal occurs with nanosecond resolution, using this format: Consumer consumed 'I' at index 7 at 324491579 ns 4. Implement the Producer and the Consumer as two separate functions (no multithreading). 5. The main driver (function) must do all necessary initialization, then it must iterate for a period of 10 seconds (not 10 iterations). 6. In each iteration, the main driver calls randomly either the Producer function or the Consumer function. Once either of these two functions is called, it should return as specified below: If the corresponding function (Producer/Consumer) is successful in Producing/Consuming an item (character), then the function will return normally. Otherwise (in case of the Buffer is Full/Empty and the one second period has expired), then the corresponding function should return to the main driver without Producing/Consuming and item. 7. Sample Output: Start time in nanosecond: 443727532 ns Producer inserted 'G' at index 0 at 443729161 ns Producer inserted 'Q' at index 1 at 443730194 ns Producer inserted 'R' at index 2 at 443761114 ns Consumer consumed 'G' at index 0 at 443763544 ns Consumer consumed 'Q' at index 1 at 443769914 ns Consumer consumed 'R' at index 2 at 443774734 ns Producer inserted 'E' at index 0 at 443776594 ns Consumer consumed 'E' at index 0 at 443783544 ns Producer inserted 'W' at index 1 at 443785334 ns Producer inserted 'U' at index 2 at 443787024 ns Consumer consumed 'W' at index 1 at 443789048 ns Consumer consumed 'U' at index 2 at 443790011 ns
1) Java code for producer consumer problem with bounded buffer:
import java.util.Date;
import java.util.concurrent.Semaphore;
public class Solution{
public static void main(String args[]) {
//instantiate (create) buffer shared by Producer & Consumer
Buffer sharedBuffer = new BoundedBuffer();
// create the producer and consumer threads
Thread producerThread = new Thread(new Producer(sharedBuffer));
Thread consumerThread = new Thread(new Consumer(sharedBuffer));
//start() method allocates memory for a new thread in the JVM,
//and calls the run() method
producerThread.start();
consumerThread.start();
}
}
class BoundedBuffer implements Buffer{
private static final int BUFFER_SIZE = 3; //max size of buffer array
private int count; //number of items currently in the buffer
private int in; // points to the next free position in the buffer
private int out; // points to the first filled position in the buffer
private Object[] buffer; //array of Objects
private Semaphore mutex; //provides limited access to the buffer (mutual exclusion)
private Semaphore empty; //keep track of the number of empty elements in the array
private Semaphore full; //keep track of the number of filled elements in the array
public BoundedBuffer(){
// buffer is initially empty
count = 0;
in = 0;
out = 0;
buffer = new Object[BUFFER_SIZE];
mutex = new Semaphore(1); //1 for mutual exclusion
empty = new Semaphore(BUFFER_SIZE); //array begins with all empty elements
full = new Semaphore(0); //array begins with no elements
}
// producer calls this method
public void insert(Object item) {
/*
while (count == BUFFER_SIZE){
// do nothing, if the buffer array cannot be used (because full)
}
*/
try{
empty.acquire(); //keep track of number of empty elements (value--)
//This provides synchronization for the producer,
//because this makes the producer stop running when buffer is full
mutex.acquire(); //mutual exclusion
}
catch (InterruptedException e) {
System.out.println("ERROR in insert(): " + e);
}
// add an item to the buffer
++count;
buffer[in] = item;
//modulus (%) is the remainder of a division
//for example, 0%3=0, 1%3=1, 2%3=2, 3%3=0, 4%3=1, 5%3=2
in = (in + 1) % BUFFER_SIZE;
//buffer information feedback
if (count == BUFFER_SIZE){
System.out.println("BUFFER FULL "
+ "Producer inserted \"" + item + "\" count=" + count + ", + "in=" + in + ", out=" + out);
}
else{
System.out.println("Producer inserted \"" + item + "\" count=" + count + ", "in=" + in + ", out=" + out);
}
mutex.release(); //mutual exclusion
full.release(); //keep track of number of elements (value++)
//If buffer was empty, then this wakes up the Consumer
}
// consumer calls this method
public Object remove() {
Object item=null;
/*
while (count == 0){
//if nothing in the buffer, then do nothing
//the buffer array cannot be used (because empty)
}
*/
try{
full.acquire(); //keep track of number of elements (value--)
//This provides synchronization for consumer,
//because this makes the Consumer stop running when buffer is empty
mutex.acquire(); //mutual exclusion
}
catch (InterruptedException e) {
System.out.println("ERROR in try(): " + e);
}
// remove an item from the buffer
--count;
item = buffer[out];
//modulus (%) is the remainder of a division
//for example, 0%3=0, 1%3=1, 2%3=2, 3%3=0, 4%3=1, 5%3=2
out = (out + 1) % BUFFER_SIZE;
//buffer information feedback
if (count == 0){
System.out.println("BUFFER EMPTY "
+ "Consumer removed \"" + item
+ "\" count=" + count + ", "
+ "in=" + in + ", out=" + out);
}
else{
System.out.println("Consumer removed \"" + item + "\" count=" + count + ", " + "in=" + in + ", out=" + out);
}
mutex.release(); //mutual exclusion
empty.release(); //keep track of number of empty elements (value++)
//if buffer was full, then this wakes up the Producer
return item;
}
}
class Producer implements Runnable{
private Buffer buffer;
public Producer(Buffer b) {
buffer = b;
}
public void run(){
Date message;
while (true) {
System.out.println("Producer napping");
SleepUtilities.nap();
// produce an item & enter it into the buffer
message = new Date();
System.out.println("Producer produced \"" + message + "\"");
buffer.insert(message);
}
}
}
class Consumer implements Runnable{
private Buffer buffer;
public Consumer(Buffer b) {
buffer = b;
}
public void run(){
Date message = null;
while (true){
System.out.println("Consumer napping");
SleepUtilities.nap();
// consume an item from the buffer
System.out.println("Consumer wants to consume");
message = (Date)buffer.remove();
System.out.println("Consumer consumed \"" + message + "\"");
}
}
}
class SleepUtilities{
private static final int NAP_TIME = 5; //max nap time in seconds
/**
* Nap between zero and NAP_TIME seconds.
*/
public static void nap() {
nap(NAP_TIME);
}
/**
* Nap between zero and duration seconds.
*/
public static void nap(int duration) {
int sleeptime = (int) (NAP_TIME * Math.random() );
System.out.println("Nap for " + sleeptime + " seconds");
//Causes the currently executing thread to sleep (cease execution)
//for the specified number of milliseconds,
//subject to the precision and accuracy of system timers and schedulers.
try { Thread.sleep(sleeptime*1000); }
catch (InterruptedException e) {
//method sleep() throws InterruptedException - if any thread has interrupted the current thread.
System.out.println("ERROR in nap(): " + e);
}
}
}