1   1  /  1  页   跳转

求个JAVA原代码

求个JAVA原代码

请教下,如何用JAVA实现LRU(最近最少使用页面置换算法(Least Recently Used)) LFU(最近最不常用页面置换算法(Least Frequently Used)) LIFO 的页面置换算法啊?谢谢~
最后编辑2007-06-26 06:11:46
分享到:
gototop
 

你看看这个是不是你想用的...
import java.util.*;
public class TestReplacement
{
private final int ArraySize=20;
private int digitalArray[]=new int [ArraySize];

//private int digitalArray[]={1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6};

private static final int frameSize[]={1,2,3,4,5,6,7};
private static int errorCount=0;

Vector frame=new Vector();
   
public TestReplacement() {
  super();
  // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
  TestReplacement aT=new TestReplacement();
 
  aT.generateRandomDigit();
  aT.output();
 
  System.out.println("-------------Using FIFO--------------");
  System.out.println();
  for(int i=0;i<frameSize.length;i++){
  System.out.println("Frame size:  "+frameSize+"\n");
  aT.initFrameForFIFO(frameSize);
  aT.FIFOReplace(frameSize);
  System.out.println("Total errors found:  "+errorCount); 
  System.out.println("\n************************************\n");
 
  errorCount=0;
  }
  System.out.println();
  System.out.println("----------------Using LRU----------------");
  System.out.println();
  for(int i=0;i<frameSize.length;i++){
  System.out.println("Frame size:  "+frameSize+"\n");
  aT.initFrameForLRU(frameSize);
  aT.LRUReplace(frameSize);
  System.out.println("Total errors found:  "+errorCount);
  System.out.println("\n************************************\n");
  errorCount=0;
  }
}

public void generateRandomDigit(){
  for(int i=0;i<ArraySize;i++){
  digitalArray=(int)Math.round(Math.random()*9);
  }
}

public void output(){
  System.out.println("随机序列:");
  for(int i=0;i<ArraySize;i++){
  System.out.print(" "+digitalArray);
  }
  System.out.println();
}

public void initFrameForFIFO(int fS){
  frame.removeAllElements();
  for(int i=0;i<fS;i++){ 
  frame.addElement(new Couple(fS-i));
  }
}
public void initFrameForLRU(int fS){
  frame.removeAllElements();
  for(int i=0;i<fS;i++){ 
  frame.addElement(new Couple(0));
  }
}


public void LRUReplace(int fS){
        boolean findThesame=false;
        int pre=-1;//存放刚刚查找到的位置
        int flag=-1;
       
  for(int j=0;j<digitalArray.length;j++){
  boolean match=false;
  for(int i=0;i<fS;i++){ 
   
    if(((Couple)frame.elementAt(i)).value==digitalArray[j]){
    ((Couple)frame.elementAt(i)).time=0;
    match=true;//是否找到匹配
    System.out.println(j+": find "+((Couple)frame.elementAt(i)).value+" "+"at location "+i);
    System.out.println();   
   
    flag=i;
    break;   
    }
  }
 
  if(match==true&&flag!=pre){
    for(int i=0;i<fS;i++){
    if(i!=flag){
    ((Couple)frame.elementAt(i)).time--;
    }     
    }
    pre=flag;
  }
  else if(match==false){
    int temp=0;
    int index=0;
    for(int i=0;i<fS;i++){
    if(((Couple)frame.elementAt(i)).time<temp){
      temp=((Couple)frame.elementAt(i)).time;
      index=i;
    }
    }
    for(int i=0;i<fS;i++){
    if(i!=index){
      ((Couple)frame.elementAt(i)).time--;
    }
    else{
      ((Couple)frame.elementAt(i)).time=0;
      System.out.print(j+": replace "+((Couple)frame.elementAt(i)).value+" ");     
      System.out.print("at location "+index+" ");
      ((Couple)frame.elementAt(i)).value=digitalArray[j];
      System.out.println("with "+((Couple)frame.elementAt(i)).value);
      errorCount++;
      System.out.println("error count    "+errorCount);
      System.out.println();
    }
    }
  }
  } 
}
public void FIFOReplace(int fS){
  //boolean blank=true;//是否开始的已填充完
  int i=0;
  for(int j=0;j<digitalArray.length;j++){ 
  boolean match=false;
  for(i=0;i<fS;i++){
    if(((Couple)frame.elementAt(i)).value==digitalArray[j]){
    match=true;//是否找到匹配
    System.out.println(j+": find "+((Couple)frame.elementAt(i)).value+" "+"at location "+i);
    break;
    }
  }
  if(match==false){
    int temp=0;
    int index=-1;
    for(i=0;i<fS;i++){   
    if(((Couple)frame.elementAt(i)).time>temp){
      temp=((Couple)frame.elementAt(i)).time;
      index=i;
    }
    }
    for(i=0;i<fS;i++){
        if(i==index){
      System.out.print(j+": replace "+((Couple)frame.elementAt(i)).value+" ");     
      System.out.print("at location "+i+" ");
          ((Couple)frame.elementAt(i)).value=digitalArray[j];
          System.out.println("with "+((Couple)frame.elementAt(i)).value);
      ((Couple)frame.elementAt(i)).time=1;
        errorCount++;
        System.out.println("error count    "+errorCount);
        System.out.println();
      }
     
     
     
      else{
      ((Couple)frame.elementAt(i)).time++;
      }
   
   
   
    }
  }
 
 
  }
}

}

class Couple{
public int value=-1;
public int time=-1;
public Couple(int t){ 
  time=t; 
}
}
gototop
 

谢谢LS的,不过这程序在编译的时候出现了以下的错误,Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method initFrameForFIFO(int) in the type TestReplacement is not applicable for the arguments (int[])
The method FIFOReplace(int) in the type TestReplacement is not applicable for the arguments (int[])
The method initFrameForLRU(int) in the type TestReplacement is not applicable for the arguments (int[])
The method LRUReplace(int) in the type TestReplacement is not applicable for the arguments (int[])

请问是什么原因?
gototop
 

还有,请问temp=((Couple)frame.elementAt(i)).time中的((Couple)frame.elementAt(i)).time是什么意思?到底把什么值传给TEMP呢?这是对象的什么使用方法啊?
gototop
 
1   1  /  1  页   跳转
页面顶部
Powered by Discuz!NT