您的位置:首页 > 其它

多级循环队列-进程调度的设计与实现

2013-06-16 02:29 621 查看
//老师要求的实验.要求和书上不同.
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

/*
*  @author Anima_libera
*/
class Proceeding extends JLabel{
public int pid = 0;
public int priority = 0;
public int life = 0;
public boolean status = false;
public Proceeding next = null;
}
public class FeedBack extends JFrame implements ActionListener{
/*
* 程序入口
*/
public static void main(String args[]){
new FeedBack();
}
/*
* 程序变量
*/
private JPanel author = new JPanel();
private JPanel button = new JPanel();
private JPanel display = new JPanel();
private JPanel queue = new JPanel();
private JPanel disA[] = new JPanel[5];

private JLabel authorLab[] = new JLabel[3];
private JLabel que[] = new JLabel[5];

private JButton prior = new JButton();
private JButton FCFS = new JButton();
private JButton run = new JButton();
private JButton exit = new JButton();
private JButton create = new JButton();

private boolean pidA[] = new boolean[100];
private static int Alg = 0;

Timer delay = new Timer(1000, new DelayTime());
Timer runP = new Timer(1000, new RunP());

Proceeding pro[] = new Proceeding[5];

Toolkit toolkit = Toolkit.getDefaultToolkit();

/*
* 构造函数
*/
FeedBack(){
setTitle("多级反馈队列调度算法");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,300);
init();
}
/*
* 初始化
*/
public void init(){
this.setLayout(new BorderLayout());

toolkit.addAWTEventListener(new Key(), AWTEvent.KEY_EVENT_MASK);
initDate();
initAuthor();
initQueue();
initButton();
initDisplay();

setVisible(true);
}
/*
* 初始化作者窗体
*/
public void initAuthor(){
author.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
for(int i = 0;i < authorLab.length; ++ i){
authorLab[i] = new JLabel();
}

authorLab[0].setText("王勇杰");
authorLab[1].setText("20112004037");
authorLab[2].setText("8班");

for(int i = 0;i < authorLab.length; ++ i){
author.add(authorLab[i]);
}

Container con = getContentPane();
con.add(author,BorderLayout.NORTH);
setVisible(true);
}
public void initQueue(){
queue.setLayout(new GridLayout(5,1));

for(int i = 1;i <= que.length;++ i){
que[i - 1] = new JLabel("队列" + i + ":");
queue.add(que[i - 1]);
}

Container con = getContentPane();
con.add(queue,BorderLayout.WEST);
setVisible(true);
}
public void initButton(){
button.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));

prior.setText("优先数算法");
FCFS.setText("先来先服务算法");
run.setText("进入调度循环(R)");
exit.setText("退出循环(Q)");
create.setText("动态创建进程(F)");

prior.addActionListener(this);
FCFS.addActionListener(this);
run.addActionListener(this);
exit.addActionListener(this);
create.addActionListener(this);

button.add(prior);
button.add(FCFS);
button.add(run);
button.add(create);
button.add(exit);

