?? lru.java
字號:
import java.awt.Color;
public class LRU {
public static void main(String[] args) {
MyFrame myframe=new MyFrame();
//System.out.println(Thread.currentThread().getName());
AssCache ac=new AssCache();
int hittimes=0;
int loadtimes=0;
int replacetimes=0;
int state=0;//操作的狀態:1=hit;2=load;3=replace;
double hitrate=0;
/*try {//輸入數據要在15s之內完成,要不就會出錯
Thread.currentThread().sleep(15000);
}
catch(InterruptedException e){}*/
while(myframe.getmutex()==0) {//檢查是否按過按鈕
}
System.out.println(myframe.getInput());
String runseq;//運行序列
for(int i=0;i<myframe.getInput().length();i++) {//0
runseq=myframe.getInput().substring(i+1);
myframe.setJTextFieldString(runseq, 0);//設置訪問序列
char c=myframe.getInput().charAt(i);
myframe.setJTextFieldString(c+"", 9);
System.out.println((i+1)+" "+c);//test sentance
if(ac.ishit(c)) {//hit
System.out.println("Hit the Cache!!!");
ac.hitopt(c);
state=1;//修改狀態
hittimes++;//命中次數+1
}
else {
if(ac.isnotfull()) {
ac.place(c);//place
loadtimes++;//裝入次數+1
state=2;//修改狀態
System.out.println("Place the Cache");
}
else {
//System.out.println(ac.lruseq());//sequence number
ac.replace(c,ac.lruseq());//replace
replacetimes++;//替換次數+1
state=3;//修改狀態
System.out.println("Replace the Cache");
}
}
//顏色閃一下,代表哪個cache塊的內容有變化
myframe.setTextFieldColor(Color.pink,ac.findlocation(c)+1);
try {//小暫停2s
Thread.currentThread().sleep(2000);
}
catch(InterruptedException e){}
myframe.setTextFieldColor(Color.WHITE,ac.findlocation(c)+1);
//---------------------------------------------
//修改相應cache塊的內容
myframe.setJTextFieldString(ac.cache0bblock(),1);
myframe.setJTextFieldString(ac.cache1bblock(),2);
myframe.setJTextFieldString(ac.cache2bblock(),3);
myframe.setJTextFieldString(ac.cache3bblock(),4);
//System.out.println(ac.blocktoString());
//顯示命中次數,裝入次數,替換次數
myframe.setJTextFieldString(hittimes+"", 5);
myframe.setJTextFieldString(loadtimes+"", 6);
myframe.setJTextFieldString(replacetimes+"", 7);
try {//小暫停2s
Thread.currentThread().sleep(2000);
}
catch(InterruptedException e){}
}//0
hitrate=hittimes/(hittimes+loadtimes+replacetimes+0.0);//計算命中率
myframe.setJTextFieldString(hitrate+"", 8);//顯示命中率
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -