Question

In: Computer Science

Please do this in java program. In this assignment you are required to implement the Producer...

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

Solutions

Expert Solution

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);

}

}

}


Related Solutions

Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
In this project, you are required to write the java program “IO.java” to implement integer operations...
In this project, you are required to write the java program “IO.java” to implement integer operations “+”, “−”, “*”. Specifically, your program reads operands from a file named “input.txt” (which will be manually placed under the directory of the program) as strings. Then your program converts strings to integers, and computes the addition, subtraction, and multiplication of the operands. Finally, your program creates a file named “output.txt” (under the directory of the program) and writes the results to the file....
java program You are required to implement a Patient Information System for a clinic according to...
java program You are required to implement a Patient Information System for a clinic according to the following specifications: Class #1 PatientRecords (Tester class with main): This class contains a full list of patients (array list) and it provides the following functionalities: Add patient by ID-Number Print all records Print the number of patients based on specific attributes such as: disease name, gender. ***** See examples below for the input format (use console) Class #2 Patient: This class contains personal...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
Java-- For this homework you are required to implement a change calculator that will provide the...
Java-- For this homework you are required to implement a change calculator that will provide the change in the least amount of bills/coins. The application needs to start by asking the user about the purchase amount and the paid amount and then display the change as follows: Supposing the purchase amount is $4.34 and the paid amount is $20, the change should be: 1 x $10 bill 1 x $5 bill 2 x Quarter coin 1 x Dime coin 1...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT