package OS;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
主界面
*/
public class Main extends JFrame {
private JTextField numTextField;//进程个数
private JPanel mainPanel;
private JButton createButton;//创建进程按钮
private JButton clearButton;//清空进程按钮
private JButton startButton;//开始
public static DefaultTableModel tableModel;
public JTable processTable;
private int inputNumber = 0;
private JPanel processListJPanel;
public Object[][] object;
public ArrayList<PCB> arraylist = new ArrayList<PCB>();//保存创建的进程信息
public static boolean startTime = true; // 控制总时间的运行
private JLabel startLabel;
private JLabel timeLabel;
private boolean firstRun = true;
private Runnable proc;
private Set<Integer> set = new HashSet<Integer>();//保存进程名称
private Thread p1 = new Thread();
private JComboBox comboBox;
/**
* 界面初始化
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
JFrame frame = new Main();
frame.setTitle("单处理器系统的进程调度");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);//居中
frame.setVisible(true);
}
public Main() throws Exception {
JPanel centerFrame = new JPanel(new GridLayout(1, 2, 10, 10));
mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 25, 5));
// 需要选择的条目
String[] listData = new String[]{"时间片轮转", "优先数", "最短进程优先", "最短剩余时间优先"};
comboBox = new JComboBox<String>(listData);// 创建一个下拉列表框
//行数,居中
numTextField = new JTextField(8);
numTextField.setHorizontalAlignment(SwingConstants.CENTER);
createButton = new JButton("创建进程");
clearButton = new JButton("清空");
startButton = new JButton("开始");
//监听事件
numTextField.addKeyListener(new InputNumListener());
buttonListener buttonListener = new buttonListener();
createButton.addActionListener(buttonListener);
clearButton.addActionListener(buttonListener);
startButton.addActionListener(buttonListener);
startLabel = new JLabel();
timeLabel = new JLabel();
startLabel.setText("运行时间:");
mainPanel.add(comboBox);
mainPanel.add(numTextField);
mainPanel.add(createButton);
mainPanel.add(clearButton);
mainPanel.add(startButton);
mainPanel.add(startLabel);
mainPanel.add(timeLabel);
this.add(mainPanel, BorderLayout.NORTH);
processListJPanel = new JPanel(new BorderLayout(10, 10));
//创建表格
tableModel = new DefaultTableModel();
tableModel.addColumn("进程");
tableModel.addColumn("进程状态");
tableModel.addColumn("优先级");
tableModel.addColumn("服务时间");
tableModel.addColumn("剩余服务时间");
processTable = new JTable(tableModel);
processTable.getColumnModel().getColumn(0).setPreferredWidth(80);
processTable.getColumnModel().getColumn(1).setPreferredWidth(80);
processTable.getColumnModel().getColumn(2).setPreferredWidth(80);
processTable.getColumnModel().getColumn(3).setPreferredWidth(80);
processTable.getColumnModel().getColumn(4).setPreferredWidth(80);
JScrollPane scrollPane = new JScrollPane(processTable);
processListJPanel.add(scrollPane);
centerFrame.add(processListJPanel);
this.add(centerFrame, BorderLayout.CENTER);
}
private class InputNumListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
String text = numTextField.getText();
inputNumber = Integer.parseInt(text);
}
}
private class buttonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == createButton) {
if (numTextField.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(null, "请输入进程个数!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
createProcess(inputNumber);
} else if (e.getSource() == startButton) {
if (arraylist.size() == 0) {
JOptionPane.showMessageDialog(null, "请点击创建进程!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (firstRun) {
firstRun = false;
//开始计时
Runnable time = new TimeRecord(timeLabel);
Thread thread = new Thread(time);
thread.start();
}
//"时间片轮转"
if (comboBox.getSelectedIndex()==0) {
proc = new RR(processTable, arraylist);
p1 = new Thread(proc);
p1.start();
// "优先数"
} else if (comboBox.getSelectedIndex()==1) {
System.out.println("1111");
proc = new Priority(processTable, arraylist);
p1 = new Thread(proc);
p1.start();
//"最短进程优先"
} else if (comboBox.getSelectedIndex()==2) {
proc = new SPN(processTable, arraylist);
p1 = new Thread(proc);
p1.start();
//"最短剩余时间优先"
} else if (comboBox.getSelectedIndex()==3) {
proc = new SRT(processTable, arraylist);
p1 = new Thread(proc);
p1.start();
}
}else if (e.getSource() == clearButton) {
clearRow();
}
}
}
/**
* 创建随机进程
* @param number
*/
void createProcess(int number) {
object = new Object[number + 1][5];
int count1 = number;
int[] processArray = new int[count1 + 1];
int processName = 0; // 进程名称
int i = 1, count = 0;
while (count < count1) {
//1-100随机数
processName = 1 + (int) (Math.random() * 100);
if (!set.contains(processName)) {
set.add(processName);
processArray[i++] = processName;
}
count++;
}
for (i = 1; i <= count1; i++) {
int processPri = (int) (Math.random() * 5); // 优先级
int processTime = 1 + (int) (Math.random() * 5); // 所需时间
PCB pcb = new PCB(processArray[i], 1, processPri, processTime, processTime);
// "进程", "进程状态", "优先级", "服务时间","剩余服务时间"
object[i] = new Object[] { pcb.name, "就绪态", pcb.priority, pcb.total, pcb.time};
tableModel.addRow(object[i]);//向状态区添加一个进度条
arraylist.add(pcb);
if (!firstRun) {
//RR
if (comboBox.getSelectedIndex()==0) {
((RR) proc).size = arraylist.size();
//Priority
} else if (comboBox.getSelectedIndex()==1) {
proc = new Priority(processTable, arraylist);
p1.stop();
p1 = new Thread(proc);
p1.start();
//SPN
} else if (comboBox.getSelectedIndex()==3) {
((SPN) proc).runInterupt = true;
((SPN) proc).map.put(pcb.name, arraylist.size() - 1);
Collections.sort(arraylist, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return ((PCB) o1).time - (((PCB) o2).time);
}
});
SPN.runNum = 0;
((SPN) proc).size = ((SPN) proc).arraylistTemp.size();
//SRT
} else if (comboBox.getSelectedIndex()==3) {
((SRT) proc).map.put(pcb.name, arraylist.size() - 1);
Collections.sort(arraylist, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return ((PCB) o1).time - (((PCB) o2).time);
}
});
SRT.runNum = 0;
((SRT) proc).size = ((SRT) proc).arraylistTemp
.size();
}
}
}
}
private void clearRow() {
DefaultTableModel defaultTableModel = (DefaultTableModel) processTable.getModel();
defaultTableModel.setRowCount(0);
defaultTableModel.fireTableDataChanged();
arraylist = new ArrayList<PCB>();
Main.startTime = true;
firstRun = true;
if (p1.isAlive()) {
p1.stop();
}
//结束进程
if (Thread.currentThread().isAlive()) {
Thread.currentThread().stop();
}
timeLabel.setText("0");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
OperatingSystem.rar (35个子文件)
OperatingSystem
bin
OS
SRT.class 3KB
SPN$1.class 670B
Main$2.class 735B
SRT$1.class 670B
Priority.class 2KB
SPN.class 3KB
Main$InputNumListener.class 1KB
RR.class 2KB
Main.class 8KB
Main$1.class 735B
Main$buttonListener.class 3KB
PCB.class 502B
TimeRecord.class 1KB
META-INF
MANIFEST.MF 46B
.settings
org.eclipse.core.resources.prefs 77B
org.eclipse.jdt.core.prefs 598B
src
OS
RR.java 1KB
TimeRecord.java 685B
PCB.java 403B
Priority.java 2KB
SPN.java 2KB
SRT.java 2KB
Main.java 8KB
META-INF
MANIFEST.MF 46B
.idea
uiDesigner.xml 9KB
misc.xml 242B
modules.xml 277B
encodings.xml 1KB
workspace.xml 4KB
.gitignore 371B
artifacts
OperatingSystem_jar.xml 325B
.project 391B
classes
artifacts
OperatingSystem_jar
OperatingSystem.jar 16KB
.classpath 301B
OperatingSystem.iml 478B
共 35 条
- 1
ノBye~_77
- 粉丝: 13
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- YOLOV4-TINY权重文件
- 以下是一个使用贪心算法解决多机调度问题的基本步骤0.txt
- 基于大数据的房产估价是近年来随着技术的发展而兴起的一种新型估价方法.txt
- 企业供应链管理系统v3.rar
- 富芮坤FR8016HA蓝牙开发板使用手册+硬件PCB图+封装库+DEMO演示软件源代码.zip
- 基于YOLOv7的芯片表面缺陷检测系统
- 京东物流 数字化供应链综合研究报告2018.rar
- 基于YOLOv7的植物虫害识别&防治系统
- 2000.1-2023.8中国经济政策不确定性指数月度数据.xlsx
- Screenshot_2024-04-21-20-42-15-443_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论10