java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java页面置换算法

java实现页面置换算法

作者:cnkeysky

这篇文章主要为大家详细介绍了java实现页面置换算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现页面置换算法的具体代码,供大家参考,具体内容如下

原理就不说了,直接上代码

FIFO

import java.util.ArrayList;
import java.util.List;

import utils.ListUtils;


/**
 * 
 * 
 * @author cnkeysky
 *
 */

public class FIFO {

 public void run() {
 String[] inputStr = {"1", "2", "3", "4", "2", "1", "2", "3", "5", "2", "3", "7", "6"};
 // 内存块
 int memory = 3;
 List<String> list = new ArrayList<>();
 for(int i = 0; i < inputStr.length; i++){
  if(i == 0){
  list.add(inputStr[i]);
  System.out.println("第"+ i +"次访问:\t\t" + ListUtils.listToString(list));
  }else {
  if(ListUtils.find(list, inputStr[i])){
   System.out.println("第" + i + "次" + "访问:\t\t" + ListUtils.listToString(list));
  }else{
   if(list.size() < memory){
   list.add(inputStr[i]);
   }else{
   list.remove(0);
   list.add(inputStr[i]);

   }
   System.out.println("第" + i + "次" + "访问:\t\t" + ListUtils.listToString(list));
  }
  }
 }
 }

}

LRU

import utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 最近最久未用置换算法
 * @author cnkeysky
 *
 */

public class LRU {

 public static void main(String[] args) {
 String[] inputStr = {"6", "7", "6", "5", "9", "6", "8", "9", "7", "6", "9", "6"};
 // 内存块
 int memory = 3;
 List<String> list = new ArrayList<>();
 for(int i = 0; i < inputStr.length; i++){
  if(i == 0){
  list.add(inputStr[i]);
  System.out.println("第"+ i +"次访问:\t\t" + ListUtils.listToString(list));
  }else {
  if(ListUtils.find(list, inputStr[i])){
   // 存在字符串,则获取该下标
   int index = ListUtils.findIndex(list, inputStr[i]);
   // 下标不位于栈顶时,且list大小不为1时
   if(!(list.get(list.size() - 1)).equals(inputStr[i]) && list.size() != 1) {
   String str = list.get(index);
   list.remove(index);
   list.add(str);
   }
   System.out.println("第" + i + "次" + "访问:\t\t" + ListUtils.listToString(list));
  }else{
   if(list.size()>= memory) {
   list.remove(0);
   list.add(inputStr[i]);
   System.out.println("第" + i + "次" + "访问:\t\t" + ListUtils.listToString(list));
   }else {
   list.add(inputStr[i]);
   System.out.println("第" + i + "次" + "访问:\t\t" + ListUtils.listToString(list));
   }
  }
  }
 }
 }
}

Clock

import java.util.ArrayList;
import java.util.List;

import utils.ListUtils;

/**
 * 
 * 
 * @author cnkeysky
 *
 */
public class Clock {

 public static void main(String[] args) {
 String[] inputStr = {"6", "7", "6", "5", "9", "6", "8", "9", "7", "6", "9", "6"};
 List<String> list = new ArrayList<>();
 // 内存块
 int memory = 3;
 // 缺页次数
 int count = 0;
 String[] clock = new String[memory];
 int indexNext = 0;
 int index = 0;
 // 初始化时钟
 for(int i = 0; i < memory; i++) {
  clock[i] = "0";
 }
 for(int i = 0; i < inputStr.length; i++) {
  int indexPre = 0;
  if (i == 0) {
  list.add(inputStr[i]);
  clock[indexNext] = "1";
  indexNext++;
  System.out.println("第"+ i +"次访问:\t\t" + ListUtils.listToString(list));
  }else {

  if(ListUtils.find(list, inputStr[i])) {
   indexPre = ListUtils.findIndex(list, inputStr[i]);
   if(clock[indexPre].equals("0")) {
   clock[indexPre] = "1";
   }
   count++;
   System.out.println("第"+ i +"次访问:\t\t" + ListUtils.listToString(list));
  }else {
   if(list.size() < memory) {
   list.add(inputStr[i]);
   clock[indexNext] = "1";
   indexNext++;
   System.out.println("第"+ i +"次访问:\t\t" + ListUtils.listToString(list));
   }else {
   index = ListUtils.findZero(indexNext, clock, memory);
   list.remove(index);
   list.add(index, inputStr[i]);
   clock[index] = "1";
   indexNext = index + 1;
   System.out.println("第"+ i +"次访问:\t\t" + ListUtils.listToString(list));
   }
  }
  }
  if(indexNext > memory - 1) {
  indexNext = Math.abs(memory - indexNext);
  }
 }
 System.out.println("缺页次数:" + (inputStr.length-count));
 }

}

工具类ListUtils

import java.util.List;

public class ListUtils {

 public ListUtils() {

 }

 /**
 * 输出
 * @param list 将List转为数组并输出, out: 2, 3, 4 
 * @return
 */
 public static String listToString(List list){

 StringBuffer content = new StringBuffer();
 for(int i = 0; i < list.size(); i++){
  content.append(list.get(i));
  if(i < list.size() - 1){
  content.append(",");
  }
 }
 return content.toString();
 }

 /**
 * 在list中查找是否有str
 * @param list
 * @param str
 * @return
 */
 public static boolean find(List<String> list, String str){
 boolean flag = false;
 for(String lis : list){
  if(lis.equals(str)){
  flag = true;
  }
 }
 return flag;
 }

 /**
 * 在List中查找是否有String,如果有返回下标, 否则返回 -1
 * @param list
 * @param str
 * @return
 */
 public static int findIndex(List<String> list, String str) {

 int index = 0;
 for(String lis : list) {
  if(lis.equals(str)) {
  return index;
  }
  index++;
 }
 return -1;
 }

 public static boolean clockJudge(String[] clock, int index) {
 if(clock[index].equals("0")) {
  return true;
 }
 return false;
 }
 /**
 * 
 * @param index 下标
 * @param clock 时钟
 * @param range 当前使用内存块
 * @return
 */
 public static int findZero(int index, String[] clock, int range) {

 while(true) {

  if(clock[index].equals("0")) {
  break;
  }else {
  clock[index] = "0";
  index++;
  if(index > range-1) {
   index = Math.abs(range - index);
  }
  }
 }
 return index;
 }

 /**
 * 在数组中查找是否存在该字符串
 * @param obj
 * @param str
 * @return
 */
 public static boolean strJudge(Object[] obj, String str) {
 boolean flag = false;
 if(obj == null) {
  return flag;
 }
 for(int i = 0; i < obj.length; i++) {
  if(str.equals(obj[i])) {
  flag = true;
  break;
  }
 }
 return flag;
 }

 /**
 * 获取二维数组中同一列的行的长度
 * @param str 数据
 * @param length 二维数组的列
 * @param memory 内存块
 * @return
 * 
 */

 public static int findNull(Object[][] str, int length, int memory) {

 int index = 0;
 if(str == null) {
  return -1;
 }
 for(int i = 0; i < memory; i++) {
  if(str[i][length] != null) {
  index = i;
  }
 }
 return index;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文