Container con = getContentPane();
con.add(button,BorderLayout.SOUTH);
setVisible(true);
}
public void initDate(){
for(int i = 0;i < pro.length; ++ i){
pro[i] = new Proceeding();
}

for(int i = 0;i < pidA.length;++ i){
pidA[i] = false;
}
}
public void initDisplay(){
display.setLayout(new GridLayout(5,1));
for(int i = 0;i < disA.length;++ i){
disA[i] = new JPanel();
disA[i].setBorder(new BevelBorder(BevelBorder.LOWERED));
display.add(disA[i]);
}
}
/*
* 事件处理
*/
public void actionPerformed(ActionEvent e){
String choice = ((JButton)e.getSource()).getText();
if(choice.equals("优先数算法")){
initDaPan();
Alg = 0;
}
if(choice.equals("先来先服务算法")){
initDaPan();
Alg = 1;
}
if(choice.equals("进入调度循环(R)")){
//runPro();
if(!runP.isRunning())
runP.start();
}
if(choice.equals("动态创建进程(F)")){
createPro();
}
if(choice.equals("退出循环(Q)")){
exitPro();
}
}
/*
* 清空数据和版面
*/
public void initDaPan(){
if(runP.isRunning())
runP.stop();
for(int i = 0;i < pro.length;++ i){
pro[i].next = null;
update(i);
}
for(int i = 0;i < pidA.length;++ i){
pidA[i] = false;
}
}
/*
* exitPro
*/
public void exitPro(){
if(runP.isRunning())
runP.stop();
}
/*
* runPro
*/
public void runPro(){
Proceeding tem;
for(int i = 0;i < pro.length;++ i){
if(pro[i].next != null){
tem = pro[i].next;
tem.status = true;
update(i);

if(! delay.isRunning())
delay.start();
return;
}
}
}
/*
* createPro
*/
public void createPro(){
Proceeding p = new Proceeding();
p.life = (int)((Math.random() * 5) + 1);
while(pidA[p.pid = (int)(Math.random() * 100)]);
pidA[p.pid] = true;
p.priority = (int)(Math.random() * 50);

System.out.println(p.pid + "." + p.priority + "." + p.life);

inQue(p,0);
}
/*
* 从队列中删除
*/
public void delQue(Proceeding p,int q){
Proceeding tem;
//p留作扩展应用
tem = pro[q].next;
while(tem != null){
if(tem.status == true){
pidA[tem.pid] = false;
pro[q].next = tem.next;
tem.next = null;
tem.status = false;
tem.life --;
update(q);
}else{
tem = tem.next;
}
}
}
/*
* 插入就绪队列
*/
public void inQue(Proceeding p,int q){
if(p.life > 0){
Proceeding tem;
if(pro[q].next == null){
pro[q].next = p;
}else{
tem = pro[q];
if(Alg == 0){
while(tem.next != null){
if(tem.next.priority > p.priority)
tem = tem.next;
else break;
}
p.next = tem.next;
tem.next = p;
}else{
while(tem.next != null){
tem = tem.next;
}
tem.next = p;
}
}
update(q);
}
}
/*
* 更新显示
*/
public void update(int q){
disA[q].setVisible(false);
disA[q].removeAll();

Proceeding tem;
tem = pro[q].next;

while(tem != null){
tem.setOpaque(true);
tem.setBorder(BorderFactory.createLineBorder(Color.RED));
tem.setText("pid:" + tem.pid + " life:" + tem.life + " priority:" + tem.priority);
if(tem.status == true){
tem.setBackground(Color.RED);
}else{
tem.setBackground(Color.WHITE);
}
disA[q].add(tem);
tem = tem.next;
}

Container con = getContentPane();
con.add(display,BorderLayout.CENTER);
disA[q].setVisible(true);
setVisible(true);
}
/*
* 运行处理
*/
class RunP implements ActionListener{
public void actionPerformed(ActionEvent e){
boolean flag = false;
for(int i = 0;i < pro.length;i ++){
if(pro[i].next != null){
flag = true;
}
}
if(flag != false)
runPro();
else{
runP.stop();
}
}
}
/*
* 进程处理
*/
class DelayTime implements ActionListener{
public void actionPerformed(ActionEvent e){
Proceeding tem;

for(int i = 0;i < pro.length;++ i){
tem = pro[i].next;
if(pro[i].next != null)
while(tem != null){
if(tem.status == true){
delQue(tem,i);
if(tem.life > 0){
inQue(tem,i + 1);//卡在这里
update(i + 1);
}
update(i);
delay.stop();
return;
}else{
tem = tem.next;
}
}
}
}
}
/*
* 组合键事件
*/
class Key
implements AWTEventListener{
public void eventDispatched(AWTEvent event){
if(event.getClass() == KeyEvent.class){
KeyEvent keyEvent = (KeyEvent)event;
if(keyEvent.getID() == KeyEvent.KEY_PRESSED){
keyPressed(keyEvent);
}
}
}
public void keyPressed(KeyEvent e){
if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_F)
createPro();
if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Q)
exitPro();
if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_R){
if(!runP.isRunning())
runP.start();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: