📄 rr.java
字号:
/*
RR时间片轮转调度算法
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RR extends JFrame implements ActionListener,Runnable{
Thread myThread;
JPanel p1=new JPanel(); /*就绪*/
JPanel p2=new JPanel(); /*控制台*/
JPanel p3=new JPanel(); /*完成*/
JPanel p4=new JPanel(); /*左边*/
JButton []b=new JButton[10]; /*就绪队列*/
JButton []d=new JButton[10]; /*完成队列*/
JButton jbBegin,jbAdd,jbSuspend;
public int [][]pcb=new int [10][5]; /*最多10个进程。存放进程信息:第一列表示所需时间片,第二列表示已执行与否,第三列表示剩下的时间片,第四列表示到达时间,第五列进程ID*/
public final static int pT=20; /*单位时间片长度 Piece of Time*/
public int curTime=0; /*作为程序运行时的时间*/
public int pcbCount=0; /*进程数量,最多10个进程*/
boolean Continue=false; /*开始执行标志*/
boolean Susp=false; /*增加进程标志*/
public RR(){
/*线程*/
myThread=new Thread(this);
myThread.start();
/*程序窗口初始化*/
JFrame f=new JFrame();
f.setTitle("时间片轮转调度算法(RR)");
f.setSize(650,450);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setLayout(new GridLayout(1,2));
/*进程信息初始化*/
for(int i=0;i<10;i++){
/*
if(i<5){
int tNeed=(int)(Math.random()*888);
pcb[i][0]=tNeed;
pcb[i][1]=1; //未执行,显示
b[i]=new JButton(" 进程"+(i+1)+" 需要时间"+tNeed+" ");
}
*/
//else{
pcb[i][1]=0; /*无进程,不显示*/
b[i]=new JButton();
//}
}
p1.setVisible(true);
p1.setLayout(new GridLayout(10,1));
for(int i=0;i<10;i++)
p1.add(b[i]);
/*初始化“添加进程”和“开始”按钮*/
jbBegin=new JButton(" 开始 ");
jbAdd=new JButton(" 添加进程 ");
jbSuspend=new JButton(" 阻塞 ");
p2.setVisible(true);
p2.setLayout(new GridLayout(3,1));
p2.add(jbBegin);
p2.add(jbAdd);
p2.add(jbSuspend);
f.add(p1);
f.add(p2);
/*添加事件监听*/
jbBegin.addActionListener(this);
jbAdd.addActionListener(this);
jbSuspend.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == jbBegin){
if(Continue==false)
Continue=true;
}
if (e.getSource() == jbAdd){
if(pcbCount<10){
int tNeed=(int)(Math.random()*100);
pcb[pcbCount][0]=tNeed;
pcb[pcbCount][1]=1;
pcb[pcbCount][2]=tNeed;
pcb[pcbCount][3]=curTime;
pcb[pcbCount][4]=pcbCount+1;
b[pcbCount].setText(" 进程"+(pcbCount+1)+" 到达时间"+curTime+" 需要时间"+tNeed+" ");
pcbCount++;
curTime++;
}
}
if (e.getSource() == jbSuspend){
Susp=true;
}
}
public void run(){
while(true){
if(Continue==true){
int select=0;
//选出最先到达的进程,冒泡排序
for(int i=0;i<pcbCount;i++){
for(int j=0;j<pcbCount-i-1;j++){
if(pcb[j][3]>pcb[j+1][3]){
int temp[]=pcb[j];
pcb[j]=pcb[j+1];
pcb[j+1]=temp;
}
}
}
//重新显示就绪队列
for(int j=0;j<10;j++){
if(pcb[j][1]==1)
b[j].setText(" 进程"+(pcb[j][4])+" 到达时间"+pcb[j][3]+" 需要时间"+pcb[j][0]+" "+" 剩余时间"+pcb[j][2]);
if(pcb[j][1]==2)
b[j].setText(" 进程"+(pcb[j][4])+" 执行完毕");
}
/*已执行的进程不能再执行,置select 为-1作为标志*/
if(pcb[0][1]==2)
select=-1;
//被选中而开始执行的进程,则不再在队列中显示,把显示标记置1
else
pcb[0][1]=1;
//放到开始按钮(CPU)上显示
if(select!=-1){
for(int j=1;j<=pT;j++){
if(Susp==true){
jbBegin.setText("处理I/O请求,"+"进程"+(pcb[0][4])+" 阻塞");
try{
Thread.sleep(8888);
}catch(InterruptedException e){};
Susp=false;
break;
}
else{
curTime++;
if(pcb[0][2]>=0)
jbBegin.setText("进程"+(pcb[0][4])+" 需要时间"+pcb[0][0]+" 正在执行: "+pcb[0][2]--);
else
break;
try{
Thread.sleep(100);
}catch(InterruptedException e){};
}
}
pcb[0][3]=curTime;
}
/*如果已经执行完*/
if(pcb[0][2]<0){
pcb[0][1]=2;
pcb[0][3]=1000000;
}
}
}
}
public static void main(String []args){
new RR();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -