📄 memalloc.java
字号:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/*
* Created on 2004-5-19
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author 李秋军(010200)
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class MemAlloc extends JPanel implements Runnable
{
LinkedList memList = new LinkedList();
MemAreaInfo memAreaInfo;
JTextArea text = new JTextArea(20, 12);
JButton bt = new JButton("首次适应算法演示");
private boolean controlFlag = false;
private boolean stopFile = false;
PrintStream ps;
public MemAlloc()
{
init();
bt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if (!controlFlag)
{
text.setText("");
memList.clear();
stopFile = true;
init();
controlFlag = true;
repaint();
}
}
});
this.add(text); //将文本区加入面板
this.add(bt); //将按钮加入面板
}
// 初始化链表
void init()
{
memAreaInfo = new MemAreaInfo(0, 640, false, -1);
memList.add(memAreaInfo);
if (stopFile)
{
try
{
ps =
new PrintStream(
new BufferedOutputStream(
new FileOutputStream("firstMem.txt")));
} catch (IOException e)
{
System.out.println(e);
}
ps.println(" 首次适应算法\r\n");
print("内存初始状态");
}
}
// 分配内存
public boolean alloc(int memSize, int id)
{
for (int i = 0; i < memList.size(); i++)
{
memAreaInfo = (MemAreaInfo) memList.get(i);
if (!memAreaInfo.isAlloc() && memAreaInfo.getLength() >= memSize)
{
if (memAreaInfo.getLength() == memSize)
{
memAreaInfo.setId(id);
memAreaInfo.setAlloc(true);
} else
{
MemAreaInfo newMemArea =
new MemAreaInfo(
memAreaInfo.getStartAddr(),
memSize,
true,
id);
memAreaInfo.setStartAddr(
memAreaInfo.getStartAddr() + memSize);
memAreaInfo.setLength(memAreaInfo.getLength() - memSize);
memList.add(i, newMemArea);
}
break;
}
}
if (stopFile)
{
print("作业" + id + "分配" + memSize + "M内存后");
}
repaint();
return true;
}
// 释入内存
public boolean dealloc(int id)
{
int a = 0, b = 0;
for (int i = 0; i < memList.size(); i++)
{
memAreaInfo = (MemAreaInfo) memList.get(i);
if (memAreaInfo.getId() == id)
{
memAreaInfo.setAlloc(false);
memAreaInfo.setId(-1);
if (i > 0 && !((MemAreaInfo) memList.get(i - 1)).isAlloc())
{
((MemAreaInfo) memList.get(i - 1)).setLength(
((MemAreaInfo) memList.get(i - 1)).getLength()
+ memAreaInfo.getLength());
a = i;
}
if (i < memList.size() - 1
&& !((MemAreaInfo) memList.get(i + 1)).isAlloc())
{
if (a > 0)
{
((MemAreaInfo) memList.get(i - 1)).setLength(
((MemAreaInfo) memList.get(i - 1)).getLength()
+ ((MemAreaInfo) memList.get(i + 1)).getLength());
b = i + 1;
} else if (a == 0)
{
memAreaInfo.setLength(
memAreaInfo.getLength()
+ ((MemAreaInfo) memList.get(i + 1)).getLength());
b = i + 1;
}
}
break;
}
}
if (b > 0)
{
memList.remove(b);
if (a > 0)
{
memList.remove(a);
a = 0;
}
}
if (a > 0)
{
memList.remove(a);
}
if (stopFile)
{
print("作业" + id + "释放内存后");
}
repaint();
return true;
}
public void paintComponent(Graphics g)
{
g.setColor(new Color(123, 193, 193));
g.fillRect(0, 0, 1024, 768);
g.setColor(Color.magenta);
g.drawString("作业情况:", 230, 90);
text.setBackground(new Color(123, 180, 123));
text.setLocation(220, 100);
bt.setLocation(120, 20);
for (int i = 0; i < memList.size(); i++)
{
memAreaInfo = (MemAreaInfo) memList.get(i);
if (memAreaInfo.isAlloc())
{
g.setColor(Color.blue);
g.fillRect(
50,
(memAreaInfo.getStartAddr() + 100) * 2 / 3,
120,
memAreaInfo.getLength() * 2 / 3);
} else
{
g.setColor(Color.white);
g.fillRect(
50,
(memAreaInfo.getStartAddr() + 100) * 2 / 3,
120,
memAreaInfo.getLength() * 2 / 3);
}
g.setColor(Color.magenta);
g.drawString(
String.valueOf(memAreaInfo.getStartAddr()) + "M",
180,
(memAreaInfo.getStartAddr() + 110) * 2 / 3);
}
g.drawString("640M", 180, (640 + 110) * 2 / 3);
g.setColor(Color.blue);
g.fillRect(50, 510, 60, 20);
g.drawString("已分配内存", 120, 523);
g.setColor(Color.white);
g.fillRect(50, 540, 60, 20);
g.drawString("空闲内存", 120, 553);
}
//线程的run()函数
public void run()
{
while (true)
{
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
System.out.println(e);
}
if (controlFlag)
{
repaint();
try
{
alloc(130, 1);
text.append("作业1申请130K\n");
Thread.sleep(1000);
alloc(60, 2);
text.append("作业2申请60K\n");
Thread.sleep(1000);
alloc(100, 3);
text.append("作业3申请100k\n");
Thread.sleep(1000);
dealloc(2);
text.append("作业2释放60K\n");
Thread.sleep(1000);
alloc(200, 4);
text.append("作业4申请200K\n");
Thread.sleep(1000);
dealloc(3);
text.append("作业3释放100K\n");
Thread.sleep(1000);
dealloc(1);
text.append("作业1释放130K\n");
Thread.sleep(1000);
alloc(140, 5);
text.append("作业5申请140K\n");
Thread.sleep(1000);
alloc(60, 6);
text.append("作业6申请60K\n");
Thread.sleep(1000);
alloc(50, 7);
text.append("作业7申请50K\n");
Thread.sleep(1000);
dealloc(6);
text.append("作业6释放60K\n");
if (stopFile)
{
ps.close();
stopFile = false;
}
text.append("\n详细内存分区信息在\n文件(firstMem.txt)中");
} catch (InterruptedException e)
{
System.out.println(e.toString());
}
controlFlag = false;
}
}
}
void print(String s)
{
ps.print("\r\n-----------------" + s + "-----------------\r\n");
ps.println(" 起始地址 长度\t\t目前状态\r\n");
for (int i = 0; i < memList.size(); i++)
{
MemAreaInfo m = (MemAreaInfo) memList.get(i);
ps.println(m);
}
ps.println("\r\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -