/**
* @(#)prsf.java
*
*
* @author
* @version 1.00 2010/11/22
*/
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
public class prsf extends JFrame{
JPanel p1=new JPanel();
JButton jb1=new JButton("查看"),
jb2=new JButton("执行"),
jb3=new JButton("退出"),
jb4=new JButton("创建");
JTextArea jt=new JTextArea();
int num=0;
public class s{
private String name;
private int cputime;
private int needtime;
private int priority;
private String state;
private int waittime;
}
s nei_num[]=new s[10];
public prsf() {
super("优先调度算法");
for (int i=0;i<nei_num.length;i++) {
nei_num[i] = new s();
}
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(jb1);
p1.add(jb2);
p1.add(jb3);
p1.add(jb4);
add(p1,BorderLayout.NORTH);
add(new JScrollPane(jt),BorderLayout.CENTER);
al listener=new al();
jb1.addActionListener(listener);
jb2.addActionListener(listener);
jb3.addActionListener(listener);
jb4.addActionListener(listener);
setSize(300,400);
setVisible(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
public void creat(){
String cs=JOptionPane.showInputDialog(null,"请输入要创建进程的个数:");
num=Integer.parseInt(cs);
for(int i=0;i<num;i++)
{ nei_num[i].name= JOptionPane.showInputDialog(null,"请输入name:");
String ss= JOptionPane.showInputDialog(null,"请输入needtime:");
nei_num[i].needtime=Integer.parseInt(ss);
nei_num[i].cputime=0;
nei_num[i].priority=nei_num[i].needtime*6+(num-i)*7;
nei_num[i].state="ready";
}
}
class al implements ActionListener{
public void actionPerformed(ActionEvent e){
if((JButton)e.getSource()==jb1)
chakan();
else if((JButton)e.getSource()==jb2)
zhixing();
else if((JButton)e.getSource()==jb4)
creat();
else if((JButton)e.getSource()==jb3)
System.exit(1);
}
}
public void chakan(){
String stext="name cputime needtime priority state "+'\n';
for(int i=0;i<num;i++)
{
stext+=" "+nei_num[i].name+" "+nei_num[i].cputime+" "+nei_num[i].needtime+" "+nei_num[i].priority+" "+nei_num[i].state+'\n';
}
jt.setText(stext);
}
public void zhixing(){
int max=0,tt=0;
for(int i=0;i<num;i++)
if(nei_num[i].needtime==0)
//nei_num[i].priority=nei_num[i].priority*(nei_num[i].waittime+nei_num[i].needtime)/nei_num[i].needtime;
//else
nei_num[i].state="finish";
for(int i=0;i<num;i++)
if(nei_num[i].needtime!=0&&max<nei_num[i].priority)
{
max=nei_num[i].priority;
tt=i;
}
for(int i=0;i<num;i++)
if(nei_num[i].needtime!=0)
if(tt==i)
{nei_num[i].state="working";
nei_num[i].priority=nei_num[i].priority-3;
nei_num[i].cputime++;
nei_num[i].needtime--;
}
else
{nei_num[i].waittime++;
nei_num[i].state="ready";
}
}
public static void main(String ar[]){
prsf sf=new prsf();
}
}