时间片轮转算法调度实验

  • K9_813027
    了解作者
  • 9.9KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-23 04:47
    上传日期
用java实验的时间片轮转法调度实验,基于MVC框架,界面简单,功能完好,代码简单易懂~
OSPractice.rar
  • OSPractice
  • Exercise1
  • RRview.class
    2.8KB
  • TestCPUScheduling.class
    451B
  • BtnRunListener.java
    4.4KB
  • BtnRunListener.class
    4.7KB
  • ProcessVO.class
    3.3KB
  • ProcessVO.java
    2.9KB
  • TestCPUScheduling.java
    130B
  • RRview.java
    1.8KB
  • .project
    386B
  • .classpath
    226B
内容介绍
package Exercise1; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.Timer; public class BtnRunListener implements ActionListener { private RRview view; private Vector<ProcessVO>vector1=new Vector<ProcessVO>(); //dtModel中的向量 private Vector<ProcessVO>vector2=new Vector<ProcessVO>();//txtArea中的向量 private StringBuffer strb=new StringBuffer(""); //线程安全的可变字符序列,一个类似于 String 的字符串缓冲区 private int current_time=0; private int proNum; //规定的进程道数 private int finishNum; //完成的进程道数 private ProcessVO readyHead=new ProcessVO(); //jiuxu Timer timer=new Timer(100, this);//创建一个 Timer 并将初始延迟和事件间延迟初始化为100毫秒。 public BtnRunListener(RRview view) { this.view=view; } //获取vector1中的数据 public void getData(){ vector1.clear(); for(int i=0;i<RRview.dtModel.getRowCount();i++){ if(RRview.dtModel.getValueAt(i, 0)!=null){ vector1.addElement(new ProcessVO(RRview.dtModel.getValueAt(i, 0).toString(), Integer.parseInt( RRview.dtModel.getValueAt(i, 1).toString()), Integer.parseInt(RRview.dtModel.getValueAt(i, 2).toString()), Integer.parseInt(RRview.dtModel.getValueAt(i, 3).toString()) )); } } } //显示vector1中的数据 public void show(Vector<ProcessVO>vector){ int proNum=vector.size(); StringBuffer strbr=new StringBuffer(""); strbr.append(ProcessVO.getTitle()+"\n"); for(int i=0;i<proNum;i++){ strbr.append(vector.get(i).toString()+"\n"); } strbr.append("\n"); strb.append(strbr); } // 获取就绪队列 public ProcessVO getReadyQueue(Vector<ProcessVO>vector,int current_time){ int jobNum=vector.size(); //构造一个队列,然后遍历vector,使其中已到达并且未完成的进程在队列中根据其优先级从小到大一次排序 ProcessVO readyHead=new ProcessVO(); ProcessVO currentNode; for(int i=0;i<jobNum;i++){ //去掉已完成的和没有到达的进程 if(vector.get(i).getArrive_time()<=current_time&&vector.get(i).getServed_time()<vector.get(i).getServes_time()){ currentNode=readyHead.getNext(); boolean isAdded = false; if(readyHead.getNext() == null){ readyHead.setNext(vector.get(i)); currentNode = readyHead.getNext(); currentNode.setState(true); currentNode.setNext(null); continue; } if(currentNode.getPriority()>vector.get(i).getPriority()){ readyHead.setNext(vector.get(i)); readyHead.getNext().setNext(currentNode); continue; } while(currentNode.hasNext()){ if(currentNode.getNext().getPriority()<=vector.get(i).getPriority()){ currentNode=currentNode.getNext(); continue; } vector.get(i).setNext(currentNode.getNext()); currentNode.setNext(vector.get(i)); isAdded=true; break; } if(isAdded==false){ currentNode.setNext(vector.get(i)); currentNode.getNext().setNext(null); } } } return readyHead; } // 显示就绪队列 public void show(ProcessVO readyHead){ StringBuffer strQue=new StringBuffer("\t运行后的就绪队列:"); ProcessVO currentNode=readyHead.getNext(); if(currentNode!=null){ strQue.append(currentNode.getName()+" "); while (currentNode.hasNext()) { strQue.append(currentNode.getNext().getName()+" "); currentNode = currentNode.getNext(); } }else{ strQue.append("empty"); } strQue.append("\n"); strb.append(strQue); RRview.txtArea.setText(strb.toString()); } //事件监听 public void actionPerformed(ActionEvent e) { if(e.getSource()==RRview.btnRun){ getData(); current_time=0; finishNum=0; vector2=vector1; proNum=vector2.size(); show(vector2); readyHead=getReadyQueue(vector2, current_time); strb.append("时间:\t"+current_time+"\t"); show(readyHead); timer.start(); }else{ //timer事件响应:每次响应视为一个时间片 if(finishNum<proNum){ if(readyHead.hasNext()){ strb.append("时间:\t" + current_time + " - " + (current_time+1) +"\t运行进程 " + readyHead.getNext().getName()); }if(readyHead.getNext().run()){ finishNum++; } current_time++; readyHead=getReadyQueue(vector2,current_time); show(readyHead); }else{ strb.append("\n"); timer.stop(); show(vector2); } } } }
评论
    相关推荐