📄 memory.java
字号:
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class memory extends JFrame {
int msize=1;
private static int DELAY = 100;
int maxmem=100;//最大内存
int memvalue=10;
int runtime=1;
boolean flag = true;
int omgtime=0;
Label c0 = new Label("0");
Label c1=new Label("10");
Label c2=new Label("20");
Label c3=new Label("30");
Label c4=new Label("40");
Label c5=new Label("50");
Label c6=new Label("60");
Label c7=new Label("70");
Label c8=new Label("80");
Label c9=new Label("90");
Label c10=new Label("100(MB)");
JProgressBar aJProgressBar = new JProgressBar(0, 10);//进度条类哦
List Lready;
List Lwaiting;
List Lsuspend;
List L_cpus_info, L_memory_table, L_memory_gra;
JButton BCpuscheduler,BRestore, Bgetp_r, BgetP_s, Bgetp_s_r,random;
JTextField tp_name, tp_time, tp_priority, tp_memory_size;
JTextField tcpuinfo;
JLabel p_name, p_time, p_priority, p_memory_size, r_queue, w_queue, s_queue,
memory_gra, memory_table, cpus_info;
JPanel p1, p2, p3, p4,p5,p6;
PCB FirstPcb = new PCB();
notable stable = new notable(10,90,true);
ArrayList<notable> notables = new ArrayList<notable>();
ArrayList<notable> cnotables = new ArrayList<notable>();
ArrayList<PCB> pcbs = new ArrayList<PCB>();
ArrayList<PCB> Gpcbs = new ArrayList<PCB>();
ArrayList<PCB> Hpcbs = new ArrayList<PCB>();
Timer timer = new Timer();
void sort(ArrayList<PCB> pcbs){
for(int i=0;i<pcbs.size()-1;i++){
for(int j = i+1 ; j<pcbs.size();j++){
if(pcbs.get(i).getPriority() >pcbs.get(j).getPriority()){
PCB d = pcbs.get(i);
pcbs.set(i,pcbs.get(j));
pcbs.set(j,d);
}
}
}
}
void fenpei(PCB pcb){
int size=pcb.getSize();
for(int i=0;i<notables.size();i++){
notable table=notables.get(i);
if(table.isState()){
if(table.getSize()>=pcb.getSize()){
if(table.getSize()>pcb.getSize()+msize){
notable ta1=new notable(table.getStmem(),pcb.getSize(),false);
notable ta2=new notable(table.getStmem()+pcb.getSize(),table.getSize()-pcb.getSize(),true);
pcb.setStmem(table.getStmem());
notables.remove(i);
notables.add(i, ta1);
notables.add(i+1, ta2);
return;
}else{
pcb.setStmem(table.getStmem());
notables.get(i).setState(false);
return;
}
}else {
}
}
}
int cc=0;
int sta=10;
for(int i=0;i<notables.size();i++){
notable table=notables.get(i);
if(table.isState()){
/*notable ta= new notable(sta,table.getSize(),true);
cnotables.add(ta);
sta=sta+table.getSize();*/
}
else{
table.setStmem(sta);
sta=sta+table.getSize();
cnotables.add(table);
}
}
notable ta=new notable(sta,400-sta,true);
cnotables.add(ta);
notables=cnotables;
fenpei(pcb);
System.out.println("无法分区");
tcpuinfo.setText(" 无法分区!");
}
void huishou(PCB pcb){
int count=0;
int stmem=pcb.getStmem();
for(int i=0;i<notables.size();i++){
notable table=notables.get(i);
if(stmem==table.getStmem()){
if(i==0){
if(notables.get(i+1).isState()){
notable ta1=new notable(table.getStmem(),table.getSize()+notables.get(i+1).getSize(),true);
notables.add(i, ta1);
notables.remove(i+1);
notables.remove(i+1);
return;
}else{
notables.get(i).setState(true);
return;
}
}else {
if(i==notables.size()-1){
if(notables.get(i-1).isState()){
notable ta1=new notable(notables.get(i-1).getStmem(),table.getSize()+notables.get(i-1).getSize(),true);
notables.add(i+1,ta1);
notables.remove(i);
notables.remove(i-1);
return;
}else{
notables.get(i).setState(true);
return;
}
}else{
notables.get(i).setState(true);
if(notables.get(i-1).isState()){
notable ta1=new notable(notables.get(i-1).getStmem(),table.getSize()+notables.get(i-1).getSize(),true);
notables.add(i+1,ta1);
notables.remove(i);
notables.remove(i-1);
count++;
}
if(count==0){
if(notables.get(i+1).isState()){
notable ta1=new notable(table.getStmem(),table.getSize()+notables.get(i+1).getSize(),true);
notables.add(i, ta1);
notables.remove(i+1);
notables.remove(i+1);
//return;
}
}else{
if(notables.get(i).isState()){
notable ta1=new notable(notables.get(i-1).getStmem(),notables.get(i-1).getSize()+notables.get(i).getSize(),true);
notables.add(i, ta1);
notables.remove(i+1);
notables.remove(i-1);
//return;
}
}
return;
}
}
}
}
}
class MyTask extends java.util.TimerTask{
@Override
public void run() {
int Ftime = 0;
int Fpri = FirstPcb.getPriority()+1;
if (Ftime > 0) {
FirstPcb.setTime(Ftime);
FirstPcb.setPriority(Fpri);
} else {
if(flag){
memvalue = memvalue-FirstPcb.getSize();
FirstPcb.setState("ready");
}
else{
flag = true;
}
huishou(pcbs.get(0));
pcbs.remove(FirstPcb);// 不能用remove(j) 感觉是不能用这个方法移除第一个类
}
if(pcbs.size()>0){
tcpuinfo.setText(" 调度成功!");
}
else{
tcpuinfo.setText(" 无进程在就绪队列中 !");
}
// Table a=new Table(notables);
BCpuscheduler.setEnabled(true);
BgetP_s.setEnabled(true);
Bgetp_s_r.setEnabled(true);
repaint();
}
}
// Panel a = new Panel();
// Panel b = new Panel();
public static void main(String[] args) {
new memory().View();
// new ChatClient().reView();
// new ChatClient().information();
}
void View() {
// new Thread(new PaintThread()).start();
notables.add(stable);
aJProgressBar.setStringPainted(true); // 显示百分比字符
aJProgressBar.setIndeterminate(false); // 不确定的进度条
this.setSize(800, 600);
this.setLocation(100, 100);
this.setLayout(null);
this.setBackground(Color.GRAY);
this.setLayout(null/*new GridLayout(2, 1, 0, 200)*/);
p_name = new JLabel("进程名");// 第一行,输入进程信息面板;
tp_name = new JTextField(10);
p_time = new JLabel("运行时间(s)");
tp_time = new JTextField(10);
p_priority = new JLabel("优先权");
tp_priority = new JTextField(10);
p_memory_size = new JLabel("所需主存大小(MB)");
tp_memory_size = new JTextField(15);
//Bgetp_r = new JButton("置入就绪队列");
BRestore = new JButton("置入后备队列");
random = new JButton("随机产生就绪队列");
p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p1.setBounds(0, 0, 800, 60);
p1.add(p_name);
p1.add(tp_name);
p1.add(p_time);
p1.add(tp_time);
p1.add(p_priority);
p1.add(tp_priority);
p1.add(p_memory_size);
p1.add(tp_memory_size);
//p1.add(Bgetp_r);
p1.add(BRestore);
p1.add(random);
r_queue = new JLabel("就绪队列");// 第二行,队列名面板;
w_queue = new JLabel("后备队列");
s_queue = new JLabel("挂起队列");
memory_gra = new JLabel("内存状态图");
p2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 130, 10));
p2.setBounds(0, 70, 800, 70);
p2.add(r_queue);
p2.add(w_queue);
p2.add(s_queue);
p2.add(memory_gra);
p2.add(aJProgressBar);
Lready = new List(5);
// 第三行,队列状态面板;
//Lready.setSize(200, 40);
Lwaiting = new List(5);
// ?Lwaiting.setSize(200, 40);
Lsuspend = new List(5);
// ?Lsuspend.setSize(200, 40);
L_memory_gra = new List(5);
p3 = new JPanel(new GridLayout(1,4 ,0, 0));
p3.setBounds(0, 400, 800, 100);
p3.add(Lready);
p3.add(Lwaiting);
p3.add(Lsuspend);
//p3.add(L_memory_gra);
BCpuscheduler = new JButton("进行调度");// 第四行,调度面板;
BgetP_s = new JButton("挂起");
Bgetp_s_r = new JButton("解挂");
tcpuinfo = new JTextField("info", 30);
p4 = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 0));
p4.setBounds(0, 520, 800, 70);
p4.add(BCpuscheduler);
p4.add(BgetP_s);
p4.add(Bgetp_s_r);
p4.add(tcpuinfo);
c0.setBounds(140, 330, 15, 10);
c1.setBounds(180, 330, 15, 10);
c2.setBounds(220, 330, 15, 10);
c3.setBounds(260, 330, 15, 10);
c4.setBounds(300, 330, 15, 10);
c5.setBounds(340, 330, 15, 10);
c6.setBounds(380, 330, 15, 10);
c7.setBounds(420, 330, 15, 10);
c8.setBounds(460, 330, 15, 10);
c9.setBounds(500, 330, 15, 10);
c10.setBounds(520, 330, 50, 10);
this.add(p4);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(c0);
this.add(c1);
this.add(c2);
this.add(c3);
this.add(c4);
this.add(c5);
this.add(c6);
this.add(c7);
this.add(c8);
this.add(c9);
this.add(c10);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new MyWindowMonitor());
this.addMouseListener(new MyMouseMonitor());
//BgetLis b1 = new BgetLis();
BCpuLis b2 = new BCpuLis();
BgetP_sLis b3=new BgetP_sLis();
Bgetp_s_rLis b4=new Bgetp_s_rLis();
BRestoreLis b5=new BRestoreLis();
randomLis b6=new randomLis();
//Bgetp_r.addActionListener(b1);
BCpuscheduler.addActionListener(b2);
BgetP_s.addActionListener(b3);
Bgetp_s_r.addActionListener(b4);
BRestore.addActionListener(b5);
random.addActionListener(b6);
}
class MyMouseMonitor extends MouseAdapter{
public void mouseClicked(MouseEvent e){
int x=e.getX();
int y=e.getY();
if(x<=740&&x>=650&&y<=180&&y>=160)
{
Table a=new Table(notables);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -