Java实现FIFO、LRU、LFU、OPT页面置换算法
目录
- 题目要求
- 具体代码
题目要求
采用多道程序思想设计一个程序,模拟页存储管理地址变换的过程,可采用FIFO、LRU、LFU、OPT四页面置换算法。基本要求如下:
- 需要建立访问页表线程、访问快表线程、缺页中断处理线程、访问内存线程等,协同这些线程模拟完成地址变换的过程。
- 输入一个逻辑页面访问序列和随机产生逻辑页面访问序列,分别由四个算法完成页面置换;
- 能够设定驻留内存页面的个数、内存的存取时间、缺页中断的时间、快表的时间,并提供合理省缺值,可以暂停和继续系统的执行;
- 能够设定页号序列中逻辑页面个数和范围;
- 能够设定有快表和没有快表的运行模式;
- 提供良好图形界面,同时能够展示四个算法运行的结果;
- 给出每种页面置换算法每个页面的存取时间;
- 能够将每次的实验输入和实验结果存储起来,随时可查询;
- 完成多次不同设置的实验,总结实验数据,看看能得出什么结论。
github地址
运行界面
执行界面
保存界面
历史界面
具体代码
四种页面置换算法具体实现
config
package com.lr.algurithm; public class config { public static int page_low = 0; public static int page_high = 0; public static int page_list_low = 0; public static int page_list_high = 0; public static int run_time = 0; public static int quick_table_num = 0;//快表大小 public static int memory_num = 0;//内存块数 public static double memory_Access = 0;//内存存取时间 public static double quick_table_access = 0;//快表存取时间 public static double miss_page_interrupt = 0;//缺页中断处理时间 public static int taskSize = 0;//页面序列数量 public static int[] task = new int[taskSize];//页面序列存放 public static boolean is_check=false;//快表启用判断 }
FIFO算法
FIFO
package com.lr.algurithm; import com.lr.frame.Page; import sun.awt.Mutex; import Java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; public class FIFO extends Thread{ private int fifo_i = 0; public int fifo_lackpage=0;//缺页数 public double fifo_ave_time=0;//平均时间 public double fifo_total_time=0;//总时间 public List<Integer> fifo_memory_page = new ArrayList<>();//内存现有页数存放 public List<Integer> fifo_quick_table_page = new ArrayList<>();//快表现有页数存放 public Mutex pause = new Mutex(); public String snumber = new String(); public String interruption = new String(); public String totalTime = new String(); public String avrTime = new String(); public String missPage = new String(); public double start(int a, double per_time) { if (!config.is_check) { //无快表模式 if (!fifo_memory_page.contains(a)) { //不在内存,产生缺页中断 fifo_lackpage ++ ; if (fifo_memory_page.size() < config.memory_num) fifo_memory_page.add(a); else { fifo_memory_page.remove(0); fifo_memory_page.add(a); } fifo_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { fifo_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //快表模式 if (!fifo_quick_table_page.contains(a)) { //不在快表 if (!fifo_memory_page.contains(a)) { //不在内存,产生缺页中断 fifo_lackpage ++ ; if (fifo_memory_page.size() < config.memory_num) //内存块未满,直接调入 fifo_memory_page.add(a); else { fifo_memory_page.remove(0); fifo_memory_page.add(a); } //快表置换用FIFO if (fifo_quick_table_page.size() < config.quick_table_num) fifo_quick_table_page.add(a); else { fifo_quick_table_page.remove(0); fifo_quick_table_page.add(a); } fifo_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { //在内存 fifo_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //在快表 fifo_total_time += config.quick_table_access + config.memory_access; per_time += config.quick_table_access + config.memory_access;; } } return per_time; } @Override public void run() { super.run(); for(fifo_i = 0; fifo_i < config.taskSize; fifo_i ++ ) { pause.lock(); double per_time = 0; per_time = start(config.task[fifo_i], per_time); String str1="查找"; String s1 = String.valueOf(config.task[fifo_i]); snumber += str1; snumber += s1; snumber += ":"; for (int j = 0; j < config.memory_num; j ++ ) { if (j < fifo_memory_page.size()) { String s = String.valueOf(fifo_memory_page.get(j)); snumber += s; snumber += " "; } else { snumber += " "; } } pause.unlock(); BigDecimal bd = new BigDecimal(per_time); bd = bd.setScale(2, RoundingMode.HALF_UP); snumber += "耗费时间:" + bd + "\n"; Page.FIFOtest.append(snumber); snumber = ""; try { sleep(config.run_time);//睡眠300ms } catch (InterruptedException e) { e.printStackTrace(); } } String s1 = String.valueOf(config.taskSize); s1 = String.valueOf(fifo_lackpage); interruption = s1 + "次"; // emit fifo_updata(s1+"次","interrupt_label"); double d = (double)fifo_lackpage / config.taskSize * 100; BigDecimal bd = new BigDecimal(d); bd = bd.setScale(2, RoundingMode.HALF_UP); totalTime = String.valueOf(fifo_total_time); fifo_ave_time = fifo_total_time / config.taskSize; BigDecimal bda = new BigDecimal(fifo_ave_time); bda = bda.setScale(2, RoundingMode.HALF_UP); avrTime = String.valueOf(bda); missPage = bd + "%"; Page.fifo_interrupt_label.setText(interruption); Page.fifo_total_time_label.setText(totalTime); Page.fifo_ave_time_label.setText(avrTime); Page.fifo_miss_page_label.setText(missPage); // System.out.println("fifo:"+s1); // System.out.println("fifo:"+fifo_total_time); // System.out.println("fifo:"+fifo_ave_time); // System.out.println("fifo:"+d + "%"); } }
LRU算法
LRU
package com.lr.algurithm; import com.lr.frame.Page; import sun.awt.Mutex; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; public class LRU extends Thread { private int lru_i = 0; public int lru_lackpage=0;//缺页数 public double lru_ave_time=0;//平均时间 public double lru_total_time=0;//总时间 public List<Integer> lru_memory_page = new ArrayList<>();//内存现有页数存放 public List<Integer> lru_quick_table_page = new ArrayList<>();//快表现有页数存放 public Mutex pause = new Mutex(); public String snumber = new String(); public String interruption = new String(); public String totalTime = new String(); public String avrTime = new String(); public String missPage = new String(); public double start(int a, double per_time) { if(!config.is_check) { //无快表模式 if(!lru_memory_page.contains(a)) { //不在内存,产生缺页中断 lru_lackpage ++ ; if(lru_memory_page.size() < config.memory_num) { lru_memory_page.add(a); } else { lru_memory_page.remove(0); lru_memory_page.add(a); } lru_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { //在内存中则找出这个元素并删除,在末尾添加这个元素,保证最近访问的置于末尾 // if (a < lru_memory_page.size()) int temp = 0; for (int i = 0; i < lru_memory_page.size(); ++ i) { if (a == lru_memory_page.get(i)) temp = i; } lru_memory_page.remove(temp); // System.out.println(a); lru_memory_page.add(a); lru_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //快表模式 if(!lru_quick_table_page.contains(a)) { //不在快表 if(!lru_memory_page.contains(a)) { //不在内存,产生缺页中断 lru_lackpage ++ ; if(lru_memory_page.size() < config.memory_num) { //内存块未满,直接调入 lru_memory_page.add(a); } else { lru_memory_page.remove(0); lru_memory_page.add(a); } //快表置换用FIFO if(lru_quick_table_page.size() < config.quick_table_num) { lru_quick_table_page.add(a); } else { lru_quick_table_page.remove(0); lru_quick_table_page.add(a); } lru_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { //在内存 int temp = 0; for (int i = 0; i < lru_memory_page.size(); ++ i) { if (a == lru_memory_page.get(i)) temp = i; } lru_memory_page.remove(temp); lru_memory_page.add(a); lru_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //在快表 int temp = 0; for (int i = 0; i < lru_memory_page.size(); ++ i) { if (a == lru_memory_page.get(i)) temp = i; } lru_memory_page.remove(temp); lru_memory_page.add(a); lru_total_time += config.quick_table_access + config.memory_access; per_time += config.quick_table_access + config.memory_access; } } return per_time; } @Override public void run() { for (lru_i = 0; lru_i < config.taskSize; lru_i ++ ) { pause.lock(); double per_time = 0; per_time = start(config.task[lru_i],per_time); String str1="查找"; String s1 = String.valueOf(config.task[lru_i]); snumber += str1; snumber += s1; snumber += ":"; for (int j = 0; j < config.memory_num; j ++ ) { if (j < lru_memory_page.size()) { String s = String.valueOf(lru_memory_page.get(j)); snumber+=s; snumber+=" "; } else { snumber+=" "; } } pause.unlock(); BigDecimal bd = newpython BigDecimal(per_time); bd = bd.setScale(2, RoundingMode.HALF_UP); snumber += "耗费时间:" + bd.toString() + "\n"; Page.LRUtest.append(snumber); snumber = ""; try { sleep(config.run_time); } catch (InterruptedException e) { e.printStackTrace(); } } String s1 = String.valueOf(config.taskSize); s1 = String.valueOf(lru_lackpage); interruption = s1 + "次"; double d = (double)lru_lackpage / config.taskSize * 100; BigDecimal bd =编程客栈 new BigDecimal(d); bd = bd.setScale(2, RoundingMode.HALF_UP); lru_ave_time = (double)lru_total_time / config.taskSize; totalTime = String.valueOf(lru_total_time); BigDecimal bda = new BigDecimal(lru_ave_time); bda = bda.setScale(2, RoundingMode.HALF_UP); avrTime = String.valueOf(bda); missPage = bd + "%"; Page.lru_interrupt_label.setText(interruption); Page.lru_total_time_label.setText(totalTime); Page.lru_ave_time_label.setText(avrTime); Page.lru_miss_page_label.setText(missPage); // System.out.println("lru"+s1); // System.out.println("lru"+lru_total_time); // System.out.println("lru"+lru_ave_time); // System.out.println("lru"+d + "%"); } }
LFU算法
LFU
package com.lr.algurithm; import com.lr.frame.Page; import com.lr.tool.Node; import sun.awt.Mutex; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; public class LFU extends Thread { private int lfu_i = 0; public int lfu_lackpage=0;//缺页数 public double lfu_ave_time=0;//平均时间 public double lfu_total_time=0;//总时间 public List<Node> lfu_memory_page = new ArrayList<>();//内存现有页数存放 public List<Integer> lfu_quick_table_page = new ArrayList<>();//快表现有页数存放 public Mutex pause = new Mutex(); public String snumber = new String(); public String interruption = new String(); public String totalTime = new String(); public String avrTime = new String(); public String missPage = new String(); public double start(int a, double per_time) { if(!config.is_check) { //无快表模式 boolean flag = true; int index = 0; for (Node map:lfu_memory_page) { if(map.key == a) { flag = false; break; } index ++ ; } if (flag) { //不在内存 lfu_lackpage ++ ; //内存满,删去访问频率最小的那个,相等的频率按fifo删除 if(lfu_memory_page.size() >= config.memory_num) { Node min_obj; min_obj = lfu_memory_page.get(0); for (Node pair : lfu_memory_page) { if (pair.value < min_obj.value) { min_obj = pair; } } lfu_memory_page.remove(min_obj); } lfu_memory_page.add(new Node(a)); lfu_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { lfu_memory_page.get(index).value++; lfu_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //快表模式 if(!lfu_quick_table_page.contains(a))//不在快表 { boolean flag = true; int index = 0; for (Node map : lfu_memory_page) { if (map.key == a) { flag = false; break; } index ++ ; } if (flag) { //不在内存 lfu_lackpage ++ ; //内存满,删去访问频率最小的那个,相等的频率按lfu删除 if (lfu_memory_page.size() >= config.memory_num) { Node min_obj; min_obj = lfu_memory_page.get(0); for (Node map : lfu_memory_page) { if(map.value < min_obj.value) { min_obj = map; } } lfu_memory_page.remove(min_obj); } lfu_memory_page.add(new Node(a)); //快表置换用FIFO if (lfu_quick_table_page.size() < config.quick_table_num) { lfu_quick_table_page.add(a); } else { lfu_quick_table_page.remove(0); lfu_quick_table_page.add(a); } lfu_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { //在内存 lfu_memory_page.get(index).value++; lfu_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //在快表 int index = 0; for (Node map : lfu_memory_page) { if(map.key == a) { lfu_memory_page.get(index).value++; break; } index++; } lfu_total_time += config.quick_table_access + config.memory_access; per_time += config.quick_table_access + config.memory_access; } } return per_time; } @Override public void run() { for(lfu_i = 0; lfu_i < config.taskSize; lfu_i ++ ) { pause.lock(); double per_time = 0; per_time = start(config.task[lfu_i], per_time); String str1 = "查找"; String s1 = String.valueOf(config.task[lfu_i]); snumber += str1; snumber += s1; snumber += ":"; for (int j = 0; j < config.memory_num; j ++) { if (j < lfu_memory_page.size()) { String s = String.valueOf(lfu_memory_page.get(j).key); snumber+=s; snumber+=" "; } else { snumber+=" "; } } pause.unlock(); BigDecimal bd = new BigDecimal(per_time); bd = bd.setScale(2, RoundingMode.HALF_UP); snumber += "耗费时间:" + bd + "\n"; Page.LFUtest.append(snumber); snumber = ""; try { sleep(config.run_time); } catch (InterruptedException e) { e.printStackTrace(); } } String s1 = String.valueOf(config.taskSize); s1 = String.valueOf(lfu_lackpage); interruption = s1 + "次"; double d = (double)lfu_lackpage / config.taskSize * 100; BigDecimal bd = new BigDecimal(d); bd = bd.setScale(2, RoundingMode.HALF_UP); lfu_ave_time = (double)lfu_total_time / config.taskSize; totalTime = String.valueOf(lfu_total_time); BigDecimal bda = new BigDecimal(lfu_ave_time); bda = bda.setScale(2, RoundingMode.HALF_UP); avrTime = String.valueOf(bda); missPage = bd + "%"; Page.lfu_interrupt_label.setText(interruption); Page.lfu_total_time_label.setText(totalTime); Page.lfu_ave_time_label.setText(avrTime); Page.lfu_miss_page_label.setText(missPage); // System.out.println("lfu"+s1); // System.out.println("lfu"+lfu_total_time); // System.out.println("lfu"+lfu_ave_time); // System.out.println("lfu"+d + "%"); } }
Node
package com.lr.tool; public class Node { public int key; public int value; public Node() { key = 0; value = 0; } public Node(int Key) { key = Key; value = 1; } public Node(int a, int i) { } }
OPT算法
OPT
package com.lr.algurithm; import com.lr.frame.Page; import sun.awt.Mutex; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class OPT extends Thread { private int opt_i = 0; public int opt_lackpage=0;//缺页数 public double opt_ave_time=0;//平均时间 public double opt_total_time=0;//总时间 public List<Integer> opt_memory_page = new ArrayList<>();//内存现有页数存放 // public int[] opt_memory_page = new int[config.taskSize]; public List<Integer> opt_quick_table_page = new ArrayList<>();//快表现有页数存放 public Mutex pause = new Mutex(); public String snumber = new String(); public String interruption = new String(); public String totalTime = new String(); public String avrTime = new String(); publicwww.devze.com String missPage = new String(); public double start(int a, double per_time) { if (!config.is_check) { //无快表模式 if (!opt_memory_page.contains(a)) { //不在内存,产生缺页中断 opt_lackpage ++ ; if (opt_memory_page.size() < config.memory_num) { opt_memory_page.add(a); } else { //遍历之后所有页号,与内存内页号相同则移到尾部,最后删除头部 Map<Integer, Boolean> has_move = new HashMap<>();//移动过的不能再移动 int cnt = 0;//移动次数要小于内存块数 for (int j = 0; j < config.taskSize; j ++ ) { has_move.put(config.task[j], false); } for (int j = opt_i + 1; j < config.taskSize; j ++ ) { //从需要进入内存的页号的后一位开始 if(opt_memory_page.contains(config.task[j]) && has_move.get(config.task[j]) != true && cnt < config.memory_num - 1) { int temp = 0; for (int i = 0; i < opt_memory_page.size(); ++ i) { if (config.task[j] == opt_memory_page.get(i)) temp 开发者_JAVA学习= i; } opt_memory_page.remove(temp); opt_memory_page.add(config.task[j]); has_move.put(config.task[j], true); cnt ++ ; } } opt_memory_page.remove(0); opt_memory_page.add(a); } opt_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { opt_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //快表模式 if(!opt_quick_table_page.contains(a)) { //不在快表 if(!opt_memory_page.contains(a)) { //不在内存,产生缺页中断 opt_lackpage ++ ; if(opt_memory_page.size() < config.memory_num) { //内存块未满,直接调入 opt_memory_page.add(a); } else { Map<Integer, Boolean> has_move = new HashMap<>();//移动过的不能再移动 int cnt = 0;//移动次数要小于内存块数 for (int j = 0; j < config.taskSize; j ++ ) { has_move.put(config.task[j], false); } for(int j = opt_i + 1; j < config.taskSize; j ++ ) { //从需要进入内存的页号的后一位开始 if(opt_memory_page.contains(config.task[j]) && has_move.get(config.task[j]) != true && cnt < config.memory_num - 1) { int temp = 0; for (int i = 0; i < opt_memory_page.size(); ++ i) { if (config.task[j] == opt_memory_page.get(i)) temp = i; } opt_memory_page.remove(temp); opt_memory_page.add(config.task[j]); has_move.put(config.task[j], true); cnt++; } } opt_memory_page.remove(0); opt_memory_page.add(a); } //快表置换用FIFO if(opt_quick_table_page.size() < config.quick_table_num) { opt_quick_table_page.add(a); } else { opt_quick_table_page.remove(0); opt_quick_table_page.add(a); } opt_total_time += 3 * config.memory_access + config.miss_page_interrupt; per_time += 3 * config.memory_access + config.miss_page_interrupt; } else { //在内存 opt_total_time += 2 * config.memory_access; per_time += 2 * config.memory_access; } } else { //在快表 opt_total_time += config.quick_table_access + config.memory_access; per_time += config.quick_table_access + config.memory_access; } } return per_time; } @Override public void run() { for (opt_i = 0; opt_i < config.taskSize; opt_i ++ ) { pause.lock(); double per_time = 0; per_time = start(config.task[opt_i], per_time); String str1="查找"; String s1 = String.valueOf(config.task[opt_i]); snumber += str1; snumber += s1; snumber += ":"; for (int j = 0; j < config.memory_num; j ++ ) { if (j < opt_memory_page.size()) { String s = String.valueOf(opt_memory_page.get(j)); snumber += s; snumber += " "; } else { snumber+=" "; } } pause.unlock(); BigDecimal bd = new BigDecimal(per_time); bd = bd.setScale(2, RoundingMode.HALF_UP); snumber += "耗费时间:" + bd + "\n"; Page.OPTtest.append(snumber); snumber = ""; try { sleep(config.run_time); } catch (InterruptedException e) { e.printStackTrace(); } } String s1 = String.valueOf(config.taskSize); s1 = String.valueOf(opt_lackpage); interruption = s1 + "次"; double d = (double)opt_lackpage / config.taskSize * 100; BigDecimal bd = new BigDecimal(d); bd = bd.setScale(2, RoundingMode.HALF_UP); opt_ave_time = (double)opt_total_time / config.taskSize; totalTime = String.valueOf(opt_total_time); BigDecimal bda = new BigDecimal(opt_ave_time); bda = bda.setScale(2, RoundingMode.HALF_UP); avrTime = String.valueOf(bda); missPage = bd + "%"; Page.opt_interrupt_label.setText(interruption); Page.opt_total_time_label.setText(totalTime); Page.opt_ave_time_label.setText(avrTime); Page.opt_miss_page_label.setText(missPage); // System.out.println("opt"+s1); // System.out.println("opt"+opt_total_time); // System.out.println("opt"+opt_ave_time); // System.out.println("opt"+d + "%"); } }
连接数据库
保存记录
Save
package com.lr.db; import java.sql.*; import com.lr.frame.Page; import com.lr.algurithm.config; import javax.swing.*; public class Save { private static String url = null; private static String username = null; private static String password = null; private static Connection conn = null; private static Statement st = null; public Save() { try { saveRecord(); } catch (SQLException throwables) { throwables.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (st != null) { try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } } public void saveRecord() throws SQLException, ClassNotFoundException { //1.加载驱动 //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver");//固定写法 //2.用户信息和 url = "jdbc:mysql://127.0.0.1:3306/os?useUnicode=true&characterEncoding=utf8&useSSL=true"; username = "root"; password = "123456"; //3.连接成功,数据库对象 Connection 代表数据库 conn = DriverManager.getConnection(url, username, password); //4.获得SQL的执行对象 st = conn.createStatement(); String sql = "INSERT INTO test(memory_n,page_n,page_list,memory_t,quick_table_t," + "interrupt_t,is_check,fifo_total_t,fifo_ave_t,fifo_miss_page,fifo_interrupt_n," + "fifo_text,lru_total_t,lru_ave_t,lru_miss_page,lru_interrupt_n,lru_text," + "lfu_total_t,lfu_ave_t,lfu_miss_page,lfu_interrupt_n,lfu_text,opt_total_t," + "opt_ave_t,opt_miss_page,opt_interrupt_n,opt_text)VALUES('" + config.memory_num + "','" + config.taskSize + "','" + Page.task_edit.getText() + "','" + config.memory_access + "','" + config.quick_table_access + "','" + config.miss_page_interrupt + "','" + config.is_check + "','" + Page.fifo_total_time_label.getText() + "','" + Page.fifo_ave_time_label.getText() + "','" + Page.fifo_miss_page_label.getText() + "','" + Page.fifo_interrupt_label.getText() + "','" + Page.FIFOtest.getText() + "','" + Page.lru_total_time_label.getText() + "','" + Page.lru_ave_time_label.getText() + "','" + Page.lru_miss_page_label.getText() + "','" + Page.lru_interrupt_label.getText() + "','" + Page.LRUtest.getText() + "','" + Page.lfu_total_time_label.getText() + "','" + Page.lfu_ave_time_label.getText() + "','" + Page.lfu_miss_page_label.getText() + "','" + Page.lfu_interrupt_label.getText() + "','" + Page.LFUtest.getText() + "','" + Page.opt_total_time_label.getText() + "','" + Page.opt_ave_time_label.getText() + "','" + Page.opt_miss_page_label.getText() + "','" + Page.opt_interrupt_label.getText() + "','" + Page.OPTtest.getText() + "')"; int i = st.executeUpdate(sql); if (i > 0) { JOptionPane.showMessageDialog(null, "保存成功", "提交信息", 1); } else { JOptionPane.showMessageDialog(null, "保存失败", "warning", JOptionPane.ERROR_MESSAGE); } } public static void main(String[] args) { new Save(); } }
历史记录
History
package com.lr.db; import javax.swing.*; import java.awt.*; import java.sql.*; public class History { private static String url=null; private static String username=null; private static String password=null; private JFrame frame; public History() { frame = new JFrame("历史记录"); // frame.setDefaultCloseoperation(WindowConstants.EXIT_ON_CLOSE); // 退出应用程序默认窗口关闭 // //获取与系统相关的默认工具类对象 Toolkit toolkit = Toolkit.getDefaultToolkit(); //获取屏幕分辨率 Dimension d = toolkit.getScreenSize(); frame.setBounds((int)(d.getWidth() - 1000) / 2,(int)(d.getHeight() - 850) / 2, 1000, 850); frame.setVisible(true); try { historyRecord(); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE); } catch (SQLException sqle) { sqle.printStackTrace(); JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE); } } public void historyRecord() throws SQLException, ClassNotFoundException { //1.加载驱动 //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver");//固定写法 //2.用户信息和 url="jdbc:mysql://127.0.0.1:3306/os?useUnicode=true&characterEncoding=utf8&useSSL=true"; username="root"; password="123456"; //3.连接成功,数据库对象 Connection 代表数据库 Connection conn=DriverManager.getConnection(url,username,password); //4.执行SQL的对象,去执行SQL,可能存在结果,返回结果信息 String sql = "select * from test"; PreparedStatement pstm = conn.prepareStatement(sql); ResultSet rs = pstm.executeQuery(); // ResultSet rs=statement.executeQuery(sql); int count = 0; while (rs.next()) { count++; } rs = pstm.executeQuery(); // 将查询获得的记录数据,转换成适合生成JTable的数据形式 Object[][] info = new Object[count][27]; Object[] title = {"内存块数", "页面大小", " 页号序列", "内存存取时间", "快表存取时间", "中断执行时间", "是否勾选快表", "FIFO总时间", "FIFO平均时间", "FIFO缺页率", "FIFO中断次数", "FIFO执行过程", "LRU总时间", "LRU平均时间", "LRU缺页率", "LRU中断次数", "LRU执行过程", "LFU总时间", "LFU平均时间", "LFU缺页率", "LFU中断次数", "LFU执行过程", "OPT总时间", "OPT平均时间", "OPT缺页率", "OPT中断次数", "OjavascriptPT执行过程"}; count = 0; while (rs.next()) { // info[count][0] = rs.getObject("id"); info[count][0] = rs.getObject("memory_n"); info[count][1] = rs.getObject("page_n"); info[count][2] = rs.getObject("page_list"); info[count][3] = rs.getObject("memory_t"); info[count][4] = rs.getObject("quick_table_t"); info[count][5] = rs.getObject("interrupt_t"); info[count][6] = rs.getObject("is_check"); info[count][7] = rs.getObject("fifo_total_t"); info[count][8] = rs.getObject("fifo_ave_t"); info[count][9] = rs.getObject("fifo_miss_page"); info[count][10] = rs.getObject("fifo_interrupt_n"); info[count][11] = rs.getObject("fifo_text"); info[count][12] = rs.getObject("lru_total_t"); info[count][13] = rs.getObject("lru_ave_t"); info[count][14] = rs.getObject("lru_miss_page"); info[count][15] = rs.getObject("lru_interrupt_n"); info[count][16] = rs.getObject("lru_text"); info[count][17] = rs.getObject("lfu_total_t"); info[count][18] = rs.getObject("lfu_ave_t"); info[count][19] = rs.getObject("lfu_miss_page"); info[count][20] = rs.getObject("lfu_interrupt_n"); info[count][21] = rs.getObject("lfu_text"); info[count][22] = rs.getObject("opt_total_t"); info[count][23] = rs.getObject("opt_ave_t"); info[count][24] = rs.getObject("opt_miss_page"); info[count][25] = rs.getObject("opt_interrupt_n"); info[count][26] = rs.getObject("opt_text"); count++; } JTable table = new JTable(info, title); // 设置表格内容颜色 table.setForeground(Color.BLACK); // 字体颜色 table.setFont(new Font(null, Font.PLAIN, 14)); // 字体样式 table.setSelectionForeground(Color.DARK_GRAY); // 选中后字体颜色 table.setSelectionBackground(Color.LIGHT_GRAY); // 选中后字体背景 table.setGridColor(Color.GRAY); // 网格颜色 // 设置表头 table.getTableHeader().setFont(new Font(null, Font.BOLD, 14)); // 设置表头名称字体样式 table.getTableHeader().setForeground(Color.RED); // 设置表头名称字体颜色 table.getTableHeader().setReorderingAllowed(false); // 设置不允许拖动重新排序各列 // 设置行高 table.setRowHeight(30); // 第一列列宽设置为40 table.getColumnModel().getColumn(0).setPreferredwidth(40); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jscrollPane scroll = new JScrollPane(table); frame.add(scroll); frame.setVisible(true); frame.pack(); } public static void main(String[] args) { new History(); } }
GUI界面
Page
package com.lr.frame; import com.lr.algurithm.FIFO; import com.lr.algurithm.LFU; import com.lr.algurithm.LRU; import com.lr.algurithm.OPT; import com.lr.algurithm.config; import com.lr.db.History; import com.lr.db.Save; import javax.swing.*; import java.awt.*; import java.util.Arrays; import java.util.List; public class Page { public JFrame frame; private JPanel panel; public static TextArea FIFOtest; public static TextArea LRUtest; public static TextArea LFUtest; public static TextArea OPTtest; public static JTextField task_edit; public static JTextField memory_num_edit; public static JTextField taskSize_edit; public static JTextField taskSize_low_edit; public static JTextField taskSize_high_edit; public static JTextField task_low_edit; public static JTextField task_high_edit; public static JCheckBox quick_table_check; public static JTextField memory_access_edit; public static JTextField quick_table_access_edit; public static JTextField miss_page_interrupt_edit; public static JTextField run_time_edit; public static JLabel fifo_interrupt_label; public static JLabel fifo_miss_page_label; public static JLabel fifo_total_time_label; public static JLabel fifo_ave_time_label; public static JLabel lru_interrupt_label; public static JLabel lru_miss_page_label; public static JLabel lru_total_time_label; public static JLabel lru_ave_time_label; public static JLabel lfu_interrupt_label; public static JLabel lfu_miss_page_label; public static JLabel lfu_total_time_label; public static JLabel lfu_ave_time_label; public static JLabel opt_interrupt_label; public static JLabel opt_miss_page_label; public static JLabel opt_total_time_label; public static JLabel opt_ave_time_label; public static JTextField quick_table_num_edit; FIFO fifo = new FIFO(); LRU lru = new LRU(); LFU lfu = new LFU(); OPT opt = new OPT(); public Page() { frame = new JFrame(); frame.setTitle("页面置换算法"); //获取与系统相关的默认工具类对象 Toolkit toolkit = Toolkit.getDefaultToolkit(); //获取屏幕分辨率 Dimension d = toolkit.getScreenSize(); frame.setBounds((int)(d.getWidth() - 1185) / 2,(int)(d.getHeight() - 895) / 2, 1185, 895); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 退出应用程序默认窗口关闭 frame.getContentPane().setLayout(null); // frame.setVisible(true); panel = new JPanel(); panel.setBounds(10, 10, 1165, 890); frame.getContentPane().add(panel); panel.setLayout(null); JLabel label_1 = new JLabel("内存块数:"); label_1.setBounds(35, 20, 100, 20); label_1.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_1); panel.add(label_1); memory_num_edit = new JTextField(); memory_num_edit.setBounds(145, 20, 180, 20); // frame.getContentPane().add(memory_num_edit); panel.add(memory_num_edit); // memory_num_edit.setColumns(10); JLabel label_2 = new JLabel("页面数量:"); label_2.setBounds(35, 60, 100, 20); label_2.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_2); panel.add(label_2); taskSize_edit = new JTextField(); taskSize_edit.setBounds(145, 60, 180, 20); // frame.getContentPane().add(taskSize_edit); panel.add(taskSize_edit); taskSize_low_edit = new JTextField(); taskSize_low_edit.setBounds(480, 60, 40, 20); // frame.getContentPane().add(taskSize_low_edit); panel.add(taskSize_low_edit); JLabel label_32 = new JLabel("-"); label_32.setBounds(525, 60, 10, 20); label_32.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_32); panel.add(label_32); taskSize_high_edit = new JTextField(); taskSize_high_edit.setBounds(540, 60, 40, 20); // frame.getContentPane().add(taskSize_high_edit); panel.add(taskSize_high_edit); JButton taskSize_btn = new JButton("随机生成"); taskSize_btn.setBounds(350, 55, 120, 30); taskSize_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(taskSize_btn); panel.add(taskSize_btn); taskSize_btn.addActionListener(e -> { if (!taskSize_low_edit.getText().isEmpty()) { config.page_low = Integer.parseInt(taskSize_low_edit.getText()); } else { config.page_low = 1; } if (!taskSize_high_edit.getText().isEmpty()) config.page_high = Integer.parseInt(taskSize_high_edit.getText()); else config.page_high = 9; // int r = (int) Math.random(); config.taskSize = (int) (config.page_low + Math.random() * (config.page_high - config.page_low + 1)); taskSize_edit.setText(String.valueOf(config.taskSize)); }); JLabel label_3 = new JLabel("页面序号:"); label_3.setBounds(35, 100, 100, 20); label_3.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_3); panel.add(label_3); task_low_edit = new JTextField(); task_low_edit.setBounds(480, 100, 40, 20); // frame.getContentPane().add(task_low_edit); panel.add(task_low_edit); JLabel label_33 = new JLabel("-"); label_33.setBounds(525, 100, 10, 20); label_33.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_33); panel.add(label_33); task_high_edit = new JTextField(); task_high_edit.setBounds(540, 100, 40, 20); // frame.getContentPane().add(task_high_edit); panel.add(task_high_edit); task_edit = new JTextField(); task_edit.setBounds(145, 100, 180, 20); // frame.getContentPane().add(task_edit); panel.add(task_edit); JButton task_btn = new JButton("随机生成"); task_btn.setBounds(350, 95, 120, 30); task_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(task_btn); panel.add(task_btn); task_btn.addActionListener(e -> { if (!task_low_edit.getText().isEmpty()) { config.page_list_low = Integer.parseInt(task_low_edit.getText()); } else { config.page_list_low = 1; } if (!task_high_edit.getText().isEmpty()) config.page_list_high = Integer.parseInt(task_high_edit.getText()); else config.page_list_high = 9; config.taskSize = Integer.parseInt(taskSize_edit.getText()); String list = "js"; config.task = new int[config.taskSize]; int i = 0; for (; i < config.taskSize - 1; i ++ ) { config.task[i] = (int) (config.page_list_low + Math.random() * (config.page_list_high - config.page_list_low + 1)); list += config.task[i] + ","; } config.task[i] = (int) (config.page_list_low + Math.random() * (config.page_list_high - config.page_list_low + 1)); list += config.task[i]; task_edit.setText(list); }); JLabel label_15 = new JLabel("快表大小:"); label_15.setBounds(35, 145, 100, 20); label_15.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_15); panel.add(label_15); quick_table_num_edit = new JTextField(); quick_table_num_edit.setBounds(145, 145, 180, 20); // frame.getContentPane().add(quick_table_num_edit); panel.add(quick_table_num_edit); quick_table_num_edit.addActionListener(e -> { config.quick_table_num = Integer.parseInt(quick_table_num_edit.getText()); }); quick_table_num_edit.setEnabled(false); quick_table_check = new JCheckBox("启用快表"); quick_table_check.setBounds(380, 145, 130, 30); quick_table_check.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(quick_table_check); panel.add(quick_table_check); quick_table_check.addActionListener(e -> { on_quick_table_check_clicked(quick_table_check.isSelected()); }); JLabel label_4 = new JLabel("内存存取时间:"); label_4.setBounds(700, 20, 140, 20); label_4.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_4); panel.add(label_4); memory_access_edit = new JTextField(); memory_access_edit.setBounds(845, 20, 50, 20); // frame.getContentPane().add(memory_access_edit); panel.add(memory_access_edit); JLabel label_34 = new JLabel("ms"); label_34.setBounds(900, 20, 20, 20); label_34.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_34); panel.add(label_34); JLabel label_6 = new JLabel("快表存取时间:"); label_6.setBounds(700, 60, 140, 20); label_6.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_6); panel.add(label_6); quick_table_access_edit = new JTextField(); quick_table_access_edit.setBounds(845, 60, 50, 20); // frame.getContentPane().add(quick_table_access_edit); panel.add(quick_table_access_edit); JLabel label_35 = new JLabel("ms"); label_35.setBounds(900, 60, 20, 20); label_35.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_35); panel.add(label_35); JLabel label_5 = new JLabel("缺页中断时间:"); label_5.setBounds(700, 100, 140, 20); label_5.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_5); panel.add(label_5); miss_page_interrupt_edit = new JTextField(); miss_page_interrupt_edit.setBounds(845, 100, 50, 20); // frame.getContentPane().add(miss_page_interrupt_edit); panel.add(miss_page_interrupt_edit); JLabel label_36 = new JLabel("ms"); label_36.setBounds(900, 100, 20, 20); label_36.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_36); panel.add(label_36); JLabel label_16 = new JLabel("执行时间控制:"); label_16.setBounds(700, 140, 140, 20); label_16.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_16); panel.add(label_16); run_time_edit = new JTextField(); run_time_edit.setBounds(845, 140, 50, 20); // frame.getContentPane().add(run_time_edit); panel.add(run_time_edit); JLabel label_17 = new JLabel("ms"); label_17.setBounds(900, 140, 20, 20); label_17.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_17); panel.add(label_17); JButton start_btn = new JButton("开始执行"); start_btn.setBounds(200, 185, 120, 30); start_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(start_btn); panel.add(start_btn); start_btn.addActionListener(e -> { on_clear_btn_clicked(); config.memory_num = Integer.parseInt(memory_num_edit.getText()); if (quick_table_check.isSelected()) { config.quick_table_num = Integer.parseInt(quick_table_num_edit.getText()); if (quick_table_check.isSelected() && config.quick_table_num >= config.memory_num) { JOptionPane.showMessageDialog(null, "快表大小应该小于内存大小", "warning", JOptionPane.ERROR_MESSAGE); return; } } config.memory_access = Double.parseDouble(memory_access_edit.getText()); config.quick_table_access = Double.parseDouble(quick_table_access_edit.getText()); config.miss_page_interrupt = Double.parseDouble(miss_page_interrupt_edit.getText()); config.is_check = Boolean.parseBoolean(String.valueOf(quick_table_check.isSelected())); String str = task_edit.getText(); List<String> lst = Arrays.asList(str.split(",")); if (run_time_edit.getText().isEmpty()) { run_time_edit.setText("300"); } config.run_time = Integer.parseInt(run_time_edit.getText()); config.taskSize = lst.size(); config.task = new int[config.taskSize]; for (int i = 0; i < config.taskSize; ++ i) { str = lst.get(i); config.task[i] = Integer.parseInt(str); } new Thread(() -> fifo.run()).start(); new Thread(() -> lru.run()).start(); new Thread(() -> lfu.run()).start(); new Thread(() -> opt.run()).start(); }); JButton stop_continue_btn = new JButton("暂停执行"); stop_continue_btn.setBounds(500, 185, 120, 30); stop_continue_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(stop_continue_btn); panel.add(stop_continue_btn); stop_continue_btn.addActionListener(e -> { if (stop_continue_btn.getText() == "暂停执行") { fifo.pause.lock(); lru.pause.lock(); lfu.pause.lock(); opt.pause.lock(); stop_continue_btn.setText("继续执行"); } else { fifo.pause.unlock(); lru.pause.unlock(); lfu.pause.unlock(); opt.pause.unlock(); stop_continue_btn.setText("暂停执行"); } }); JButton stop_btn = new JButton("结束执行"); stop_btn.setBounds(800, 185, 120, 30); stop_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(stop_btn); panel.add(stop_btn); stop_btn.addActionListener(e -> { System.exit(0); }); // FIFO JLabel lblFifo = new JLabel("FIFO"); title(lblFifo, 20, 250, 230, 50, 15); FIFOtest = new TextArea(); textBox(FIFOtest, 20, 180, 250, 200, 250); // JScrollPane scroll_fifo = new JScrollPane(FIFOtest); // scroll(scroll_fifo, 180, 250, 200, 250); // LRU JLabel lblLru = new JLabel("LRU"); title(lblLru, 20, 490, 230, 50, 15); LRUtest = new TextArea(); textBox(LRUtest, 20, 420, 250, 200, 250); // JScrollPane scroll_lru = new JScrollPane(LRUtest); // scroll(scroll_lru, 420, 250, 200, 250); // LFU JLabel lblLfu = new JLabel("LFU"); title(lblLfu, 20, 730, 230, 50, 15); LFUtest = new TextArea(); textBox(LFUtest, 20, 660, 250, 200, 250); // JScrollPane scroll_lfu = new JScrollPane(LFUtest); // scroll(scroll_lfu, 660, 250, 200, 250); //OPT JLabel lblOpt = new JLabel("OPT"); title(lblOpt, 20, 970, 230, 50, 15); OPTtest = new TextArea(); textBox(OPTtest, 20, 900, 250, 200, 250); // JScrollPane scroll_opt= new JScrollPane(OPTtest); // scroll(scroll_opt, 900, 250, 200, 250); JLabel label_11 = new JLabel("中断次数:"); label_11.setBounds(35, 550, 100, 20); label_11.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_11); panel.add(label_11); fifo_interrupt_label = new JLabel(""); fifo_interrupt_label.setBounds(220, 550, 100, 20); fifo_interrupt_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(fifo_interrupt_label); panel.add(fifo_interrupt_label); lru_interrupt_label = new JLabel(""); lru_interrupt_label.setBounds(460, 550, 100, 20); lru_interrupt_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lru_interrupt_label); panel.add(lru_interrupt_label); lfu_interrupt_label = new JLabel(""); lfu_interrupt_label.setBounds(700, 550, 100, 20); lfu_interrupt_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lfu_interrupt_label); panel.add(lfu_interrupt_label); opt_interrupt_label = new JLabel(""); opt_interrupt_label.setBounds(940, 550, 100, 20); opt_interrupt_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(opt_interrupt_label); panel.add(opt_interrupt_label); JLabel label_12 = new JLabel("总时间:"); label_12.setBounds(35, 600, 100, 20); label_12.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_12); panel.add(label_12); fifo_total_time_label = new JLabel(""); fifo_total_time_label.setBounds(220, 600, 100, 20); fifo_total_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(fifo_total_time_label); panel.add(fifo_total_time_label); lru_total_time_label = new JLabel(""); lru_total_time_label.setBounds(460, 600, 100, 20); lru_total_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lru_total_time_label); panel.add(lru_total_time_label); lfu_total_time_label = new JLabel(""); lfu_total_time_label.setBounds(700, 600, 100, 20); lfu_total_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lfu_total_time_label); panel.add(lfu_total_time_label); opt_total_time_label = new JLabel(""); opt_total_time_label.setBounds(940, 600, 100, 20); opt_total_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(opt_total_time_label); panel.add(opt_total_time_label); JLabel label_13 = new JLabel("平均时间:"); label_13.setBounds(35, 650, 100, 20); label_13.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_13); panel.add(label_13); fifo_ave_time_label = new JLabel(""); fifo_ave_time_label.setBounds(220, 650, 100, 20); fifo_ave_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(fifo_ave_time_label); panel.add(fifo_ave_time_label); lru_ave_time_label = new JLabel(""); lru_ave_time_label.setBounds(460, 650, 100, 20); lru_ave_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lru_ave_time_label); panel.add(lru_ave_time_label); lfu_ave_time_label = new JLabel(""); lfu_ave_time_label.setBounds(700, 650, 100, 20); lfu_ave_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lfu_ave_time_label); panel.add(lfu_ave_time_label); opt_ave_time_label = new JLabel(""); opt_ave_time_label.setBounds(940, 650, 100, 20); opt_ave_time_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(opt_ave_time_label); panel.add(opt_ave_time_label); JLabel label_14 = new JLabel("缺页率:"); label_14.setBounds(35, 700, 100, 20); label_14.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(label_14); panel.add(label_14); fifo_miss_page_label = new JLabel(""); fifo_miss_page_label.setBounds(220, 700, 100, 20); fifo_miss_page_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(fifo_miss_page_label); panel.add(fifo_miss_page_label); lru_miss_page_label = new JLabel(""); lru_miss_page_label.setBounds(460, 700, 100, 20); lru_miss_page_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lru_miss_page_label); panel.add(lru_miss_page_label); lfu_miss_page_label = new JLabel(""); lfu_miss_page_label.setBounds(700, 700, 100, 20); lfu_miss_page_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(lfu_miss_page_label); panel.add(lfu_miss_page_label); opt_miss_page_label = new JLabel(""); opt_miss_page_label.setBounds(940, 700, 100, 20); opt_miss_page_label.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(opt_miss_page_label); panel.add(opt_miss_page_label); JButton save_btn = new JButton("保存"); save_btn.setBounds(200, 760, 120, 30); save_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(save_btn); panel.add(save_btn); save_btn.addActionListener(e -> { new Save(); }); JButton open_btn = new JButton("打开"); open_btn.setBounds(500, 760, 120, 30); open_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(open_btn); panel.add(open_btn); open_btn.addActionListener(e -> { new History(); }); JButton clear_btn = new JButton("清空"); clear_btn.setBounds(800, 760, 120, 30); clear_btn.setFont(new Font("宋体", Font.PLAIN, 20)); // frame.getContentPane().add(clear_btn); panel.add(clear_btn); clear_btn.addActionListener(e->{ on_clear_btn_clicked(); clear_parameter(); }); } /** * 四种算法标题 * @param label * @param size * @param x * @param y * @param w * @param h */ public void title(JLabel label, int size, int x, int y ,int w, int h) { label.setFont(new Font("宋体", Font.PLAIN, size)); label.setBounds(x, y, w, h); // frame.getContentPane().add(label); panel.add(label); } /** * 四种算法运行结果显示框 * @param textArea * @param size * @param x * @param y * @param w * @param h */ public void textBox(TextArea textArea, int size, int x, int y ,int w, int h) { textArea.setFont(new Font("宋体", Font.PLAIN, size)); textArea.setBackground(SystemColor.controlHighlight); textArea.setBounds(x, y, w, h); // frame.getContentPane().add(textArea); panel.add(textArea); } /** * 分别设置水平和垂直滚动条自动出现 * @param scrollPane * @param x * @param y * @param w * @param h */ public void scroll(JScrollPane scrollPane,int x, int y, int w, int h) { scrollPane.setLocation(x, y); scrollPane.setSize(w, h); scrollPane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // frame.getContentPane().add(scrollPane); panel.add(scrollPane); } public void on_clear_btn_clicked() { // fifo.task[] = new int[fifo.taskSize]; final int[] task = config.task; FIFOtest.setText(""); fifo.fifo_lackpage = 0; fifo.fifo_ave_time=0; fifo.fifo_total_time=0; fifo.fifo_memory_page.clear(); fifo.fifo_quick_table_page.clear(); fifo.fifo_memory_page.clear(); fifo_interrupt_label.setText(""); fifo_miss_page_label.setText(""); fifo_total_time_label.setText(""); fifo_ave_time_label.setText(""); LRUtest.setText(""); lru.lru_lackpage = 0; lru.lru_ave_time=0; lru.lru_total_time=0; lru.lru_memory_page.clear(); lru.lru_quick_table_page.clear(); lru_interrupt_label.setText(""); lru_miss_page_label.setText(""); lru_total_time_label.setText(""); lru_ave_time_label.setText(""); LFUtest.setText(""); lfu.lfu_lackpage = 0; lfu.lfu_ave_time=0; lfu.lfu_total_time=0; lfu.lfu_memory_page.clear(); lfu.lfu_quick_table_page.clear(); lfu_interrupt_label.setText(""); lfu_miss_page_label.setText(""); lfu_total_time_label.setText(""); lfu_ave_time_label.setText(""); OPTtest.setText(""); opt.opt_lackpage = 0; opt.opt_ave_time=0; opt.opt_total_time=0; opt.opt_memory_page.clear(); opt.opt_quick_table_page.clear(); opt_interrupt_label.setText(""); opt_miss_page_label.setText(""); opt_total_time_label.setText(""); opt_ave_time_label.setText(""); } public static void on_quick_table_check_clicked(boolean checked) { if (checked) quick_table_num_edit.setEnabled(true); else quick_table_num_edit.setEnabled(false); } public static void clear_parameter() { memory_num_edit.setText(""); taskSize_edit.setText(""); taskSize_low_edit.setText(""); taskSize_high_edit.setText(""); task_low_edit.setText(""); task_high_edit.setText(""); task_edit.setText(""); quick_table_num_edit.setText(""); memory_access_edit.setText(""); quick_table_access_edit.setText(""); miss_page_interrupt_edit.setText(""); run_time_edit.setText(""); quick_table_check.setSelected(false); quick_table_num_edit.setEnabled(false); } public static void main(String[] args) { Page window = new Page(); window.frame.setVisible(true); } }
到此这篇关于Java实现FIFO、LRU、LFU、OPT页面置换算法的文章就介绍到这了,更多相关Java 页面置换算法内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论