1. 程式人生 > 程式設計 >java實現簡單銀行家演算法

java實現簡單銀行家演算法

本文例項為大家分享了java實現銀行家演算法的具體程式碼,供大家參考,具體內容如下

題目:

初始時,Allocate[i,j]=0,表示初始時沒有程序得到任何資源。假定程序對資源的請求序
列為:

Request(1)[M]=(1,0);
Request(2)[M]=(2,1,1);
Request(3)[M]=(2,1);
Request(4)[M]=(0,2);
Request(2)[M]=(1,1);
Request(1)[M]=(1,1);

請用 Banker 演算法判斷每一次資源請求是否接受,如果接受請求,請給出請求接受後的資
源分配狀態,即 Allocate 矩陣、Need 矩陣和 Available 向量。

大致思路:

(1):判斷該程序資源請求是否小於Need需求矩陣,小於則進第二步
(2):判斷該程序資源請求向量是否小於剩餘資源向量Available,小於則進入第三步
(3):備份下資源狀態矩陣,假設接收該需求,求出相應的資源狀態矩陣,需求矩陣,剩餘資源向量
(4):判斷接收請求後的狀態是否是安全狀態
A:初始該狀態下的程序標識都為false,work為資源剩餘向量
B;迴圈該狀態下的程序,如果滿足標識為false,並且該程序的需求向量小於work 則進入C,當迴圈完畢都沒有滿足條件的進入D。
C:work+Allocate(對應程序的狀態),將該程序對應的程序狀態標識為true,將B的迴圈數變為0,從頭開始迴圈(進入B)

D:迴圈遍歷該狀態下的程序標識,如果都為true則判斷狀態安全,否則判斷狀態不安全
(5):如果狀態是安全的輸入該狀態下的各個矩陣與向量,如果不安全,則利用剛剛備份的資源狀態矩陣,回滾。

執行截圖:

原始碼

package Banker;

public class Banker {
 public static int N = 4;// 執行緒個數
 public static int M = 3;// 資源個數
 public static int[] Resource = { 9,3,6 };// 資源向量;
 public static int[][] Cliam = { { 3,2,2 },{ 6,3 },{ 3,4 },{ 4,2 } };
 public static int[][] Allocate = new int[N][M];
 public static int[][] Need = { { 3,2 } };
 public static int[] Available = { 9,6 };
 public int[][] state = new int[N][M];
 

 
 public static void main(String args[]) {

 Banker ban = new Banker();
 //請求序列陣列,包含第幾個請求,那條程序,請求資源向量。
 int[][][] re={{{1},{1,0}},{{2},{2,1}},{{3},{{4},{0,2}},{{1},1}}};
 for(int j=0;j<re.length;j++){
  /*
  * re[j][1] 請求向量
  * re[j][0][0]-1 第幾個程序
  * j第幾個請求
  */
  ban.judgeqingqiu(re[j][1],re[j][0][0]-1,j);//輸入第幾條程序,請求向向量,第幾個請求,呼叫判斷是否符合要求函式 
 }
 
 
 }


 //判斷請求是否符合要求
 public void judgeqingqiu(int[] Request,int i,int j) {
 /*judgementrequest(Request,i)呼叫函式,判斷該程序請求向量是否小於請求矩陣中對應的向量請求資源
  * judgementrequest(Request,i)呼叫函式,判斷該程序請求向量是否小於剩於資源向量
  */
 if (judgementrequest(Request,i) && judgementrequest(Request,i)) {
  distribute(Request,i);//呼叫假設分配函式,並將分配狀態copy出來
  //judgementsafe(Allocate)判斷是否是安全狀態
  if (judgementsafe(Allocate)) {
  
  System.out.println("############");
  System.out.println("第"+(j+1)+"個請求"+"程序"+(i+1)+"請求資源被允許");
  printJuzhen("Allocate",Allocate);
  printJuzhen("Need",Need);
  PrintXianglaing("Available",Available);
  } else {
  System.out.println("############");
  System.out.println("第"+(j+1)+"個請求"+"程序"+(i+1)+"請求資源被拒絕");
  erWeiCopy(Allocate,state);
  }
 } else {
  System.out.println("*****************");
  System.out.println("第"+(j+1)+"個請求"+"程序"+(i+1)+"請求資源被拒絕");
 }
 }

 // 假設符合,分配資源,記錄下剩餘資源
 public void distribute(int[] Request,int i) {

  state = erWeiCopy(state,Allocate);//將資源分配矩陣保留下來,如果不正確方便回滾
  Allocate = addrequest(Allocate,Request,i);//分配後的資源分配矩陣
  Need = reducerequest(Need,Allocate);//分配後的資源需求矩陣
  Available = AvaileReduceRequest(Available,Allocate);//分配後的資源剩餘矩陣
 }
 
 // 判斷狀態安全函式
 public boolean judgementsafe(int[][] Allocate) {
  int[] work = new int[M];//相當於標記變數,標識程序是否符合,如果符合為true
  work = yiweicopy(work,Available);//將剩餘資源響亮copy到work中
  boolean safe = true;//安全狀態,預設為true
  Boolean[] finish = { false,false,false };//相當於標記變數,標識程序是否符合,如果符合為true,初始值都為false
  //迴圈遍歷該狀態中的程序,判斷程序的資源需求是否小於剩餘資源數
  for (int j = 0; j < N; j++) {
  //程序資源請求是否小於剩餘資源work,並且該程序標識為false,
  if (judgementsafeWork(Need[j],work) && finish[j] == false) {
   finish[j] = true;//,將該程序標識為true,改變work
   for (int h = 0; h < M; h++) {
   work[h] = work[h] + Allocate[j][h];
   }
   j = -1;//,將j=0,再次從頭遍歷檢視程序
  }
  }
  /*
  * 當沒有程序滿足資源請求是否小於剩餘資源work,並且該程序標識為false時
  * 遍歷狀態陣列,看是否都為true
  */
  for (int m = 0; m < N; m++) {
  if (finish[m] == false) {
   safe = false;//如果狀態陣列中有false那麼將safe設定為false
  }
  }
  return safe;
 }

 // 判斷狀態是否安全時程序資源請求是否小於剩餘資源work
 public boolean judgementsafeWork(int[] Request,int[] work) {
  for (int k = 0; k < M; k++) {
//  PrintXianglaing("",Request);
  if (Request[k] >work[k]) {
   return false;
  }
  }
  return true;//返回狀態

 }

 
 // 判斷該程序請求向量是否小於請求矩陣中對應的向量請求資源
 public boolean judgementrequest(int[] Request,int i) {
 
 for (int j = 0; j < M; j++) {
  if (Request[j] > Need[i][j]) {
  return false;
  }
 }
 
 return true;
 }

 // 判斷該程序請求向量是否小於剩於資源向量
 public boolean judgementAvali(int[] Request) {
 for (int j = 0; j < M; j++) {
  if (Request[j] >Available[j]) {
  return false;
  }
 }
 return true;

 }

 // 假設分配後修改資源分配矩陣
 public int[][] addrequest(int[][] Allocate,int[] Request,int i) {

 for (int h = 0; h < M; h++) {
  Allocate[i][h] = Allocate[i][h] + Request[h];
 }

 return Allocate;

 }

 // 假設分配後修改資源的需求矩陣
 public int[][] reducerequest(int[][] Need,int[][] state) {
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  Need[j][h] = Cliam[j][h] - state[j][h];
  }
 }
 return Need;
 }

 // 假設分配後修改資源剩餘矩陣
 public int[] AvaileReduceRequest(int[] Available,int[][] Allocate) {
 Available = yiweicopy(Available,Resource);
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  Available[h] = Available[h] - Allocate[j][h];
  }
 }
 return Available;
 }
 
 // 二維陣列拷貝
 public int[][] erWeiCopy(int[][] x1,int[][] y1) {
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  x1[j][h] = y1[j][h];
  }
 }
 return x1;
 }

 // 一維陣列拷貝
 public int[] yiweicopy(int[] x1,int[] y1) {
 for (int j = 0; j < M; j++) {
  x1[j] = y1[j];
 }
 return x1;
 }

 // 列印向量
 public static void PrintXianglaing(String id,int[] x) {
 System.out.println(id);
 for (int j = 0; j < x.length; j++) {
  System.out.print(x[j] + " ");
 }
 System.out.println("");
 }

 // 列印矩陣
 public static void printJuzhen(String id,int[][] y) {
 System.out.println(id);
 for (int j = 0; j < N; j++) {
  for (int h = 0; h < M; h++) {
  System.out.print(y[j][h] + " ");
  }
  System.out.println();
 }
 }

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。