Implementation of banker algorithm in Java

Time:2021-3-6

This example for you to share the Java banker algorithm specific code, for your reference, the specific content is as follows

import java.util.Arrays;
import javax.swing.JOptionPane;

public class Banker_Dijkstra {
 Static int available [] = {3,3,2}; // number of available resources 
 Static int Max []] = {{7,5,3}, {3,2,2}, {9,0,2}, {2,2}, {4,3,3}; // maximum demand per thread 
 Static int allocation []] = {0,1,0}, {2,0,0}, {3,0,2}, {2,1,1}, {0,0,2}; // allocated resources 
 Static int need []] = {7,4,3}, {1,2,2}, {6,0,0}, {0,1,1}, {4,3,1}; // demand 
 Static int request [] = New Int [3]; // stores the request resource 
 Static int thread; // thread number 
 static JOptionPane jpane = new JOptionPane();
 // static boolean m;
 public static void main(String[] argv){ 
  int n = 0 ;
  Banker_Dijkstra bd = new Banker_Dijkstra();

  for(int i=0;i<5;i++){
   if(bd.safeState(i)){
    JOptionPane.showMessageDialog (jpane, "system state security");
    n=1;
    break;
   }else{
    n=2;
    continue;   
   }
  }  
  if(n==1){
   bd.getThread();
   }
  else if(n==2){
   JOptionPane.showMessageDialog (jpane, "unsafe system state");
  }
 }

 Protected void getthread() {// enter the test thread number and output the result
  try{
  String xiancheng =  JOptionPane.showInputDialog (jpane, "please enter the thread of the resource application:);
  thread = (int) Integer.parseInt(xiancheng);
  }catch(Exception e){
   int response =  JOptionPane.showConfirmDialog (jpane, "please enter 0-4:", null, JOptionPane.YES_ NO_ OPTION); 
   //Exception handling
   if(response==0){
    getThread();
   }else if(response ==1){
    System.exit(0);
   }
  }

  if(thread<0||thread>4){ 
   JOptionPane.showMessageDialog (jpane, "please enter 0-4:"); 
   getThread(); 
  }else{ 
   for(int i=0;i<3;i++){ 
    String requestR =  JOptionPane.showInputDialog (jpane, "please input the" + (I + 1) + "resource (if not, fill in 0)");
    try{
    request[i]=Integer.parseInt(requestR);}
    catch(Exception e){
     JOptionPane.showConfirmDialog (jpane, "please input the" + (I + 1) + "resource (if not, fill in 0)", null, JOptionPane.YES_ NO_ OPTION);
    }
   } 
   if(request[0]>need[thread][0]||request[1]>need[thread][1]||request[2]>need[thread][2]){ 
    JOptionPane.showMessageDialog (jpane, thread + "the resources requested by the thread exceed the required resources, please re-enter"); 
    getThread(); 
   }else{ 
    if(request[0]> available[0]||request[1]> available[1]||request[2]> available[2]){ 
    JOptionPane.showMessageDialog (jpane, thread + "the resource requested by thread is larger than the system resource, please re-enter"); 
     getThread(); 
    } 
   } 
   //Allocation of resources
   allocateData(thread); 
   //Selection and processing of judgment and simulation
   int tag=0;
   if(check(thread)){ 
    try{
     String str =  JOptionPane.showInputDialog (jpane, "yes / no, continue simulation? ( 1/0 ):");
     tag = Integer.parseInt(str);
     }catch(Exception e){
      JOptionPane.showMessageDialog (jpane, "continue to input (numerical value) 1, do not continue to input (numerical value) 0! ");
     }
     if(tag==1){ 
      recoverData(thread); 
      getThread();
      }
     else{
     if( (JOptionPane.YES_NO_OPTION)==JOptionPane.CANCEL_OPTION)System.exit(0);
     if((JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) recoverData(thread);
     }
   }else{ 
    recoverData(thread); 
    getThread(); 
    }
   }
 }
 //Security algorithm
 private boolean check(int thread2) {
  boolean[] finish = new boolean[5];
   Arrays.fill(finish, false);
  int[] work = new int[3];
  int[] queue = new int[5];
  Int q = 0; // secure sequence subscript
  for(int i = 0;i<3;i++){
   work[i] = available[i];
  }
  int tT = thread2;
  while(tT<5){
   for(int R=0;R<3;R++){
    if((!(finish[tT]==false))||(!(work[R]>=need[tT][R]))){
     tT++;
     break;
    }else{
     if(R==2){
      for(int m =0;m<3;m++){
       work[m] += allocation[tT][m];
      }
      for(int s:work){
       System.out.print(s+" ");
      }
      System.out.println("");
      finish[tT] = true;
      queue[q] = tT;
      q++;
      tT =0;
     }
    }
   } 
  }

  for(int p =0;p<5;p++){
   if(finish[p]==false){
    JOptionPane.showMessageDialog (jpane, "security sequence generation failed");
    return false;
    }
  }

   JOptionPane.showMessageDialog (jpane, "security sequence": + queue [0] + ", + queue [1] +",)
  +queue[2]+","+queue[3]+","+queue[4]);
   return true;

 }
 private boolean safeState(int thread3){
  boolean[] finish = new boolean[5];
   Arrays.fill(finish, false);
   int[] work = new int[3];
   int[] queue = new int[5];
   Int q = 0; // secure sequence subscript
   for(int i = 0;i<3;i++){
    work[i] = available[i];
   }
   int tT = thread3;
   while(tT<5){
    for(int R=0;R<3;R++){
     if((!(finish[tT]==false))||(!(work[R]>=need[tT][R]))){
      tT++;
      break;
     }
     else{
      if(R==2){
       for(int m =0;m<3;m++){
        work[m] += allocation[tT][m];
       }
       finish[tT] = true;
       queue[q] = tT;
       q++;
       tT =0;
      }
     }
//     if((finish[tT]==false)&&(work[R]>=need[tT][R])){
//      for(int m =0;m<3;m++){
//       work[m] += allocation[tT][m];}
//      finish[tT] = true;
//      queue[q] = tT;
//      q++;
//      tT=0;
//      }else{ 
//       tT++;
//       break;
//       }
    } 
   }
   for(int p =0;p<5;p++){
    if(finish[p]==false){
     return false;
     }
   }
    return true;
 }

 Private void recoverdata (int thread2) {// reset allocated resources if generation fails
  for(int i=0;i<3;i++){ 
   //Readjust the number of system resources 
   available[i]+=request[i]; 
   //Computing resources owned by each thread 
   allocation[thread2][i]-=request[i]; 
   //Recalculate requirements 
   need[thread2][i]+=request[i]; 
  } 
 }

 Private void allocatedata (int thread2) {// allocation
  for(int i=0;i<3;i++){ 
   //Readjust the number of available system resources 
   available[i]-=request[i]; 
   //Calculate the resources owned by each thread after allocation 
   allocation[thread2][i]+=request[i]; 
   //Recalculate requirements 
   need[thread2][i]-=request[i]; 
  }  
 }
}

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