⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lru.java

📁 一个java实现的用于模拟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 + -