Question
The goal is to implement a multi-threaded request scheduler similar to how web servers schedule request. The problem specification is given below: Assume that request
The goal is to implement a multi-threaded request scheduler similar to how web servers schedule request. The problem specification is given below:
Assume that request scheduling involves a Master thread and N Slave threads. The master thread "listens" for requests and inserts it into a request queue. Slave threads wait for requests to arrive into the request queue and upon the arrival of a new request, any idle slave thread that is waiting for a request may take the request and process it.
Your goal is to implement this technique for dispatching requests using a bounded buffer producer-consumer framework. Assume that the request queue is a circular buffer of size N (i.e., an array of size N). The method involves a single producer, which is the master thread, and N consumers, which are the slave threads.
The master thread will sleep for a random short duration and produce a request. Each request has a sequentially increasing request ID and a randomly chosen request length (assign each new request a random length between 1 and M seconds). The master thread then inserts the request into the queue and goes back to sleep for a random short duration before it produces another request. Of course, if the request queue, which is a bounded buffer, is full, the master thread must wait before it can insert the request into the queue.
Each slave thread can be idle or busy. When a slave thread is idle, it acts as a consumer waiting for a new request in the request queue. After it consumes a request from the queue, the slave thread will be busy for a duration that is equal to the request length for that request. The busy state of a slave thread should be emulated by making the thread sleep for that duration. Upon completing the request, the slave thread goes back to idle thread and attempts to consume a pending request from the request queue. If the queue is empty, the slave thread must wait, like a consumer in the producer-consumer problem.
Implement the problem above using Java and Monitors. Run your program with a single producer (Master thread) and N consumers (slave threads). N should be a configurable parameter that you specify as input to your program. You can also specify other inputs such as M, the max duration of a request and parameter that indicate how long a producer should sleep before producing the next request. The master and slave threads should generate and consume requests as discussed above. Have the master and slave threads print useful messages that indicate their actions. For example, the producer should print messages such as
Producer: produced request ID n, length t seconds at time CURRENT-TIME Producer: sleeping for X seconds
The consumer should print messages such as Consumer i: assigned request ID n, processing request for the next t seconds, current time is CURRENT-TIME Consumer i: completed request ID n at time CURRENT-TIME
Please include the output in your answer.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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