📄 lru.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package util;import java.awt.Color;import java.awt.Label;/** * * @author Administrator */public class LRU extends PageReplace{ int[] seq=null; Label[] label=null; int []onLabel=null; int in; //换进来页面 int still;//原来就有又进来的 int []come=null; double []efficiency=null; //换页率 double formFeed=0; //换页次数 String nextSeq = null; String lastSeq = null; Label next = null; Label last = null; Label efficiencyLabel = null; public LRU(int[]s,double[] eff,Label[] l,Label next,Label last,Label efficiencyLabel) { seq=s; label=new Label[l.length]; onLabel=new int[l.length]; come = new int[l.length]; efficiency= eff; this.next=next; this.last=last; this.efficiencyLabel = efficiencyLabel; for (int i = 0; i < l.length; i++) { label[i]= l[i]; onLabel[i]=0; come[i]=0; } char[] show = new char[40]; for (int i = 0,j=0; i < seq.length; i++) { show[j++] = (char)(seq[i]+48); show[j++] = ' '; } nextSeq = new String().valueOf(show); lastSeq = ""; } public void run() { next.setVisible(true); next.setText("待访问的页面编号为:"+nextSeq); last.setVisible(true); last.setText("已访问的页面编号为:"+lastSeq); efficiencyLabel.setVisible(true); int page=0; for (int i = 0; i < seq.length; i++) { try { in = -1; still = -1; if (page < label.length) { for (int j = 0; j < page; j++) { if (seq[i] == onLabel[j]) { still = j; } } if (still == -1) { formFeed++; in = page; onLabel[page] = seq[i]; page++; } calCome(page); } else { for (int j = 0; j < onLabel.length; j++) { if (seq[i] == onLabel[j]) { still = j; } } if (still == -1) { formFeed++; in = 0; for (int j = 1; j < label.length; j++) { if (come[j] > come[in]) { in = j; } } onLabel[in] = seq[i]; } calCome(); } efficiency[i] = (double) (formFeed/(i+1)); show(i); Thread.sleep(1000); } catch (Exception ex) {} } } private void calCome(int i) { for (int j = 0; j < i; j++) come[j]++; } private void calCome() { for (int j = 0; j < label.length; j++) come[j]++; if(in!=-1) come[in]=0; else come[still]=0; } private void show(int cur) { for (int i = 0; i < label.length; i++) { label[i].setText(Integer.toString(onLabel[i])); label[i].setBackground(Color.CYAN); } if(still!=-1) label[still].setBackground(Color.green); else label[in].setBackground(Color.red); lastSeq = lastSeq.concat(nextSeq.substring(0, 2)); nextSeq = nextSeq.substring(2); next.setText("待访问的页面编号为:"+nextSeq); last.setText("已访问的页面编号为:"+lastSeq); efficiencyLabel.setText("当前缺页率为:"+Double.toString(efficiency[cur])); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -