Implementation of banker algorithm in Java (swing interface)

Time:2021-3-3

Java code to achieve the banker’s algorithm, the interface is written in a more detailed way, complete realization of the search for security sequence algorithm function, can be used as a reference to learn the banker’s algorithm.

Direct code: ① interface display method:

public void ShowFrame()       
 {
       this.setSize (500, 350); // size      
       this.setAlwaysOnTop(true);
       this.setResizable (false); // cannot be dragged        
       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 ("OK");
       jp1.add(jb1);
       jb1.addActionListener(this);
       this.add(jp1,"South");                 
        Jta1 = new jtextarea(); // displays the file field   
        Showdata(); // display data    
        Jta1. Setlinewrap (true); // Auto adapt
        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" + "resource: +" a B C "+" a B C "+
          "    A    B   C  " +"    A    B   C"); 
    Jta1. Append ("\ nprocess / 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("");
    
 }

Screenshot:

② Algorithm implementation code:

Public void myallocation (int i) // allocate resources
 {  
   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) // preliminarily check whether there are enough resources  
 { 
    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 safetycheck (int p) // security check
 {  
  int k = 0;
  boolean b=true;
  Work = New Int [SnO]; // define work vector and assign initial value
  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; // the initial value is false 
  Finish [P] = true; // initial inspection
  For (int j = 0; J < SnO; j + +) // release resources
  {
  Work[j]=Work[j]+Allocation[p][j];
  }
  temp[k++]=p;
  Boolean found = false; // mark whether the secure process is found 
  
  While (k < pno-1) {// traverses to find secure sequences
  for(int i=0;i<pno;i++)
   {
      Boolean flag = true; // mark whether there are enough resources
     if(Finish[i]) continue ; 
     for(int j=0;j<sno;j++)
     {      
      If (need [i] [J] > work [J]) // insufficient resources, exit
      {
         flag=false;
        break;
         }
        }
     If (flag) // find the resource
        {
       Temp [K + +] = I; // store security sequence
        Finish[i]=true;
        found=true;     
        For (int j = 0; J < SnO; j + +) // release resources
         Work[j]=Work[j]+Allocation[i][j];
        }  
     }
    if(found) 
    {
   found=false;
    }
    Else break; // traverse, try to allocate, fail to jump out 
  }
  
  For (int i = 0; I < PNO; I + +) {// if there is false, jump out
     if(!Finish[i])
      {
         b=false;
         break;
        }
  }
  return b;
 }

Screenshot:

③ Event response function:

public void actionPerformed(ActionEvent e) {
//Event response function
    If (e.getsource() = = JB1) {// press OK      
       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, "your input is wrong! Please input again! ");
        ShowData();
        return;
    }
    If (P > 4) // limit the input process ID range
        {
        JOptionPane.showMessageDialog (this, "PID is between 0-4! ");
        jtf1.setText("");
        return;
        }
       If (judge (P, request)) // preliminary allocation check
       {         
           If (safetycheck (P)) {// security check         
             ShowData();
               Jta1. Append ("- security check passed! The security sequence is ";";
               For (int i = 0; I < PNO; I + +) // print security sequence
               jta1.append("P"+String.valueOf(temp[i])+" ");
               Jta1. Append ("approve request, resource allocated! ");
               Myallocation (P); // assign only after checking the security sequence
           }Else // unsafe
              {             
             ShowData();
                  //Jta1. Append ("- no secure sequence found! Don't approve the request! ");
                  JOptionPane.showMessageDialog (this, "no secure sequence found! Don't approve the request! ");
              }
       }else{
         ShowData();
           Jta1. Append ("- insufficient system resources! "); 
       }      
    }
   /***
    *When avaliable is selected!    
    */
       
    
    Else if (jcb1. Getselecteditem() = = available ") {// set available resources 
       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, "your input is wrong! Please input again! ");
        ShowData();
        return;
    }
      ShowData();  
      Jta1. Append ("- set available resources successfully! ");    
    }
   
       /**
    *When choosing Max! 
    */
    
   Else if (jcb1. Getselecteditem() = = max ") {// set the maximum demand source      
       int p = 0;    
       try{
           p =Integer.parseInt(jtf1.getText());
           if(p>4)
        {
           JOptionPane.showMessageDialog (this, "process ID is between 0-4! ");
           jtf1.setText("");
           return;
           }  
           Int maxago] [] = New Int [PNO] [SnO]; // temporary storage of maximum demand               
           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 + +) {// judge whether the setting is reasonable
                  int temp =Max[p][j]-Allocation[p][j];
                  if(temp>=0)
                  Need[p][j]=temp;
                  else
                  {
                     JOptionPane.showMessageDialog The maximum demand is too small! Please input again! ");
                      Max=Maxago;
                      ShowData();
                      return;
                  }                    
              }         
    }catch(Exception d)
    {
        JOptionPane.showMessageDialog (this, "input error! Please input again! ");
        ShowData();
        return;
    }  
     ShowData();         
     Jta1. Append ("- set the maximum requirement successfully! "); 
    }
     
       
   /**
    *When selecting allocation!
    */
       
    Else if (jcb1. Getselecteditem() = = allocation ") {// set allocated resources
      int p = 0;
      try{
       p =Integer.parseInt(jtf1.getText());
         if(p>4)
         {
         JOptionPane.showMessageDialog (this, "process ID is between 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, "input error! Please input again! ");
           ShowData();
           return;
       }  
       ShowData();         
       Jta1. Append ("- set the allocated resource successfully! "); 
       }
 
}

Screenshot:

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Practice analysis of rust built-in trait: partialeq and EQ

Abstract:Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. This article is shared from Huawei cloud community《Analysis of rust built-in trait: partialeq and EQ》Author: debugzhang Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. Some traits can be automatically […]