码迷,mamicode.com
首页 > 系统相关 > 详细

进程调度

时间:2019-08-08 23:26:28      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:imp   sys   优先   print   scanner   java   compare   his   更改   

//进程一

public class Process1 {
    public  int name;                //进程ID名
    public  int priority;            //优先权数
    public  int cputime;             //进程需要的时间
    public  String state;             //进程状态
    

    //设置 process类的构造方法


    Process1(int i, int priority , int cputime , String state)
    {
        this.name = i;
        this.priority = priority;
        this.cputime = cputime;
        
        this.state = state;
    }
    public String toString() {
        return "进程名:" + name + "  优先级为:" + priority + "  需要运行的时间:" + cputime + "  进程状态:" + state;
    }
}

//进程二

public class Process2 {
    public int name;
    public int needTime;
    public int ocpTime;
    public String state;
    
    Process2(int i, int needTime , int ocpTime , String state)
    {
        this.name = i;
        this.needTime = needTime;
        this.ocpTime = ocpTime;    
        this.state = state;
    }
    
    public String toString() {
        return "进程名:" + name + "  需要时间片:" + needTime + "  已占用时间片:" + ocpTime + "  进程状态:" + state;
    }

}

//测试类

import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
import java.util.LinkedList;

public class ProcessTest {
    public static LinkedList<Process1> input1(){
        LinkedList<Process1> l = new LinkedList<Process1>();   
        System.out.println("要创建进程数:");
        Scanner s = new Scanner(System.in);
        int num = s.nextInt();
        for (int i = 1; i <= num; i++) {
            ThreadLocalRandom ran = ThreadLocalRandom.current();
            int a = ran.nextInt(10,20);
            int b = ran.nextInt(1,20);
            Process1 p = new Process1(i,a,b,"ready");  
            l.add(p);
        }    
        s.close();
        return l;
        
    }
    
    
    public static LinkedList<Process2> input2(){
        LinkedList<Process2> l = new LinkedList<Process2>();   
        System.out.println("要创建进程数:");
        Scanner s = new Scanner(System.in);
        int num = s.nextInt();
        for (int i = 1; i <= num; i++) {
            ThreadLocalRandom ran = ThreadLocalRandom.current();
            int a = ran.nextInt(10,20);
            Process2 p = new Process2(i,a,0,"ready");  //
            l.add(p);
        }    
        s.close();
        return l;
        
    }
    
    //比较器
    class PriorityComparator implements Comparator<Process1> {
        public int compare(Process1 p1, Process1 p2) {
            if (p1.priority < p2.priority) {
                return 1;
            } else if (p1.priority > p2.priority) {
                return -1;
            } else {
                return 0;
            }
        }
    }
    public void run1(){
        
        LinkedList<Process1> l = ProcessTest.input1();
        Collections.sort(l, new PriorityComparator());//排序
        
        for(Process1 p:l) {
        System.out.println(p);
        }
        
        System.out.println("===============================");
        
        
        while(!l.isEmpty()) {
            
            l.peek().state = "running";
            
            for(Process1 p:l) {
                System.out.println(p);
                }
            
            l.peek().priority -= 3;
            l.peek().cputime -= 1;
            l.peek().state = "ready";
            
            Collections.sort(l, new PriorityComparator());
                    
            
            System.out.println("===============================");
            
            for(int i = 0;i < l.size();i++) {
                if(l.get(i).cputime<=0) {
                    l.get(i).state = "finish";
                    
                    //System.out.println(l.get(i));
                    System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    
                    System.out.println("结束进程:"+l.get(i));
                    
                    System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    
                    l.remove(i);
                }
            }            
        }
        
        System.out.println("End");
        
    }
    
    public void run2() {
        LinkedList<Process2> l = ProcessTest.input2();
        for(Process2 p:l) {
            System.out.println(p);
            }
        
        System.out.println("===============================");
        
        while(!l.isEmpty()) {
            l.peek().state = "runnig";
            
            for(Process2 p:l) {
                System.out.println(p);
                }    
            
            l.peek().state = "ready";
            
            l.peek().needTime--;
            l.peek().ocpTime++;
            
            //更改队列元素顺序
            Process2 temp = l.get(0);
            for(int i =0;i < l.size()-1;i++) {
                l.set(i, l.get(i+1));
            }
            l.set(l.size()-1,temp );
            
            
            System.out.println("===============================");
            
            
            for(int i = 0;i < l.size();i++) {
                if(l.get(i).needTime<=0) {
                    l.get(i).state = "finish";
                    
                    //System.out.println(l.get(i));
                    System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    
                    System.out.println("结束进程:"+l.get(i));
                    
                    System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    
                    l.remove(i);
                }
            }            
        }
        
        System.out.println("END");
        
        
    }

    
    public static void main(String[] args){
        
        ProcessTest pro = new ProcessTest();
        System.out.println("输入“y”或“n”:");
        Scanner s = new Scanner(System.in);
        String cmd = s.next();
        if(cmd.equals("y")) {
            pro.run1();
        }else if(cmd.equals("n")){
            pro.run2();
        }else {
            System.out.println("输入非法,重新运行");
        }
        s.close();
    }

}

进程调度

标签:imp   sys   优先   print   scanner   java   compare   his   更改   

原文地址:https://www.cnblogs.com/cky-2907183182/p/11324192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!