java OS时间片轮转调度基本算法实例源码讲解。
public class OSprocess{
private String name; //进程名
private OSprocess next;//指向下一个进程
private int run_time; //要求运行时间
private int allo_time;//已运行时间
private int req_time;//还需要运行时间
private char state;//状态
private int pri;//优先数
//构造方法2 时间片轮转方法
public OSprocess(String name, int run_time, char state, int allo_time) {
this.name = name;
this.run_time = run_time;
this.allo_time = allo_time;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public OSprocess getNext() {
return next;
}
public void setNext(OSprocess next) {
this.next = next;
}
public int getRun_time() {
return run_time;
}
public void setRun_time(int run_time) {
this.run_time = run_time;
}
public int getAllo_time() {
return allo_time;
}
public void setAllo_time(int allo_time) {
this.allo_time = allo_time;
}
public int getReq_time() {
return req_time;
}
public void setReq_time(int req_time) {
this.req_time = req_time;
}
public char getState() {
return state;
}
public void setState(char state) {
this.state = state;
}
public int getPri() {
return pri;
}
public void setPri(int pri) {
this.pri = pri;
}
}
public class OSTimeturn {
int timepiece=2;
OSprocess op1,op2,op3,op4,op5;
public void init(){
//初始化进跳跃的唇彩(String name, int run_time, char state, int allo_time)
op1 = new OSprocess(“q1″,2,’R',0);
op2 = new OSprocess(“q2″,3,’R',0);
op3 = new OSprocess(“q3″,1,’R',0);
op4 = new OSprocess(“q4″,2,’R',0);
op5 = new OSprocess(“q5″,4,’R',0);
op1.setNext(op2);
op2.setNext(op3);
op3.setNext(op4);
op4.setNext(op5);
op5.setNext(op1);
//添加到进程队列
OSprocess temp ;
while(!list.contains(temp)){
list.add(temp);
}
}
//更新进程队列
public int getNext(OSprocess current,ArrayList list){
OSprocess next = current.getNext();
//int t=(OSprocess)list.getposition(next);//指定元素的位置
int t=0;
while(!list.contains(next)){
next=next.getNext();
t=list.indexOf(next);
}
return t;
}
public void run(int curPosition){
OSprocess current = (OSprocess) list.get(curPosition);
if((current.getAllo_time()+timepiece)>current.getRun_time()){
}
else {
}
OSprocess temp = (OSprocess) list.remove(curPosition);//获得当前运行的进程
if(temp.getAllo_time()!=temp.getRun_time())
//判断已运行时间是否等于要求运行时间
list.add(temp);
else { //已运行时间==要求运行时间
}
if(list.size()>0){
for(int i=0;i
}
}else{
}
}
public void runProcess(){
OSprocess cur = (OSprocess) list.get(0);
this.run(0);
while(list.size()>0){
int a = this.getNext(cur,list);
this.run(a);
}
}
public static void main(String老迟到的白猫 args) {
OSTimeturn ost = new OSTimeturn();
ost.init();
ost.runProcess();
}
}