In this assignment you will learn more about PriorityQueues in Java. PriorityQueue class was introduced in Java 1.5 and its part of Java Collections Framework. PriorityQueue is an unbounded queue based on a priority heap and the elements of the priority queue are ordered by default in natural order. We can provide a Comparator for ordering at the time of instantiation of priority queue. The head of the priority queue is the least element based on the natural ordering or comparator based ordering, if there are multiple objects with same ordering, then it can poll any one of them randomly. When we poll the queue, it returns the head object from the queue.

To instantiate a PriorityQueue object of size 7:

Queue< Integer > integerPriorityQueue = new PriorityQueue< >(7);

If we added the following numbers to this queue: 75, 33, 16, 9,77, 25, 18

The queue will order them in their natural order. The following code:

for(int i=0;i<7;i++){
Integer in = integerPriorityQueue.remove();
System.out.println("Processing Integer:"+in);
}

Will print:

Processing Integer:9
Processing Integer:16
Processing Integer:18
Processing Integer:25
Processing Integer:33
Processing Integer:75
Processing Integer:77

Exercise

Use the class PriorityQueue to build a processor scheduler that receives multiple processes with variant priorities. The processor will ask the processor scheduler about the next process to serve. The scheduler should remove the process that has the highest priority. Once the processor is done, the processor asks the scheduler again about the next process to serve and so on. The program ends when all processes are served. For the sake of this assignment we will work on serving 10 processes.

You project should contain only 2 classes:

Class: Process

This class contains 2 data members:

  • ID - sequential number (first object id=1, second object id=2,..etc)
  • Priority - random number from 1 to 100 (higher number = higher priority)

Hint 1: You can implement an object counter using a static data member

Class: ProcessSchedulerTest

This class has the main method that will call two functions:

1. GenerateProcesses: This function will create 10 processes. Each process will be assigned a priority number that is a random number from 1 to 100. Each process object is added to the PriorityQueue. The information about the processes objects should be printed out as shown in the following Figure.

Hint 2: To generate a random number in Java you can use the Math.Random class. Code example: https://www.geeksforgeeks.org/generating-random-numbers-in-java/

2. ServerProcesses: This function represents the processor that serves the processes in the Priority Queue. The queue will serve the process that has the highest priority. The order of serving the processes should also be printed out as shown in the following Figure.

Figure 1: Sample Run see image.

Academic Honesty!
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.