📄 processor.java~1~
字号:
/**
* 处理机类
*
*/
package process;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.BorderLayout;
//import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JSlider;
import javax.swing.JRadioButton;
import java.awt.event.*;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
/**
* @author 阿赞
*
*/
public class Processor extends JPanel implements Runnable,ActionListener {
private String arithmetic = new String("FCFS _RR");
//private String arithmetic = new String("SPF");
private static int timeslice = 20;//时间片
private final static int pauseTime = 2000;//时间片对应的实际时间,单位为毫秒
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 = new JTable();
JScrollPane jScrollPane1 = new JScrollPane();
Vector header ;
Vector data;
DefaultTableModel tableModel;
LinkedList PCBList = new LinkedList();//就绪队列
Thread thread = null;
//JPanel jPanel = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JSlider jSlider1 = new JSlider();
JRadioButton jRadioButton1 = new JRadioButton("先来先服务",true);
//JRadioButton jRadioButton1 = new JRadioButton("先来先服务");
JRadioButton jRadioButton2 = new JRadioButton("短作业优先");
//JRadioButton jRadioButton2 = new JRadioButton("短作业优先",true);
ButtonGroup Dispatching = new ButtonGroup();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
BorderLayout borderLayout3 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout(2,1);
public Processor() {//构造方法
super(new BorderLayout());
//this.setSize(600,400);
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
//getContentPane().setLayout(borderLayout1);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setVisible(true);
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel1.setPreferredSize(new Dimension(120, 10));
jPanel1.setLayout(borderLayout2);
jSlider1.setOrientation(JSlider.VERTICAL);
jSlider1.setMinimum (0);//设置最小值
jSlider1.setMaximum (100);//设置最大值
jSlider1.setMajorTickSpacing (10);//设置主标号间隔
jSlider1.setMinorTickSpacing (5);//设置辅标号间隔
//设定为显示
jSlider1.setPaintTicks(true);
jSlider1.setPaintLabels(true);
//jSlider1.setPaintTrack (true);
jSlider1.setValue (20);//设置初始值
jSlider1.setUI(new javax.swing.plaf.metal.MetalSliderUI() {// 使用MetalSliderUI为ui
protected void paintHorizontalLabel(Graphics g, int v, Component l) {
JLabel lbl = (JLabel) l;
lbl.setForeground(Color.GREEN);
super.paintHorizontalLabel(g, v, lbl);
}
});
jSlider1.setForeground(Color.BLUE);
jLabel2.setText(" 时间片大小 : "+jSlider1.getValue());
ChangeListener listener = new ChangeListener() {//注册ChangeListener事件
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JSlider) {
jLabel2.setText(" 时间片大小 : "+ ((JSlider) e.getSource()).getValue());
timeslice = jSlider1.getValue();
}
}
};
jSlider1.addChangeListener(listener);//监听jSlider1
jPanel3.setLayout(borderLayout3);
jPanel2.setLayout(gridLayout1);
jPanel2.setPreferredSize(new Dimension(206, 40));
//this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
//this.getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);
add(jPanel1, java.awt.BorderLayout.WEST);
add(jScrollPane1, java.awt.BorderLayout.CENTER);
add(jLabel1, java.awt.BorderLayout.SOUTH);
jLabel1.setText("处理机空闲");
jLabel1.setHorizontalAlignment(JLabel.CENTER);
jLabel1.setFont(new Font("Dialog",0,30));
//jLabel1.setBackground(Color.GREEN);//设置标签颜色
jLabel1.setForeground(Color.RED);//设置字体颜色
jLabel1.setBorder(BorderFactory.createEtchedBorder());//设置标签边框
jLabel1.setOpaque(true); //让组件变为不透明使标签颜色显示出
jScrollPane1.getViewport().add(jTable1);
jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);
jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);
jPanel2.add(jRadioButton1);
jPanel2.add(jRadioButton2);
jPanel3.add(jSlider1, java.awt.BorderLayout.CENTER);
jPanel3.add(jLabel2, java.awt.BorderLayout.SOUTH);
Dispatching.add(jRadioButton1);
Dispatching.add(jRadioButton2);
jRadioButton1.addActionListener(this);
jRadioButton2.addActionListener(this);
SetTable();
thread = new Thread(this);
thread.start();
}
public PCB Create(String id,String address) {
PCB pcb = new PCB(id,address);
PCBList.add(pcb);
return pcb;
}
private void SetTable() {
header = new Vector();
header.addElement("进程ID");
header.addElement("进程状态");
header.addElement("需要处理机时间");
header.addElement("已占用处理机时间");
tableModel = new DefaultTableModel(header, 0);
jTable1.setModel(tableModel);
}
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if(obj == jRadioButton1) {
arithmetic = "FCFS _RR";
System.out.println(arithmetic);
}
else if(obj == jRadioButton2){
arithmetic = "SPF";
System.out.println(arithmetic);
}
}
public void run() {
while (true) {
try {
//int size = PCBList.size();
if(arithmetic.equals("FCFS _RR")) {
int size = PCBList.size();
for (int i = 0; i < size; i++) {
PCB pcb = (PCB) PCBList.get(i); //获取就绪队列的第i个元素
//pcb.SetRuntime_already(timeslice);
pcb.SetState("Running"); //将当前进程的状态改为运行状态
//输出当前进程的PCB的内容
System.out.println("进程ID: " + pcb.GetID());
System.out.println("进程运行所需的总时间: " + pcb.GetRuntime_total());
System.out.println("进程已经运行的时间: " + pcb.GetRuntime_already());
System.out.println("进程的状态: " + pcb.GetState());
System.out.println("进程在内存中的地址: " + pcb.GetAddress());
this.SetTable();
for (int j = 0; j < size; j++) {
PCB pcb1 = (PCB) PCBList.get(j);
data = new Vector();
data.addElement(pcb1.GetID());
data.addElement(pcb1.GetState());
data.addElement(Integer.toString(pcb1.GetRuntime_total()));
data.addElement(Integer.toString(pcb1.GetRuntime_already()));
tableModel.addRow(data);
}
jLabel1.setText("处理机忙," + pcb.GetID() + "占用处理机。");
int sparetime = pcb.GetRuntime_total() - pcb.GetRuntime_already(); //当前进程还需运行的时间
if (sparetime < timeslice) { //当前进程所需的运行时间比时间片小,对进程占用处理机的之间进行变换,使得处理机能够及时被释放
System.out.println("进程" + pcb.GetID() + "占用处理机。");
Thread.sleep(pauseTime / timeslice * sparetime);
} else { //当前进程占用处理机的时间为一个时间片
System.out.println("进程" + pcb.GetID() + "占用处理机。");
Thread.sleep(pauseTime);
}
if (sparetime > timeslice) { //当前进程的时间片到了,且该进程尚未结束,将该进程挂到就绪队列的队尾
pcb.SetRuntime_already(pcb.GetRuntime_already() + timeslice);
pcb.SetState("Ready"); //把进程的状态修改为就绪状态
}
else { //进程处理完毕
PCBList.remove(i); //把处理完的进程的PCB移出就绪队列
size--; //就绪队列长度减一
i--;
System.out.println("进程" + pcb.GetID() + "处理完毕");
System.out.println("进程" + pcb.GetID() + "占用处理机的时间: " +pcb.GetRuntime_total());
//System.out.println();
}
System.out.println();
if (PCBList.size() == 0) { //判断就绪队列是否为空,如果为空则 break
System.out.println("就绪队列为空,处理机空闲。");
jLabel1.setText("处理机空闲");
//jLabel1.setBackground(Color.RED);//设置标签颜色
jLabel1.setForeground(Color.GREEN);//设置字体颜色
this.SetTable();
break;
//return;
}
}
}
else if(arithmetic.equals("SPF")) {
int size = PCBList.size();
for(int i = 0;i<size - 1;i++) {
for(int j = i + 1;j < size;j++) {
if(((PCB) PCBList.get(i)).GetRuntime_total() > ((PCB) PCBList.get(j)).GetRuntime_total()) {
PCB pcb1 = (PCB) PCBList.get(i);
PCB pcb2 = (PCB) PCBList.get(j);
PCBList.remove(i);
PCBList.add(i,pcb2);
PCBList.remove(j);
PCBList.add(j,pcb1);
}
}
}
for(int i = 0;i< size;i++) {
PCB pcb = (PCB) PCBList.get(i); //获取就绪队列的第i个元素
//pcb.SetRuntime_already(timeslice);
pcb.SetState("Running"); //将当前进程的状态改为运行状态
//输出当前进程的PCB的内容
System.out.println("进程ID: " + pcb.GetID());
System.out.println("进程运行所需的总时间: " + pcb.GetRuntime_total());
System.out.println("进程已经运行的时间: " + pcb.GetRuntime_already());
System.out.println("进程的状态: " + pcb.GetState());
System.out.println("进程在内存中的地址: " + pcb.GetAddress());
this.SetTable();
for (int j = 0; j < size; j++) {
PCB pcb1 = (PCB) PCBList.get(j);
data = new Vector();
data.addElement(pcb1.GetID());
data.addElement(pcb1.GetState());
data.addElement(Integer.toString(pcb1.GetRuntime_total()));
data.addElement(Integer.toString(pcb1.GetRuntime_already()));
tableModel.addRow(data);
}
System.out.println("进程" + pcb.GetID() + "占用处理机。");
Thread.sleep((pcb.GetRuntime_total() - pcb.GetRuntime_already()) * 20);
PCBList.remove(i);
size--; //就绪队列长度减一
i--;
}
if (PCBList.size() == 0) { //判断就绪队列是否为空,如果为空则 break
System.out.println("就绪队列为空,处理机空闲。");
jLabel1.setText("处理机空闲");
//jLabel1.setBackground(Color.RED);//设置标签颜色
jLabel1.setForeground(Color.RED);//设置字体颜色
this.SetTable();
break;
//return;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -