怎么用java实现银行家算法

发布时间:2021-08-12 18:08:35 作者:chen
来源:亿速云 阅读:99

本篇内容介绍了“怎么用java实现银行家算法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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;j<sno;j++)  {  Available[j]=Available[j]-Request[j];  Allocation[i][j]=Allocation[i][j]+Request[j];  Need[i][j]=Need[i][j]-Request[j];  } }   public boolean judge(int i, int Request[] )  //初步检查是否有足够资源   {     boolean choice=false;    for(int j=0;j<sno;j++){     if(Request[j]>Need[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;i<sno;i++)   {   Work[i]=Available[i];   }  for(int i=0;i<pno;i++)   Finish[i]=false; //初值为false   Finish[p]=true; //初次检查  for(int j=0;j<sno;j++)//释放资源  {  Work[j]=Work[j]+Allocation[p][j];  }  temp[k++]=p;  boolean found = false;//标记是否找到安全进程     while(k<pno-1){    //遍历查找安全序列  for(int i=0;i<pno;i++)   {      boolean flag=true;//标记是否有足够资源     if(Finish[i]) continue ;      for(int j=0;j<sno;j++)     {            if(Need[i][j]>Work[j])  //资源不足,退出      {         flag=false;        break;         }        }     if(flag) //找到资源        {       temp[k++]=i;//存储安全序列        Finish[i]=true;        found=true;             for(int j=0;j<sno;j++)//释放资源         Work[j]=Work[j]+Allocation[i][j];        }       }    if(found)     {   found=false;    }    else break;//遍历,试分配失败跳出   }    for(int i=0;i<pno;i++){  //若存在false,则跳出     if(!Finish[i])      {         b=false;         break;        }  }  return b; }

③事件响应函数:

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;i<pno;i++)//打印安全序列               jta1.append("P"+String.valueOf(temp[i])+" ");               jta1.append(" 批准请求,资源已分配!");               myAllocation(p);//检查到安全序列才分配           }else //不安全              {                          ShowData();                  //jta1.append("\n\n  找不到安全序列!  不批准请求!");                  JOptionPane.showMessageDialog(this, "找不到安全序列!  不批准请求!");              }       }else{         ShowData();           jta1.append("\n\n 系统资源不足!");        }          }   /***    * 选择avaliable时!        */               else if(jcb1.getSelectedItem()=="Available"){//设置可用资源        try{           Available[0]= Integer.parseInt(jtf2.getText());           Available[1]=Integer.parseInt(jtf3.getText());           Available[2]=Integer.parseInt(jtf4.getText());    }catch(Exception d)    {        JOptionPane.showMessageDialog(this, "您输入有误!请重新输入!");        ShowData();        return;    }      ShowData();        jta1.append("\n\n  可用资源设置成功!");        }          /**    * 选择Max时!     */       else if(jcb1.getSelectedItem()=="Max"){//设置最大需求源             int p = 0;           try{           p =Integer.parseInt(jtf1.getText());           if(p>4)        {           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<sno;j++){   //判断设置是否合理                  int temp =Max[p][j]-Allocation[p][j];                  if(temp>=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 已分配资源设置成功!");        } }

“怎么用java实现银行家算法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 使用java实现银行家算法的示例
  2. 使用java实现银行家算法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:yum安装包找不到是什么情况

下一篇:mysql视图的概念和用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》