进程调度算法之最高优先级(Java)

    这次是用String的一位数组加上list来实现的,上一次写短进程优先的时候,认为用date类太麻烦了。所以这次直接用String,没想到,还是一样的麻烦,因为我想要表示小时,分钟,秒数,三个时间单位,而不是两个时间单位。所以在从String转换成int,并进行计算的时候,比较麻烦。其中用到了简单的正则表达式,并将时间先转化成秒进行计算。

附上源码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Priority {
    
    private static String[] PCB = {"0进程名", "1运行状态", "2优先级", "3提交时间", "4需要运行时间", "5完成时间", "6周转时间", "7响应比", "8已执行次数"};
    //记录当前时间
    static String now = "00:00:00";
    //时间片
    static int timeSlice = 500;
    //用String一维数组先建立几个进程
    private static String[] process1 = {"process1", "0", "1", "00:08:20", "00:10:56", "", "", "", ""};
    private static String[] process2 = {"process2", "0", "3", "00:01:08", "01:00:02", "", "", "", ""};
    private static String[] process3 = {"process3", "0", "2", "00:01:01", "00:10:02", "", "", "", ""};
    private static String[] process4 = {"process4", "0", "4", "00:01:01", "00:05:02", "", "", "", ""};
    private static String[] process5 = {"process5", "0", "7", "00:01:08", "00:00:02", "", "", "", ""};
    private static String[] process6 = {"process6", "0", "9", "00:02:08", "00:30:02", "", "", "", ""};
    private static String[] process7 = {"process7", "0", "4", "00:03:08", "00:03:01", "", "", "", ""};
    private static String[] process8 = {"process8", "0", "2", "00:05:01", "00:00:02", "", "", "", ""};
    private static String[] process9 = {"process9", "0", "1", "00:00:13", "00:11:02", "", "", "", ""};
    
    public static void main(String[] args) {
        
        List<String[]> list = new ArrayList<String[]>();
        list.add(process1);
        list.add(process2);
        list.add(process3);
        list.add(process4);
        list.add(process5);
        list.add(process6);
        list.add(process7);
        list.add(process8);
        list.add(process9);
        System.out.println(PCB[0] + " " + PCB[1] + " " + PCB[2] + " " + PCB[3] + " " + PCB[4]
                + " " + PCB[5] + " " + PCB[6] + " " + PCB[7] + " " + PCB[8]);
        startUp(list);
        
    }
    
    //修复String格式转换为long之后再转换为String而造成的格式不正确
    public static String modify(String string) {
        int i = string.length();
                
        if (i == 0) {
            return "00:00:00";
        }
        
        if (i == 1) {
            return "00:00:0" + string;
        }
        
        if (i == 2) {
            return "00:00:" + string;
        }
        
        if (i == 3) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            return "00:0" + temp + ":" + temp1 + temp2;
        }
        
        if (i == 4) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            char temp3 = string.charAt(3);
            return "00:" + temp + temp1 + ":" + temp2 + temp3; 
        }
        
        if (i == 5) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            char temp3 = string.charAt(3);
            char temp4 = string.charAt(4);
            return "0" + temp  + ":" + temp1 + temp2 + ":" + temp3 + temp4;
        }
        
        if (i == 6) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            char temp3 = string.charAt(3);
            char temp4 = string.charAt(4);
            char temp5 = string.charAt(5);
            return  temp + temp1 + ":" + temp2 + temp3 + ":" + temp4 + temp5;
        }
    
        else {
            return "false";
        }
    }
    
    public static void outPut(List<String[]> list) {
        for (String[] strings : list) {
            System.out.println(strings[0] + " " + strings[1] + " " + strings[2] + " " + strings[3]
                    + " " + strings[4] + " " + strings[5] + " " + strings[6] + " "
                    + strings[7]);
    }
    }
    //进程刚开始的时候调用此方法
    public static void startUp(List<String[]> list) {
        Collections.sort(list, new sortBySubAndPri());
        String[] processTemp = list.get(0);
        processTemp[1] = "1";
        
        if (toSecond(processTemp[4].replaceAll("[-\\s:]","")) < timeSlice) {
        processTemp[5] = revString(toSecond(processTemp[3].replaceAll("[-\\s:]","")) 
                + toSecond(processTemp[4].replaceAll("[-\\s:]","")));
        processTemp[6] = processTemp[4];
        processTemp[7] = "1";
        list.remove(0);
        list.add(0, processTemp);
        outPut(list);
        
        now = list.get(0)[4];
        list.remove(0);
        System.out.println("第一次进程执行完毕");
        execute(list);
        }
        else {
            processTemp[1] = "1";
            processTemp[4] = revString(toSecond(processTemp[4].replaceAll("[-\\s:]","")) - 500);
            now = revString(toSecond(processTemp[3].replaceAll("[-\\s:]","")) + 500);
            processTemp[2] = String.valueOf(Integer.valueOf(processTemp[2]) + 1); 
            processTemp[8] = "1";
            list.remove(0);
            list.add(0, processTemp);
            for (String[] strings : list) {
                System.out.println(strings[0] + " " + strings[1] + " " + strings[2] + " " + strings[3]
                        + " " + strings[4] + " " + strings[5] + " " + strings[6] + " "
                        + strings[7]);
        }
            processTemp[1] = "0";
            System.out.println("第一次进程执行完毕");
            execute(list);
        }
  }
    
    public static void execute(List<String[]> list) {
        
        if (list.isEmpty()) {
            System.out.println("进程运行结束");
            System.exit(0);
        }
        
        List<String[]> newList = new ArrayList<>();
        for (String[] strings : list) {
            int x = toSecond(strings[3].replaceAll("[-\\s:]",""));
            int y = toSecond(now.replaceAll("[-\\s:]",""));
            if (x < y) {
                newList.add(strings);
            }
        }
        
        Collections.sort(newList, new sortByPri());
        String[] processTemp = newList.get(0);
        if (toSecond(processTemp[4].replaceAll("[-\\s:]","")) < timeSlice) {
            processTemp[1] = "1";
            processTemp[5] = revString(toSecond(now.replaceAll("[-\\s:]",""))
                    + toSecond(processTemp[4].replaceAll("[-\\s:]","")));
            
                processTemp[6] = revString(toSecond(processTemp[5].replaceAll("[-\\s:]",""))
                        - toSecond(processTemp[3].replaceAll("[-\\s:]","")));
                if (processTemp[8] != "") {
                    int temp = Integer.valueOf(processTemp[8]);
                    //processTemp[7] = revString(toSecond(processTemp[6].replaceAll("[-\\s:]",""))
                    //      /((toSecond(processTemp[4].replaceAll("[-\\s:]","")) + temp * 500)));
                    String xxx = processTemp[6].replaceAll("[-\\s:]","");
                    int x = toSecond(xxx);
                    int y = toSecond(processTemp[4].replaceAll("[-\\s:]","")) + temp * 500;
                    double z = (double)x/y;
                    processTemp[7] = String.valueOf(z);
                            
    
    
                }
                else {
                //  processTemp[7] = revString(toSecond(processTemp[6].replaceAll("[-\\s:]",""))
                //          /toSecond(processTemp[4].replaceAll("[-\\s:]","")));
                        int x = toSecond(processTemp[6].replaceAll("[-\\s:]",""));
                        int y = toSecond(processTemp[4].replaceAll("[-\\s:]",""));
                        double z = (double)x/y;
                        processTemp[7] = String.valueOf(z);
                }
                now = processTemp[5];
                System.out.println(processTemp[0] + " " + processTemp[1] + " " + processTemp[2] + " " + processTemp[3]
                        + " " + processTemp[4] + " " + processTemp[5] + " " + processTemp[6] + " "
                        + processTemp[7]);
                Iterator<String[]> iterator = list.iterator();
                while (iterator.hasNext()) {
                    String[] strings = (String[]) iterator.next();
                    if (strings[0] == processTemp[0]) {
                        iterator.remove();
                    }
                }
        }
        
        else {
            processTemp[1] = "1";
            processTemp[2] = String.valueOf(Integer.valueOf(processTemp[2]) + 1); 
            processTemp[4] = revString(toSecond(processTemp[4].replaceAll("[-\\s:]","")) - 500);
            processTemp[5] = "do not finished";
            processTemp[6] = "do not confirm";
            processTemp[7] = "do not confirm";
            if (processTemp[8] != "") {
                int temp = Integer.valueOf(processTemp[8]);
                processTemp[8] = String.valueOf(temp + 1);
            }
            else {
                processTemp[8] = "1";
            }
            
            System.out.println(processTemp[0] + " " + processTemp[1] + " " + processTemp[2] + " " + processTemp[3]
                    + " " + processTemp[4] + " " + processTemp[5] + " " + processTemp[6] + " "
                    + processTemp[7]);
            processTemp[1] = "0";
            
            
            String hehe = now.replaceAll("[-\\s:]","");
            int haha = toSecond(hehe);
            now = revString(haha + 500);
            Iterator<String[]> iterator = list.iterator();
            while (iterator.hasNext()) {
                String[] strings = (String[]) iterator.next();
                if (strings[0] == processTemp[0]) {
                    iterator.remove();
                }
            }
            list.add(processTemp);
            
        }
        execute(list);
    }
    
    //先将字符串转化成秒来进行比较
    //注意[1]和[2]不要转
    public static int toSecond(String string) {
        
        int len = string.length();
        
        if (len == 0) {
            return 0;
        }
        
        if (len == 1 || len == 2) {
            return Integer.valueOf(string);
        }
        
        if (len == 3) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            
            return (Integer.valueOf(temp) - 48) * 60 + (Integer.valueOf(temp1) - 48) * 10 + (Integer.valueOf(temp2) - 48);
        
        }
        
        if (len == 4) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            char temp3 = string.charAt(3);
            
            return (Integer.valueOf(temp) - 48) * 10 * 60 + (Integer.valueOf(temp1) - 48) * 60
                    + (Integer.valueOf(temp2) - 48) * 10 + (Integer.valueOf(temp3) - 48);
        }
        
        if (len == 5) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            char temp3 = string.charAt(3);
            char temp4 = string.charAt(4);

            return (Integer.valueOf(temp) - 48) * 60 * 10 * 60 + (Integer.valueOf(temp1) - 48) * 10 * 60
                    + (Integer.valueOf(temp2) - 48) * 60 + (Integer.valueOf(temp3) - 48) * 10 + (Integer.valueOf(temp4) - 48);
        }
        
        if (len == 6) {
            char temp = string.charAt(0);
            char temp1 = string.charAt(1);
            char temp2 = string.charAt(2);
            char temp3 = string.charAt(3);
            char temp4 = string.charAt(4);
            char temp5 = string.charAt(5);
            
            int i1 = (Integer.valueOf(temp) - 48) * 10 * 60 * 60;
            int i2 = (Integer.valueOf(temp1) - 48) * 60 * 60;
            int i3 = (Integer.valueOf(temp2) - 48) * 10 * 60;
            int i4 = (Integer.valueOf(temp3) - 48) * 60;
            int i5 = (Integer.valueOf(temp4) - 48) * 10;
            int i6 = (Integer.valueOf(temp5) - 48);
            
            
            
            return i1 + i2 + i3 + i4 + i5 + i6;
        }
        
        else {
            return 100000000; //如果都不是则说明出现了错误,则象征性的返回一个非常大的数字代表出错
        }
    }
    
    public static String revString(int a) {
        int secondTime, minuteTime, hourTime;
        String temp = "xxx";
        hourTime = a/3600;
        if (hourTime > 0) {
            minuteTime = (a - hourTime * 3600)/60;
        }
        
        else {
            minuteTime = a/60;
        }
        
        secondTime = a;
        
        if (hourTime > 0 && minuteTime > 0) {
            secondTime = a - minuteTime * 60 - hourTime * 3600;
        }
        
        if (hourTime == 0 && minuteTime > 0) {
            secondTime = a - minuteTime * 60;
        }
        
        //你这Bug我服,不写等于10的情况
        if (secondTime < 10 && minuteTime < 10) {
            temp = String.valueOf(hourTime) + "0" + String.valueOf(minuteTime) + "0" + String.valueOf(secondTime);                  
        }
        
        if (secondTime >= 10 && minuteTime < 10) {
            temp = String.valueOf(hourTime) + "0" + String.valueOf(minuteTime) + String.valueOf(secondTime);                
        }
        
        if (secondTime < 10 && minuteTime >= 10) {
            temp = String.valueOf(hourTime) + String.valueOf(minuteTime) + "0" + String.valueOf(secondTime);                
        }
        
        if (secondTime >= 10 && minuteTime >= 10) {
            temp = String.valueOf(hourTime) + String.valueOf(minuteTime) + String.valueOf(secondTime);                  
        }
        
        return modify(temp);
        
    }
}


class sortBySubAndPri implements Comparator<String[]>{
    
    
    @Override
    public int compare(String[] s1, String[] s2) {
        
        long longs1 = Priority.toSecond(s1[3].replaceAll("[-\\s:]","")); //用正则表达式处理格式问题
        long longs2 = Priority.toSecond(s2[3].replaceAll("[-\\s:]",""));
        
        int x = Integer.valueOf(s1[2]);
        int y = Integer.valueOf(s2[2]);
        
        if (longs1 != longs2) {
            return (int) (longs1 - longs2);
        }
        else {
            return y - x;
        }
        
    }
}


class sortByPri implements Comparator<String[]>{

    @Override
    public int compare(String[] o1, String[] o2) {
    
        int x = Integer.valueOf(o1[2]);
        int y = Integer.valueOf(o2[2]);
        return x - y;
    }
    
}

1286531-20180513153412213-618412707.png

转载于:https://www.cnblogs.com/DevLegal/p/9032292.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值