java模拟系统进程算法的小程序

package PID;

public class ProBean {

 private String PID;
 private int time;
 private int priority;
 public String getPID() {
  return PID;
 }
 public void setPID(String pid) {
  PID = pid;
 }
 public int getTime() {
  return time;
 }
 public void setTime(int time) {
  this.time = time;
 }
 public int getPriority() {
  return priority;
 }
 public void setPriority(int priority) {
  this.priority = priority;
 }
 
}

package PID;

import java.util.ArrayList;
import java.util.List;

public class ProList {

   List<ProBean> list=new ArrayList<ProBean>();
  
   public boolean isE(String pid){
 for(int i=0;i<list.size();i++){
  if(pid.equals(list.get(i).getPID())){
   return true;
  }
 }  
 return false;
   
   }

  
}

package PID;

import java.util.List;
import java.util.Scanner;

public class ProTest {
    private static Scanner input=new Scanner(System.in);
    private static ProList pl=new ProList();
    //private static ProBean pb=new ProBean();new only one global object
   
 public static void main(String[]args){
  NewPro();
  
 }
 public static void NewPro(){
  System.out.println("请输入进程数:");
  int pnum=input.nextInt();
  for(int i=0;i<pnum;i++){
  System.out.println("请输入该进程的PID:");
  String pid=input.next();
  boolean isExist=pl.isE(pid);
  if(!isExist){
   System.out.println("请输入该进程的运行时间(秒):");
   int time=input.nextInt();
   System.out.println("请输入该进程的优先数:");
   int priority=input.nextInt();
   ProBean pb=new ProBean();
   pb.setPID(pid);
   pb.setTime(time);
   pb.setPriority(priority);
   
      List<ProBean> pl1=pl.list;
   pl1.add(pb); 
  }else{
  System.out.println("该PID的进程已经存在!"); 
  }

  }
  
     System.out.println("------------------------");
  System.out.println("请选择算法:");
  System.out.println("------------------------");
  System.out.println("1.先进先服务算法");
        System.out.println("2.优先级算法");
        int num1=input.nextInt();
        switch(num1){
       
        case 1:
         List<ProBean> list=pl.list;
         for(int j=0;j<list.size();j++){
         ProBean pb=list.get(j);
         String pname=pb.getPID();
         MyThread t=new MyThread(pname);
         t.start();
         
         try {
    t.sleep(pb.getTime()*1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
         System.out.println(pb.getTime()+"秒后结束..."+"\t"); 
   t.stop();
   if(!t.isAlive()){
    System.out.println("进程结束");
   }
         }
/*         List<ProBean> list=pl.list;
         ProBean pb1=list.get(0);
         String pt1=pb1.getPID();
         MyThread t1 = new MyThread(pt1);
         t1.start();
         System.out.println();
         try {
     t1.sleep(pb1.getTime()*1000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   System.out.print(pb1.getTime()+"秒后结束..."+"\t");
   t1.stop();
   System.out.println("进程结束");*/
   
/*    
    ProBean pb2=list.get(1);
             String pt2=pb2.getPID();
             MyThread t2 = new MyThread(pt2);
              t2.start();
              try {
         t2.sleep(pb2.getTime()*1000);
        } catch (InterruptedException e) {
         e.printStackTrace();
        }*/
         break;
        case 2:
         int temp=0;
         int time=0;
         List<ProBean> list2=pl.list;
         for(int i=0;i<list2.size()-1;i++){
             for(int j=0;j<list2.size()-i-1;j++){

              if(list2.get(j).getPriority()<list2.get(j+1).getPriority()){

               temp=list2.get(j+1).getPriority();
               String ptemp=list2.get(j+1).getPID();
               time=list2.get(j+1).getTime();
               
               list2.get(j+1).setPriority(list2.get(j).getPriority());
               list2.get(j).setPriority(temp);
               
               list2.get(j+1).setPID(list2.get(j).getPID());
               list2.get(j).setPID(ptemp);
               
               list2.get(j+1).setTime(list2.get(j).getTime());
               list2.get(j).setTime(time);
              }
             }
         }
         
         
         for(int i=0;i<list2.size();i++){
          //int pri=list2.get(i).getPriority();
          
          ProBean pb=list2.get(i);
             String pname=pb.getPID();
             MyThread t=new MyThread(pname);
             t.start();
             
             try {
        t.sleep(pb.getTime()*1000);
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
             System.out.println(pb.getTime()+"秒后结束..."+"\t"); 
       t.stop();
       if(!t.isAlive()){
        System.out.println("进程结束");
       }
         }
 
         break;
        }
 }
 
  static class MyThread extends Thread{
   boolean RUN=true;
    MyThread(String s){
     super(s);
    }
    public void run(){
       
    System.out.print("PID:"+getName()+"\t"+"正在运行...");
   }
  
 }
 
}

原文地址:https://www.cnblogs.com/archie2010/p/1945194.html