Flask接口签名sign原理与实例代码浅析
295
2022-11-28
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
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
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
// 内存块
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] = http://"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
boolean flag = false;
for(String lis : list){
if(lis.equals(str)){
flag = true;
}
}
daHzXLAxHGreturn flag;
}
/**
* 在List中查找是否有String,如果有返回下标, 否则返回 -1
* @param list
* @param str
* @return
*/
public static int findIndex(List
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;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~