?? rr.java
字號:
/*
*聯系方式:beijihu3@163.com
*/
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 + -