谁有耐心帮我看看:
代码如下:
package process;
//调度类
import java.util.*;
public class ProcessPCB {
private String pid;
private String pname;
private String status;
private int priority;
private int timepiece;
private String blockreason;
public ProcessPCB(){
}
public ProcessPCB(String pid, String pname){
super();
Random rand = new Random();
this.pid = pid;
this.pname= pname;
status ="READY";
priority = 6 + rand.nextInt(5); //产生6-10之间的随机值
timepiece = 1 + rand.nextInt(5);//产生1-5之间的随机值
blockreason = "I/O request";
}
public String getPid(){
return pid;
}
public void setPid(String pid){
this.pid = pid;
}
public String getPname(){
return pname;
}
public void setPname(String pname){
this.pname = pname;
}
public String getStatus(){
return status;
}
public void setStatus(String status){
this.status = status;
}
public int getPriority(){
return priority;
}
public void setPriority(int priority){
this.priority = priority;
}
public int getTimepiece(){
return timepiece;
}
public void setTimepiece(int timepiece){
this.timepiece = timepiece;
}
public String getBlockReason(){
return blockreason;
}
public void setBlockReason(){
this.blockreason ="I/O request";
}}
package process;
//调度类
import java.util.*;public class Dispatcher {
private List<ProcessPCB> readylist;
private List<ProcessPCB> blocklist;
private List<ProcessPCB> finishlist;
private ProcessPCB executing;
//定义构造函数
public Dispatcher(){
super();
readylist = new LinkedList<ProcessPCB>();
blocklist = new LinkedList<ProcessPCB>();
finishlist = new LinkedList<ProcessPCB>();
executing = null;
}
//创建进程
//public ProcessPCB createProc(int pid, String pname){
//return (new ProcessPCB(pid, pname));
//}
//添加进程到就绪队列(RL),根据优先级做相应的调整。
public void addProcToRL(ProcessPCB pcb){
ProcessPCB temp;
int i;
readylist.add(pcb);
for(i=readylist.size()-2; i>=0; i--){
//比pcb优先级底(优先级数越大)的后移。
temp = readylist.get(i);
if(temp.getPriority() > pcb.getPriority()){
readylist.set(i+1, temp);
}else{
//否则,将pcb插入相应的位置。
readylist.set(i+1, pcb);
break;
}
}
if(i<0){
//刚刚插入的是第一个元素
readylist.set(0, pcb);
}
}
//从就绪队列中取出指优先级最高的进程(即队列中的第一个元素)
public ProcessPCB getProcFromRL(){
if(readylist.size()<=0){
return null;
}
return readylist.remove(0);
}
//将阻塞进程放入阻塞队列(BL)
public void addProcToBL(ProcessPCB pcb){
blocklist.add(pcb);
}
//从阻塞队列中取出一个进程
public ProcessPCB getProcFromBL(){
if(blocklist.size()<=0){
return null;
}
return blocklist.remove(0);
}
//将消亡进程放入消亡队列(FL)
public void addProcToFL(ProcessPCB pcb){
finishlist.add(pcb);
}
//设置正在执行的进程
public void setExecuting(ProcessPCB pcb){
//executing.setPid(pcb.getPid());
//executing.setPname(pcb.getPname());
//executing.setPriority(pcb.getPriority());
//executing.setStatus(pcb.getStatus());
//executing.setTimepiece(pcb.getTimepiece());
//executing.setBlockReason();
executing = pcb;
}
//返回正在执行指令的信息
public ProcessPCB getExecuting(){
return this.executing;
}
//返回就绪队列的信息
public List<ProcessPCB> getReadyList(){
return readylist;
}
public void clearReadyList(){
this.readylist.clear();
}
//返回等待队列的信息
public List<ProcessPCB> getBlockList(){
return blocklist;
}
public void clearBlockList(){
this.blocklist.clear();
}
//返回消亡队列的信息
public List<ProcessPCB> getFinishList(){
return finishlist;
}
public void clearFinishList(){
this.finishlist.clear();
}
}
代码如下:
package process;
//调度类
import java.util.*;
public class ProcessPCB {
private String pid;
private String pname;
private String status;
private int priority;
private int timepiece;
private String blockreason;
public ProcessPCB(){
}
public ProcessPCB(String pid, String pname){
super();
Random rand = new Random();
this.pid = pid;
this.pname= pname;
status ="READY";
priority = 6 + rand.nextInt(5); //产生6-10之间的随机值
timepiece = 1 + rand.nextInt(5);//产生1-5之间的随机值
blockreason = "I/O request";
}
public String getPid(){
return pid;
}
public void setPid(String pid){
this.pid = pid;
}
public String getPname(){
return pname;
}
public void setPname(String pname){
this.pname = pname;
}
public String getStatus(){
return status;
}
public void setStatus(String status){
this.status = status;
}
public int getPriority(){
return priority;
}
public void setPriority(int priority){
this.priority = priority;
}
public int getTimepiece(){
return timepiece;
}
public void setTimepiece(int timepiece){
this.timepiece = timepiece;
}
public String getBlockReason(){
return blockreason;
}
public void setBlockReason(){
this.blockreason ="I/O request";
}}
package process;
//调度类
import java.util.*;public class Dispatcher {
private List<ProcessPCB> readylist;
private List<ProcessPCB> blocklist;
private List<ProcessPCB> finishlist;
private ProcessPCB executing;
//定义构造函数
public Dispatcher(){
super();
readylist = new LinkedList<ProcessPCB>();
blocklist = new LinkedList<ProcessPCB>();
finishlist = new LinkedList<ProcessPCB>();
executing = null;
}
//创建进程
//public ProcessPCB createProc(int pid, String pname){
//return (new ProcessPCB(pid, pname));
//}
//添加进程到就绪队列(RL),根据优先级做相应的调整。
public void addProcToRL(ProcessPCB pcb){
ProcessPCB temp;
int i;
readylist.add(pcb);
for(i=readylist.size()-2; i>=0; i--){
//比pcb优先级底(优先级数越大)的后移。
temp = readylist.get(i);
if(temp.getPriority() > pcb.getPriority()){
readylist.set(i+1, temp);
}else{
//否则,将pcb插入相应的位置。
readylist.set(i+1, pcb);
break;
}
}
if(i<0){
//刚刚插入的是第一个元素
readylist.set(0, pcb);
}
}
//从就绪队列中取出指优先级最高的进程(即队列中的第一个元素)
public ProcessPCB getProcFromRL(){
if(readylist.size()<=0){
return null;
}
return readylist.remove(0);
}
//将阻塞进程放入阻塞队列(BL)
public void addProcToBL(ProcessPCB pcb){
blocklist.add(pcb);
}
//从阻塞队列中取出一个进程
public ProcessPCB getProcFromBL(){
if(blocklist.size()<=0){
return null;
}
return blocklist.remove(0);
}
//将消亡进程放入消亡队列(FL)
public void addProcToFL(ProcessPCB pcb){
finishlist.add(pcb);
}
//设置正在执行的进程
public void setExecuting(ProcessPCB pcb){
//executing.setPid(pcb.getPid());
//executing.setPname(pcb.getPname());
//executing.setPriority(pcb.getPriority());
//executing.setStatus(pcb.getStatus());
//executing.setTimepiece(pcb.getTimepiece());
//executing.setBlockReason();
executing = pcb;
}
//返回正在执行指令的信息
public ProcessPCB getExecuting(){
return this.executing;
}
//返回就绪队列的信息
public List<ProcessPCB> getReadyList(){
return readylist;
}
public void clearReadyList(){
this.readylist.clear();
}
//返回等待队列的信息
public List<ProcessPCB> getBlockList(){
return blocklist;
}
public void clearBlockList(){
this.blocklist.clear();
}
//返回消亡队列的信息
public List<ProcessPCB> getFinishList(){
return finishlist;
}
public void clearFinishList(){
this.finishlist.clear();
}
}
//视图类
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*;
public class ProcFrame extends JFrame{
JLabel ready = new JLabel("就绪队列");
JLabel block = new JLabel("阻塞队列");
JLabel finish = new JLabel("完成队列");
JLabel executing = new JLabel("正在执行的进程");
JLabel pid = new JLabel("进程标识符");
JLabel pname = new JLabel("进程名");
JTextArea readytext = new JTextArea();
JTextArea blocktext = new JTextArea();
JTextArea finishtext = new JTextArea();
JTextArea executingtext = new JTextArea();
JTextField pidtext = new JTextField();
JTextField pnametext = new JTextField();
JButton create = new JButton("创建进程");
JButton request = new JButton("申请I/O");
JButton wakeup = new JButton("唤醒进程");
JButton dispatch = new JButton("开始调度");
JButton reset = new JButton("重置系统");
JLabel proc_id =new JLabel("进程标识符");
JLabel proc_name = new JLabel("进程名");
JLabel proc_priority = new JLabel("优先级");
JLabel remained_time = new JLabel("剩余时间");
//LabelBar lb= new LabelBar();
//调度类
Dispatcher dispatcher = new Dispatcher();
List<ProcessPCB> rl;
//显示函数
public void printProc(List<ProcessPCB> lst, JTextArea jta){
ProcessPCB pcb;
String space = " ";
jta.setText(""); //将原来的文本删除
for(int i=0; i<lst.size(); i++){
pcb = (ProcessPCB)lst.get(i);
jta.append(pcb.getPid() + space + pcb.getPname() + space + pcb.getPriority() + space + pcb.getTimepiece());
jta.append("\n");
}
repaint();
}
//创建进程函数
public void createProcess(){
if(pidtext.getText().equals("")||pname.getText().equals("")){
JOptionPane.showMessageDialog(null,"请输入进程名和进程标识符!","Message", JOptionPane.INFORMATION_MESSAGE);
}else{
ProcessPCB process = new ProcessPCB(pidtext.getText(), pnametext.getText());
dispatcher.addProcToRL(process);
//在就绪队列中显示变化后的信息。
printProc(dispatcher.getReadyList(), readytext);
}
pidtext.setText("");
pnametext.setText("");
}
//请求I/O
public void requestIO(){
ProcessPCB pcb;
if(dispatcher.getExecuting()==null){
JOptionPane.showMessageDialog(null, "当前没有正在执行的进程!", "Message", JOptionPane.INFORMATION_MESSAGE);
}else{
pcb = dispatcher.getExecuting();
executingtext.setText("");
//修改状态
pcb.setStatus("BLOCK");
//添加到阻塞队列
dispatcher.addProcToBL(pcb);
//显示
printProc(dispatcher.getBlockList(), blocktext);
//重新调度
//dispatch();
}
}
//唤醒进程
public void wakeupProc(){
ProcessPCB temp;
if(dispatcher.getBlockList().size()>0){
//修改状态为READY
temp = dispatcher.getBlockList().remove(0);
//显示变化后的进程
printProc(dispatcher.getBlockList(), blocktext);
temp.setStatus("READY");
dispatcher.addProcToRL(temp);
//显示变化后的进程
printProc(dispatcher.getReadyList(), readytext);
}else{
JOptionPane.showMessageDialog(null, "阻塞队列里没有可唤醒的进程!", "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
//调度程序
public void dispatch(){
ProcessPCB temp;
String space = " ";
Random rand = new Random();
while(dispatcher.getReadyList().size()>0){
temp = dispatcher.getReadyList().remove(0);
printProc(dispatcher.getReadyList(), readytext);
temp.setStatus("Executing");
dispatcher.setExecuting(temp);
executingtext.setText(temp.getPid() + space + temp.getPname() + space + temp.getPriority() + space + temp.getTimepiece());
//延时
repaint();
//for(int i = 0; i < 10000; i++)
// for(int j = 0; j < 10000; j++){
// int k = 1;
// }
try{
synchronized(this){
this.wait(2000); //延时2秒
}
}catch(InterruptedException e){} //随机阻塞机制
if(rand.nextInt(1000)<500){
requestIO();
continue;
}
temp.setPriority(temp.getPriority()+1);
temp.setTimepiece(temp.getTimepiece()-1);
if(temp.getTimepiece()<=0){
temp.setStatus("FINISH");
dispatcher.addProcToFL(temp);
printProc(dispatcher.getFinishList(), finishtext);
}else{
temp.setStatus("READY");
dispatcher.addProcToRL(temp);
printProc(dispatcher.getReadyList(), readytext);
}
}
if(dispatcher.getReadyList().size()<=0){
JOptionPane.showMessageDialog(null, "就绪队列里没有进程!", "Message", JOptionPane.INFORMATION_MESSAGE);
}
dispatcher.setExecuting(null);
executingtext.setText("");
}
//重置系统
public void resetSystem(){
readytext.setText("");
blocktext.setText("");
finishtext.setText("");
executingtext.setText("");
repaint();
dispatcher.clearReadyList();
dispatcher.clearBlockList();
dispatcher.clearFinishList();
dispatcher.setExecuting(null);
}
public ProcFrame(String s){
super(s);
Container cp = getContentPane();
cp.setLayout(null);
ready.setBounds(10, 10, 100, 25);
//lb.setBounds(10, 35, 300, 25);
proc_id.setBounds(10, 35, 75, 25);
proc_name.setBounds(85, 35, 75, 25);
proc_priority.setBounds(160, 35, 75, 25);
remained_time.setBounds(235, 35, 75, 25);
readytext.setBounds(10, 60, 300, 50);
block.setBounds(10, 110, 100, 25);
//lb[1].setBounds(0, 135, 300, 25);
blocktext.setBounds(10, 160, 300, 50);
finish.setBounds(10, 210, 100, 25);
//lb[2].setBounds(0, 235, 300, 25);
finishtext.setBounds(10, 260, 300, 50);
executing.setBounds(10, 310, 150, 25);
//lb[3].setBounds(0, 335, 300, 25);
executingtext.setBounds(10, 360, 300, 20);
pid.setBounds(350, 35, 100, 25);
pidtext.setBounds(350, 65, 100, 25);
pname.setBounds(350, 100, 100, 25);
pnametext.setBounds(350, 130, 100, 25);
create.setBounds(350, 200, 100, 30);
request.setBounds(350, 240, 100, 30);
wakeup.setBounds(350, 280, 100, 30);
dispatch.setBounds(350, 320, 100, 30);
reset.setBounds(350, 360, 100, 30);
cp.add(ready);
//cp.add(lb);
cp.add(proc_id);
cp.add(proc_name);
cp.add(proc_priority);
cp.add(remained_time);
cp.add(readytext);
cp.add(block);
//cp.add(lb[1]);
cp.add(blocktext);
cp.add(finish);
//cp.add(lb[2]);
cp.add(finishtext);
cp.add(executing);
//cp.add(lb[3]);
cp.add(executingtext);
cp.add(pid);
cp.add(pidtext);
cp.add(pname);
cp.add(pnametext);
cp.add(create);
cp.add(request);
cp.add(wakeup);
cp.add(dispatch);
cp.add(reset);
//下面为各个按钮注册时间监听器并定义处理事件的方法
create.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
createProcess();
}
});
request.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
requestIO();
}
});
wakeup.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
wakeupProc();
}
});
dispatch.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
dispatch();
}
});
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
resetSystem();
}
});
//readytext.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent ae){
// readyPrint();
// }
//})
}
public static void main(String args[]){
ProcFrame pf = new ProcFrame("进程状态转换模拟系统");
pf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
pf.setSize(500, 450);
pf.setVisible(true);
}
}
现在想在模拟进程调度时,动态显示各个textarea的内容,但现在显示不了。
@1 点击“创建进程按钮”创建三个进程,比如,在进程标示符里输入“1”,在进程名里输入“process1”;
同理,创建“2”,“process2”,“3”,“process3”。@2 点击“开始调度”按钮,本来延时是为了让各个进程动态的显示,如正在执行的进程显示在“正在执行的进程”文本区里,可是现在这个变化过程看不到。只有当所有进程执行完后或全部阻塞后,才会显示。可是现在当系统在延时(wait)的时候,文本区里的内容并不显示。