JAVA实现请求分页_【java】请求分配分页模拟

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.TextArea;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class request {

public static void main(String arg[]){

request r = new request();

@SuppressWarnings("unused")

frame reframe = r.new frame();

}

class frame extends JFrame{

private static final long serialVersionUID = 1L;

public TextArea input = new TextArea();

public JPanel panelu = new JPanel();

public JPanel panelc = new JPanel();

public JPanel paneld = new JPanel();

public JLabel labelul = new JLabel("输入指令序列 ");

public JLabel labeluc = new JLabel(" 输出内存块中的页 ");

public JLabel labelur = new JLabel(" 缺页次数和缺页率 ");

public JLabel labeldl = new JLabel("指令条数");

public JLabel labeldc = new JLabel("页数");

public JLabel labeldr = new JLabel("调页方法");

public TextArea show = new TextArea();

public TextArea result = new TextArea();

public JComboBox choice = new JComboBox();

public JButton submit = new JButton("确定");

public pagestring pstr = new pagestring();;

public block pbl = new block();

public JTextField insize = new JTextField("320");

public JTextField pagesize = new JTextField("10");

public StringBuffer out = new StringBuffer();

frame(){

pstr.init();

choice.addItem("FIFO");

choice.addItem("LRU");

insize.setEnabled(false);

pagesize.setEnabled(false);

this.setTitle("请求分配分页方式");

this.setSize(1400,400);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

setLocationRelativeTo(null);

combine();

setlayout();

action();

}

public void combine(){

panelu.setSize(1400,50);

panelu.setLayout(new GridLayout(1,3));

panelc.setLayout(new GridLayout(1,3));

input.setSize(200,450);

show.setSize(200,450);

result.setSize(200,450);

panelu.add(labelul);

panelu.add(labeluc);

panelu.add(labelur);

panelc.add(input);

panelc.add(show);

panelc.add(result);

paneld.add(labeldl);

paneld.add(insize);

paneld.add(labeldc);

paneld.add(pagesize);

paneld.add(labeldr);

paneld.add(choice);

paneld.add(submit);

}

public void setlayout(){

setLayout(new FlowLayout(1,20,20));

add(panelu);

add(panelc);

add(paneld);

}

public void action(){

submit.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

pstr.init();

pbl.bpage.clear();

StringBuffer in = new StringBuffer();

StringBuffer re = new StringBuffer();

int loss = 0;

for(page u:pstr.pstring){

in.append("页数:"+u.ppage+" 页内偏移:"+u.prest+" 实际地址:"

+u.number);

in.append("\n");

if(choice.getSelectedItem().toString()=="FIFO"){

if(pbl.FIFOin(u)==false)

loss++;

for(page s:pbl.bpage){

out.append("\t"+s.ppage);

}

}

else if(choice.getSelectedItem().toString()=="LRU"){

if(pbl.LRUin(u)==false)

loss++;

for(page s:pbl.bpage){

out.append("\t"+s.ppage);

}

}

out.append("\n");

}

input.setText(in.toString());

show.setText(out.toString());

re.append("缺页次数: "+loss+"\n缺页率: "+(double)loss/320);

result.setText(re.toString());

out.delete(0, out.toString().length());

}

});

}

}

class block{

public LinkedList bpage;

block(){

bpage = new LinkedList();

}

public boolean isfree(){

if(bpage.size()==4){

return false;

}

else {

return true;

}

}

public boolean FIFOin(page p){

if(bpage.size()==4){

for(page u:bpage){

if(u.ppage==p.ppage){

bpage.addFirst(p);

bpage.remove(bpage.indexOf(u));

return true;

}

}

bpage.removeFirst();

bpage.add(p);

return false;

}

else {

for(page u:bpage){

if(u.ppage==p.ppage){

bpage.addFirst(p);

bpage.remove(bpage.indexOf(u));

return true;

}

}

bpage.add(p);

return false;

}

}

public int min(){

if(bpage.size()!=0){

int min = bpage.get(0).time;

int index = 0;

for(int i = 0;i

if(bpage.get(i).time

min = bpage.get(i).time;

index = i;

}

}

return index;

}

else {

return -1;

}

}

public boolean LRUin(page p){

if(bpage.size()==4){

for(page u:bpage){

if(u.ppage==p.ppage){

bpage.remove(min());

bpage.addFirst(p);

p.time++;

return true;

}

}

bpage.removeFirst();

bpage.add(p);

p.time++;

return false;

}

else {

for(page u:bpage){

if(u.ppage==p.ppage){

bpage.addFirst(p);

bpage.remove(bpage.indexOf(u));

return true;

}

}

bpage.add(p);

p.time++;

return false;

}

}

}

class page{

public int ppage;

public int prest;

public int number;

public int time;

page(){

this.ppage = 0;

this.prest = 0;

this.number = 0;

this.time = 0;

}

page(int n){

this.number = n;

this.ppage = n/10;

this.prest = n%10;

this.time = 0;

}

}

class pagestring{

public ArrayList pstring;

pagestring(){

pstring = new ArrayList();

}

public void init(){

pstring.clear();

Random random = new Random();

int initn = 160;

for (int i = 0;i<80;i++){

pstring.add(new page(initn));

pstring.add(new page(initn+1));

int s = random.nextInt(319)%(319-initn+2+1) + initn+2;

pstring.add(new page(s));

pstring.add(new page(s+1));

initn = random.nextInt(s)%(s+1);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java分页实现的示例代码: ```java public class Paging { private int pageSize; // 每页显示的记录数 private int currentPage; // 当前页码 private int totalCount; // 总记录数 private int totalPage; // 总页数 public Paging(int pageSize, int currentPage, int totalCount) { this.pageSize = pageSize; this.currentPage = currentPage; this.totalCount = totalCount; // 计算总页数 if (this.totalCount % this.pageSize == 0) { this.totalPage = this.totalCount / this.pageSize; } else { this.totalPage = this.totalCount / this.pageSize + 1; } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } /** * 获取当前页的起始记录索引 * * @return */ public int getStartIndex() { return (this.currentPage - 1) * this.pageSize; } /** * 获取当前页的结束记录索引 * * @return */ public int getEndIndex() { return Math.min(this.currentPage * this.pageSize - 1, this.totalCount - 1); } } ``` 使用方法: ```java // 假设数据库中有100条记录,每页显示10条记录 Paging paging = new Paging(10, 1, 100); System.out.println("总共有" + paging.getTotalPage() + "页"); System.out.println("当前页的起始记录索引为:" + paging.getStartIndex()); System.out.println("当前页的结束记录索引为:" + paging.getEndIndex()); ``` 输出结果: ``` 总共有10页 当前页的起始记录索引为:0 当前页的结束记录索引为:9 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值