1{returnNeedtime; } Public intGetpri () {returnpri } Public CharGetState () {returnState } Public voidSetState (CharState) { This. state = State; }} Readcommand.java FileImportJava.io.InputStream;ImportJava.util.ArrayList;ImportJava.util.Scanner; Public classReadcommand { Public Static intCommand = 0;//1-Continue execution 2-Block 3-wake public static void GetCommand () {print (); InputStream is = system.in;/* input stream */Scanner scan = new Scanner (IS);/* Input from console */int count = 1; Boolean taget = false; while (true) {if (Test.count = = 3) {return; } arraylist<string> commands = new arraylist<string> (); Commands.add ("1"); Commands.add ("2"); Commands.add ("3"); String comstring = ""; while (!commands.contains (comstring = Scan.next ()))/* Determine if there is data */2 3{System.out.println ("Please enter integers 1, 2, 3"); Command = Integer.parseint (comstring);if(Command = = 1) {intSize = Test.list.size (); Commprocess runcommprocess = Test.map.get (Test.list.get (Size-count)); while(Runcommprocess.getneedtime () = = 0 | | runcommprocess.getstate () = = ' W ' | | Runcommprocess.getstate () = = ' F ') {count++;inttemp = Size-count;if(Temp < 0) {System.out.println ("no optional Threads"); Taget =true; Count = 1; Break; } runcommprocess= Test.map.get (Test.list.get (temp)); }if(Taget) {Taget =false;Continue; } runcommprocess.run ();if(Runcommprocess.getneedtime () <= 0) {count++; } print ();if(Count > Size) {count = 1; } }if(Command = = 2) {count = 1; 4 intSize = Test.list.size (); for(inti = 0; i < size; i++) {Commprocess temp = Test.map.get (Test.list.get (i));if(Temp.getneedtime () > 0 && temp.getstate () = = ' E ') {temp.setstate (' W '); }} print (); }if(Command = = 3) {Booleantarget =false;intSize = Test.list.size (); for(inti = 0; i < size; i++) {Commprocess temp = Test.map.get (Test.list.get (i));if(temp.getstate () = = ' W ') {target =true; Break; } }if(target) {//weight re-select count = 1; Boolean falg = true; for (int i = Test.list.size ()-1; I >= 0; i--) {commprocess temp = Test. Map.get (Test.list.get (i)); if (temp.getstate () = = ' E ') {temp.setstate (' R '); } else if (Temp.getneedtime () > 0 && Temp.getsta Te () = = ' W ') {//cannot be set directly, you need to compare weights if (falg) {FAL g = false; Temp.setstate (' E '); 5}Else{temp.setstate (' R '); }}}} print (); } } } Public Static voidPrint () {// print each process state for (int i = 0; i < Test.list.size (); i++) {COMMP Rocess temp = Test.map.get (Test.list.get (i)); System.out.println ("P" +temp.getid () + "priority=" + temp.getpri () + "needtime= "+ temp.getneedtime () +" state= "+ temp.getstate ()); } System.out.println ("----------------------------------------------"); System.out.println ("1-Continue execution 2-Block 3-wake"); }} Test.java file import java.util.ArrayList; Import java.util.Collections; Import Java.util.HashMap; Import java.util.List; Import Java.util.Map; Import Java.util.Random; public class Test {public static int count = 0; public static Map<integer, commprocess> Map = new Hashmap<integer, commprocess> (); public static list<integer> List = new arraylist<integer> (); public static void Main (string[] args) {6System.out.println (""); System.out.println ("********* static priority number scheduling algorithm demo *********"); Random random =NewRandom ();intpriorityP1 = 15;intneedTimeP1 = Random.nextint (10) +1%5; Commprocess commProcessP1 =NewCommprocess (1, priorityP1, needTimeP1);/*instantiating a process P1*/Map.put (priorityP1, commProcessP1);/*inserting elements*/List.add (priorityP1);intPriorityP2 = 10;intneedTimeP2 = Random.nextint (10) +1; Commprocess commProcessP2 =NewCommprocess (2, priorityP2, needTimeP2);/*instantiating a process P2*/Map.put (priorityP2, commProcessP2); List.add (priorityP2);intPriorityP3 = 5;intNeedTimeP3 = Random.nextint (10) +1; Commprocess commProcessP3 =NewCommprocess (3, priorityP3, needTimeP3);/*instantiating a process P3*/Map.put (priorityP3, commProcessP3); List.add (priorityP3); Collections.sort (list);/*sort by from small to large*/ NewThread (NewRunnable () { Public voidRun () {Readcommand.getcommand (); }}). Start (); } }View Code
Java Priority queue Scheduling