怎么用java实现银行家算法
创始人
2024-12-24 18:02:46
0次

java代码实现了银行家算法,界面写的个人认为还是较为细致的,完整的实现了找安全序列等算法功能,可作为参考学习银行家算法。

直接上代码:①界面展示方法:

public void ShowFrame()        {       this.setSize(500, 350);  //大小             this.setAlwaysOnTop(true);       this.setResizable(false);//不可拖动               this.setLayout(new BorderLayout());       this.setTitle("lly_banktest");              jp1=new JPanel();       String s[]= {"Allocation","Max","Available","Request"};       jcb1=new JComboBox(s);       jp1.add(jcb1);           jp1.add(new JLabel("PID:"));       jtf1=new JTextField(3);       jp1.add(jtf1);       jp1.add(new JLabel("A:"));       jtf2=new JTextField(3);           jp1.add(jtf2);       jp1.add(new JLabel("B:"));       jtf3=new JTextField(3);           jp1.add(jtf3);       jp1.add(new JLabel("C:"));       jtf4=new JTextField(3);           jp1.add(jtf4);       jb1=new JButton("确定");       jp1.add(jb1);       jb1.addActionListener(this);       this.add(jp1,"South");                         jta1= new JTextArea();      //显示文件本域           ShowData();           //显示数据            jta1.setLineWrap(true);      //自动适应        int r,g,b;        jta1.setBackground(Color.white);        jta1.setEditable(false);            this.add(jta1,"Center");        // Font f=new Font("Dialog",Font.BOLD,12);  //        jp1.setBackground(new java.awt.Color(128,255,128)); } public void ShowData(){     jta1.setText(" Max \tAllocation       Need    \tAvailable\n");        jta1.append("\n"+"  资源:  " + " A    B   C     " +"   A    B   C  " +          "    A    B   C  " +"    A    B   C");     jta1.append("\n  进程\n   "+pname[0]+"     " +              +Max[0][0]+"    "+Max[0][1]+"   "              +Max[0][2]+"     " +                 "   "+Allocation[0][0]+"    "+Allocation[0][1]           +"    "+Allocation[0][2]+"  " +                 "     "+Need[0][0]+"    "+Need[0][1]           +"   "+Need[0][2]+"  " +                 "    "+Available[0]+"    "+Available[1]+                 "   "+Available[2]);    for(int i=1;i<5;i++)    {    jta1.append("\n\n   "+pname[i]+"  " +          "   "+Max[i][0]+"    "+Max[i][1]+"   "+Max[i][2]+"     " +              "   "+Allocation[i][0]+"    "+Allocation[i][1]        +"    "+Allocation[i][2]+"  " +              "     "+Need[i][0]+"    "+Need[i][1]        +"   "+Need[i][2]+"  " );    }    jtf1.setText("");    jtf2.setText("");    jtf3.setText("");    jtf4.setText("");     }

截图:

②算法实现代码:

public void myAllocation(int i)  //分配资源 {     for (int j=0;jNeed[i][j])      break;        if(Request[j]>Available[j])      break;     else choice=true;    }     return choice;  }   public boolean SaftyCheck(int p)    //安全性检查 {    int k = 0;  boolean b=true;  Work=new int[sno];    //定义工作向量并赋初值  Finish=new boolean[pno];   for(int i=0;iWork[j])  //资源不足,退出      {         flag=false;        break;         }        }     if(flag) //找到资源        {       temp[k++]=i;//存储安全序列        Finish[i]=true;        found=true;             for(int j=0;j

③事件响应函数:

public void actionPerformed(ActionEvent e) {//事件响应函数    if(e.getSource()==jb1){//按下“确定”             if(jcb1.getSelectedItem()=="Request"){              int p=0;         try{       p=Integer.parseInt(jtf1.getText());       Request[0]=Integer.parseInt(jtf2.getText());       Request[1]=Integer.parseInt(jtf3.getText());       Request[2]=Integer.parseInt(jtf4.getText());    }catch(Exception d)    {        JOptionPane.showMessageDialog(this, "您输入有误!请重新输入!");        ShowData();        return;    }    if(p>4) //限定输入进程ID范围        {        JOptionPane.showMessageDialog(this, "PID在0-4之间!");        jtf1.setText("");        return;        }       if(judge(p,Request))//初步分配检查       {                    if(SaftyCheck(p)){//安全性检查                      ShowData();               jta1.append("\n\n 通过安全性检查!安全序列为:");               for(int i=0;i4)        {           JOptionPane.showMessageDialog(this, "进程ID在0-4之间!");           jtf1.setText("");           return;           }             int Maxago[][]= new int[pno][sno];  //暂存最大需求                          Max[p][0]=Integer.parseInt(jtf2.getText());           Max[p][1]=Integer.parseInt(jtf3.getText());           Max[p][2]=Integer.parseInt(jtf4.getText());                    for(int j=0;j=0)                  Need[p][j]=temp;                  else                  {                     JOptionPane.showMessageDialog(this, "最大需求过小!请重新输入!");                      Max=Maxago;                      ShowData();                      return;                  }                                  }             }catch(Exception d)    {        JOptionPane.showMessageDialog(this, "输入有误!请重新输入!");        ShowData();        return;    }       ShowData();              jta1.append("\n\n 最大需求设置成功!");     }               /**    * 选择Allocation时!    */           else if(jcb1.getSelectedItem()=="Allocation"){//设置已分配资源      int p = 0;      try{       p =Integer.parseInt(jtf1.getText());         if(p>4)         {         JOptionPane.showMessageDialog(this, "进程ID在0-4之间!");         jtf1.setText("");         return;         }                           Allocation[p][0]=Integer.parseInt(jtf2.getText());         Allocation[p][1]=Integer.parseInt(jtf3.getText());         Allocation[p][2]=Integer.parseInt(jtf4.getText());}       catch(Exception d)       {           JOptionPane.showMessageDialog(this, "输入有误!请重新输入!");           ShowData();           return;       }         ShowData();                jta1.append("\n\n 已分配资源设置成功!");        } }

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...