java實現頁面置換演算法
阿新 • • 發佈:2020-08-10
本文例項為大家分享了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","1","5","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","6","9","8",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","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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。