You are to develop a Java program to simulate the creation and execution of prioritized processes. You will need to implement the following data types to support the simulation:
A node type to represent a process. The node will contain a process id, a priority and a time slice (in milliseconds). The time slice indicates how long the process will "run" (i.e. sleep).
A Min-Heap to store the process nodes so that a node with the lowest priority number is at the top of the heap ready to go when the next process is chosen. (NOTE: in many priority-based systems, priority 0 is the highest priority. IE the lower the number, the higher the priority). Because all of the threads in your program need to access this heap data structure, you must synchronize access to the operations on the heap that must not be interrupted before completion.
A runnable/thread type to "consume"/retrieve a node off the top of the heap. The thread will simulate execution by sleeping for the duration of the time slice.
When a process has completed its "execution," the thread reports the process id, its priority value, and the time when it completed. Use a single line for the report, and make sure that the time is precise enough to see sub-second execution times.
If there is no work available on the heap, the thread can go to sleep for a pre-determined idle time in milliseconds.
A second runnable/thread type to create and add processes to the heap. Let this thread wake up at random periodic intervals and add some new processes into the heap occasionally. [ this thread should wake up at least 3 times and add processes. It should not run forever ]
Your main program will create a single heap.
Your main program should spawn 2 instances of your consumer threads. The main program should pass a reference to the shared heap to all of its threads upon creation. Your consumer threads will pull the next available process off the heap and do their work.
Your main program should spawn the thread that adds processes to the heap.
Your program can detect that the producer thread is complete, and can then monitor the heap until it is empty. It can then notify the other idle consumer threads to exit.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.