Question
This is a screenshot of what it could look like I need help with writing code for this hw assignment. This is java btw server
This is a screenshot of what it could look like I need help with writing code for this hw assignment. This is java btw
server java file. _________________________________
public class Server
{
public static void main(String args[]) {
BoundedBuffer server = new BoundedBuffer();
// now create the producer and consumer threads
Producer producerThread = new Producer(server);
Consumer consumerThread = new Consumer(server);
producerThread.start();
consumerThread.start();
}//main
}//class
_____________________ producer file.java
import java.util.*;
public class Producer extends Thread
{
public Producer(BoundedBuffer b) {
buffer = b;
}
public void run()
{
Date message;
while (true)
{
int sleeptime = (int) (BoundedBuffer.NAP_TIME * Math.random()) +1;
System.out.println("Producer sleeping for " + sleeptime + " seconds");
try { sleep(sleeptime*1000); }
catch(InterruptedException e) {}
// produce an item & enter it into the buffer
message = new Date();
System.out.println("Producer produced " + message);
buffer.enter(message);
}
}
private BoundedBuffer buffer;
} _______________________ consumer file.java
import java.util.*;
public class Consumer extends Thread
{
public Consumer(BoundedBuffer b)
{
buffer = b;
}
public void run()
{
Date message;
while (true)
{
int sleeptime = (int) (BoundedBuffer.NAP_TIME * Math.random()) +1;
System.out.println("Consumer sleeping for " + sleeptime + " seconds");
try { sleep(sleeptime*1000); }
catch(InterruptedException e) {}
// consume an item from the buffer
System.out.println("Consumer wants to consume.");
message = (Date)buffer.remove();
System.out.println("Consumer consumed."+message);
}
}
private BoundedBuffer buffer;
} ___________________________ bounded buffer.java
import java.util.*;
public class BoundedBuffer
{
public BoundedBuffer()
{
// buffer is initially empty
count = 0;
in = 0;
out = 0;
buffer = new Object[BUFFER_SIZE];
}
// producer calls this method
public void enter(Object item) {
while (count == BUFFER_SIZE)
; // do nothing
// add an item to the buffer
++count;
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
if (count == BUFFER_SIZE)
System.out.println(" Buffer FULL");
}
// consumer calls this method
public Object remove() {
Object item;
while (count == 0)
; // do nothing
// remove an item from the buffer
--count;
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
if (count == 0)
System.out.println(" Buffer EMPTY");
return item;
}
public static final int NAP_TIME = 2;
private static final int BUFFER_SIZE = 5;
private volatile int count;
private int in; // points to the next free position in the buffer
private int out; // points to the next full position in the buffer
private Object[] buffer;
}
Consumer Mary sleeping for 5 seconds Consumer Bert sleeping for 4 seconds Producer Liz sleeping for 7 seconds Producer John sleeping for 5 seconds Consumer Bert wants to consume Consumer Mary wants to consume Producer John produced 43941 Producer John sleeping for 4 seconds Buffer EMPTY Consumer Mary consumed 43941 Sum of its digits is: 21 Consumer Mary sleeping for 4 seconds Producer Liz produced 16041 Producer Liz sleeping for 5 seconds Buffer EMPTY Consumer Bert consumed 16041 Sum of its digits is: 12, Consumer Bert sleeping for 6 seconds Consumer Mary wants to consume Producer John produced 19382 Buffer EMPTY Producer John sleeping for 6 seconds Consumer Mary consumed 19382 Sum of its digits is: 23, Consumer Mary sleeping for 5 seconds Producer Liz produced 45673 Producer Liz sleeping for 4 seconds Consumer Bert wants to consume Buffer EMPTY Consumer Bert consumed 45673 Sum of its digits is: 25 Consumer Bert sleeping for 6 seconds Consider the shared memory version of the producer/consumer problem. Your task in this assignment is to create a multithreaded program to do the following: the Server class should create two producer threads identified as John and Liz"; it should create two consumer threads identified as Mary and Bert; John & Mary share a buffer; Liz & Bert share a different buffer. A producer thread repeats the following steps forever: Sleep for a random amount of time in the range 3 to 7 seconds) and then generate a non-negative random integer value in the range 8000...50000; then insert this value into the buffer. The corresponding consumer thread repeats the following steps forever: sleep for a random amount of time (in the range 4 to 6 seconds) and then remove a value from the buffer; determine the sum of digits in this value; output both the value & its sum of digits onto the screen. Thus, in your producer/consumer problem, whatever John produces Mary consumes & whatever Liz produces Bert consumes. For your convenience, I have provided you the solution given by the authors of our textbook for the producer/consumer problem based on shared buffer that you may edit/modify to do this assignment. (See the folder PG1xxx). Note that this folder includes the following files: Server.java, Producer.java, Consumer.java, & BoundedBuffer.java. You will rename the folder PG1xxx replacing xxx with your initials. Each file must include your name, course info, assignment #, date, and a brief description of the task done in that file. NOTE: 1. Your Java source files should be well-commented & well-structured 2. You must include your name, current date, & a description of the problem as comments 3. Electronic copy of the folder PGlxxx (zipped) must be posted by due date time to the appropriate location on Canvas. Post only the .zip type file. 4. You need to turn in a hardcopy of the source files (Server.java, Producer.java, BoundedBuffer.java, and Consumer.java) by the due date timeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started