Question: write a multi - threaded Java program that does the following:Has a single Producer class object which is a thread and a single Consumer class
write a multithreaded Java program that does the following:Has a single Producer class object which is a thread and a single Consumer class which is also a thread. Create a class Packet. This class should contain a simple string. Create a class Buffer that internally uses some data structure to hold incoming packet objects it should have methods insertPkt and removePkt and should have methods to tell the user how many packets are in the Buffer at any time eg size. Make the Producer object at random time intervals, create Packet objects and place them in the Buffer object. Let the Buffer object keep growing until it reaches about packets, then start a Consumer thread that consumes packets from the Buffer.Have the producer sleep less time than the consumer say by a factor of so it produces more packets than the consumer consumes in a period of time.Start a second consumer and any more such that the consumers balance out the producer's packets. How many consumers did you find balanced the production?Now have the producer sleep more time than the consumers before going to consume from the Buffer. How often does a consumer block waiting for packets what is the time comparison between the producers' sleep time and consumers blocking time.Optional Extra Credit: Use any mechanism to synchronize the producer thread pushing packets into the Buffer and the consumer thread removing them such that the Buffer size always remains between packets. Note that all packets must be processed. Show how your objective is met by running the Producer and Consumer object for a long period of time and showing the Buffer size never drops below and never exceeds
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
