java优先级队列调度

1     {          return needtime;     }      public int getPri()     {          return pri;     }      public char getState()     {          return state;     }      public void setState(char state)     {          this.state = state;     } }  ReadCommand.java文件  import java.io.InputStream; import java.util.ArrayList; import java.util.Scanner; public class ReadCommand {      public static int command = 0;//1-继续执行   2-阻塞   3-唤醒           public static void getCommand()     {          print();          InputStream is = System.in;/*输入流*/          Scanner scan = new Scanner(is);/*从控制台输入*/         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()))/*判断是否有数据*/ 
2 
3             {                  System.out.println("请输入整数1、2、3");             }              command = Integer.parseInt(comString);             if (command == 1)             {                  int size = Test.list.size();                 CommProcess runCommProcess = Test.map.get(Test.list.get(size                         - count));                  while (runCommProcess.getNeedtime() == 0                         || runCommProcess.getState() == 'W'                         || runCommProcess.getState() == 'F')                 {                      count++;                      int temp = size - count;                     if (temp < 0)                     {                          System.out.println("没有可选的线程");                         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                 int size = Test.list.size();                 for (int i = 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)             {                  boolean target = false;                 int size = Test.list.size();                 for (int i = 0; i < size; i++)                 {                      CommProcess temp = Test.map.get(Test.list.get(i));                     if (temp.getState() == 'W')                     {                          target = true;                         break;                     }                 }                  if (target)                 {                      //权重重新选择                     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.getState() == 'W')                         {                              //不能直接设置,需要比较权重                             if (falg)                             {                                  falg = false;                                 temp.setState('E'); 
5                             }                             else                             {                                  temp.setState('R');                             }                         }                     }                 }                  print();             }         }     }      public static void print()    {          //打印各进程程状态          for (int i = 0; i < Test.list.size(); i++)         {              CommProcess 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-继续执行   2-阻塞   3-唤醒");     } }  Test.java文件  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)     { 
6      System.out.println("");       System.out.println("*********静态优先数调度算法演示*********");         Random random = new Random();         int priorityP1 = 15;          int needTimeP1 = random.nextInt(10)+1%5;          CommProcess commProcessP1 = new CommProcess(1, priorityP1, needTimeP1);/*实例化进程P1*/          map.put(priorityP1, commProcessP1);/*插入元素*/         list.add(priorityP1);         int priorityP2 = 10;          int needTimeP2 = random.nextInt(10)+1;          CommProcess commProcessP2 = new CommProcess(2, priorityP2, needTimeP2);/*实例化进程P2*/          map.put(priorityP2, commProcessP2);         list.add(priorityP2);                   int priorityP3 = 5;          int needTimeP3 = random.nextInt(10)+1;          CommProcess commProcessP3 = new CommProcess(3, priorityP3, needTimeP3);/*实例化进程P3*/          map.put(priorityP3, commProcessP3);         list.add(priorityP3);          Collections.sort(list); /*按从小到大排序*/         new Thread(new Runnable()         {              public void run()             {                  ReadCommand.getCommand();             }          }).start();     } }
View Code

 

posted @ 2015-09-10 02:17  贵重一  阅读(962)  评论(0编辑  收藏  举报