LRU页面置换算法模拟Java实现....

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class LRU {
 int blockCount;
 int seriaCount;

 int[] address;
 int[] stack;

 BufferedReader br;

 public static void main(String[] args) {

  // int address[] = { 1, 2,3, 2, 8, 4, 3, 9, 4, 1, 2, 4, 6, 8, 2 };
  LRU lru = new LRU();

  lru.init();
  lru.display();
  System.out.println("=====LRU演示算法结束=====");
  System.out.println("系统退出!!!");
  
 }

 public void init() {
  try {
   br = newBufferedReader(new InputStreamReader(System.in));
  } catch (Exception e) {
   e.printStackTrace();
   System.exit(0);
  }

  System.out.println("===LRU页面置换算法演示===");

  System.out.println("请输入物理块数:");
  blockCount = readInt();
  stack = newint[blockCount];

  System.out.println("请输入访问内存的块序列的个数:");
  seriaCount = readInt();

  System.out.println("请输入访问内存的" +seriaCount + "块序列,中间已空格分隔:");
  address = readIntArray();
 }

 public void display() {
  // int[] stack = newint[4];
  boolean flag;
  System.out.println("地址序列:");
  for (int m = 0; m< address.length; m++)
   System.out.print(address[m]+ " ");
  System.out.print("/n");
  System.out.println("逐次的交换情况:");
  for (int i = 0; i< address.length; i++) {
   int j =0;
   flag =false;
   int t, temp =address[i];

   while(stack[j] != address[i]) {
    t= stack[j];
    stack[j]= temp;
    temp= t;
    j++;
    if(temp == 0 || j == stack.length)
     break;
   }
   if (j< stack.length)
    stack[j]= temp;
   if (temp != 0&& j != stack.length)
    flag= true;
   try {
    java.lang.Thread.sleep(500);
   } catch(InterruptedException e) {
   }

   for (int m= 0; m < i - blockCount + 1; m++)
    System.out.print(" ");
   for (int m =0; m < stack.length; m++)
    System.out.print(stack[m]+ " ");
   if(flag)
    System.out.print("页面直接可用");
   System.out.print("/n");

  }

 }

 public int readInt() {
  try {
   String s =br.readLine();
   System.out.println(s);
   returnInteger.parseInt(s);
  } catch (Exception e) {
   System.out.println(e);
   return3;
  }
 }

 public int[] readIntArray() {
  try {
   String s =br.readLine();
   System.out.println(s);
   String tmp[]= s.split(" ");
   int value[] =new int[tmp.length];
   for (int i =0; i < value.length; i++)
    value[i]= Integer.parseInt(tmp[i]);
   returnvalue;
  } catch (Exception e) {
   System.out.println(e);
   returnnull;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值