基本功能的代码实现 :
可能有隐藏bug。。。
package Algorithm.others;
import java.util.Scanner;
/**
* 银行家算法
* 1.输入各种数据
* 2.循环遍历进程,判断需求量和资源剩余量关系,资源足够就将该进程已分配资源回收,
* 将进程序号记录、打上标记并且重新循环,资源不够就继续循环;
* 3.如果遍历完毕后发现没有能够回收资源的进程,则输出 "出现死锁"
* 4.如果最后全部标记完毕,则完成分析,输出进程运行顺序
*/
public class Bank {
private int ResourceNum;//记录资源数量
private int[] resource;//资源初始总量
private int [][] Alloc;//已分配资源矩阵
private int [][]Max;//最大需求矩阵
private int [][]Need;//需求矩阵
// Max = Alloc + Need
private int pNum;//进程数量
private int []rest;//某次分配后的资源剩余量
private int []result;//可行的分配结果
private boolean []flag;//记录某个进程是否已经完成分配(标记)
private int finish;//已完成分配的进行数量
public Bank() {
this.result=new int[100];
this.rest=new int[100];
this.resource = new int[100];
this.Need = new int[100][100];
this.Max = new int[100][100];
this.Alloc = new int[100][100];
this.flag = new boolean[100];
this.finish = 0;
this.Input();
this.print();
this.work();
}
//获取所有数据
private void Input(){
Scanner in = new Scanner(System.in);
System.out.println("请输入资源数量:");
this.ResourceNum = in.nextInt();
//System.out.println(this.ResourceNum);
System.out.println("请输入进程数量:");
this.pNum = in.nextInt();
System.out.println("请输入每种资源总量:");
for(int i=0;i<this.ResourceNum;++i){
System.out.println("资源 "+(char)(i+65)+" 有:");
this.resource[i] = this.rest[i] = in.nextInt();
}
System.out.println("请输入每个进程的最大资源需求量");
for(int i = 0;i<this.pNum;++i){
System.out.println("进程 p"+i+" 的最大资源需求向量:");
for(int j=0;j<this.ResourceNum;++j){
this.Max[i][j] = in.nextInt();
if(this.Max[i][j]>this.resource[j]){
System.out.println("最大需求量大于总量,无法运行!");
System.exit(1);
}
}
this.flag[i] = false;
}
System.out.println("每个进程的已分配资源矩阵");
for(int i = 0;i<this.pNum;++i){
System.out.println("进程 p"+i+" 的已分配资源向量:");
for(int j=0;j<this.ResourceNum;++j){
this.Alloc[i][j] = in.nextInt();
if(this.Alloc[i][j]>this.resource[j]||this.Alloc[i][j]>this.Max[i][j]){
System.out.println("已分配资源量大于资源总量或需求总量,无法运行!");
System.exit(2);
}
this.rest[j] -=this.Alloc[i][j];
this.Need[i][j] = this.Max[i][j] - this.Alloc[i][j];
}
}
}
//核心算法
private void work(){
int index = 0;//循环遍历进程的下标
while(index<this.pNum){
//判断是否已经分配过
if(this.flag[index]){
index++;
if(index==this.pNum)index = 0;
continue;
}
//先判断剩余量是否满足剩余需求量
boolean f1 = false;
for(int i =0;i<this.ResourceNum;++i){
if(this.rest[i]<this.Need[index][i]){
f1 = true;//不满足
break;
}
}
if(!f1){//满足需求
//先将该进程的已分配资源加入剩余资源
for(int i=0;i<this.ResourceNum;++i){
this.rest[i] += this.Alloc[index][i];
}
//然后将该进程标记,加入结果数组
this.flag[index] = true;
this.result[this.finish++]= index;
//检查是否已经分配完毕
if(this.finish == this.pNum){
System.out.println("分配完毕");
System.out.println("分配结果:");
for(int i=0;i<this.pNum;++i)System.out.print(this.result[i]+" ");
System.exit(0);
}
}
else{//不满足需求
if(index==this.pNum-1){
//如果为最后一个,说明死锁
System.out.println("死锁了");
print();
System.exit(3);
}
index++;
}
}
}
//输出各个矩阵以及数组,用于debug
private void print(){
System.out.println("资源总量");
for(int i=0;i<this.ResourceNum;++i){
System.out.print(this.resource[i]+" ");
}
System.out.println();
System.out.println("资源剩余量");
for(int i=0;i<this.ResourceNum;++i){
System.out.print(this.rest[i]+" ");
}
System.out.println();
System.out.println("进程标记");
for(int i=0;i<this.pNum;++i){
System.out.print(this.flag[i]+" ");
}
System.out.println();
System.out.println("分配结果");
for(int i=0;i<this.pNum;++i){
System.out.print(this.result[i]+" ");
}
System.out.println();
System.out.println("最大需求:");
for(int i =0;i<this.pNum;++i){
for(int j = 0;j<this.ResourceNum;++j){
System.out.print(this.Max[i][j]+" ");
}
System.out.println();
}
System.out.println("剩余需求:");
for(int i =0;i<this.pNum;++i){
for(int j = 0;j<this.ResourceNum;++j){
System.out.print(this.Need[i][j]+" ");
}
System.out.println();
}
System.out.println("已分配:");
for(int i =0;i<this.pNum;++i){
for(int j = 0;j<this.ResourceNum;++j){
System.out.print(this.Alloc[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Bank bank= new Bank();
}
}