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

 

转载于:https://www.cnblogs.com/guizhongyi/p/4796641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值