package cn.hctech2006.concurrent.thread41;
import java.util.Scanner;
public class BankerClass {
//系统可用资源
int [] Available = {10, 8, 7};
//每个用户每一种资源最大需求量
int[][] Max= new int[3][3];
//每个用户每种资源已分配数量
int [][]Alloction = new int[3][3];
//每个用户每种资源剩余需求数量
int [][]Need = new int[3][3];
//当前用户每种资源请求数
int [][]Request = new int[3][3];
//当前可用资源-工作进程
int []Work = new int[3];
//进程编号
int num = 0;//进程编号
Scanner in = new Scanner(System.in);
public BankerClass(){
}
public void setSystemVariable(){
//设置各初始系统变量,并且判断是否处于安全状态
setMax();
setAlloction();
printSystemVariable();
SecurityAlgorithm();
}
/**
* 设置Max矩阵
*/
public void setMax() {
System.out.println("请设置各进程的最大需求矩阵Max:");
for (int i = 0; i < 3; i ++){
System.out.println("请输入进程P"+i+"的最大资源需求量");
for (int j = 0; j < 3; j ++){
Max[i][j] = in.nextInt();
}
}
}
/**
* 设置已分配矩阵Alloction
* 设置可用资源数目Available
*/
public void setAlloction() {
System.out.println("请设置各进程分配矩阵Alloction");
for (int i = 0; i < 3; i ++){
System.out.println("输入进程P"+i+"的分配资源量");
for (int j = 0; j < 3; j ++){
Alloction[i][j] = in.nextInt();
}
}
System.out.println("Available=Available-Allocation");
System.out.println("Need=Max-Alloction");
for (int i = 0; i < 3; i ++){
for (int j = 0; j < 3; j ++){
Available[j] = Available[j]-Alloction[i][j];
}
}
for (int i = 0; i < 3; i ++){
for (int j = 0; j < 3; j ++){
Need[i][j] = Max[i][j]-Alloction[i][j];
}
}
}
/**
* 打印可用系统变量
*/
public void printSystemVariable(){
System.out.println("此时资源分配量如下");
System.out.println("进程 Max Alloction Need Available");
for (int i = 0; i < 3; i ++){
System.out.print("P "+i);
for (int j = 0; j < 3; j ++){
System.out.print(Max[i][j]+" ");
}
System.out.print("|");
for (int j = 0; j < 3; j ++){
System.out.print(Alloction[i][j]+" ");
}
System.out.print("|");
for (int j = 0; j < 3; j ++){
System.out.print(Need[i][j]+" ");
}
System.out.print("|");
if (i == 0){
for (int j = 0; j < 3; j ++){
System.out.print(Available[j]+" ");
}
System.out.print("|");
}
System.out.println();
}
}
// 8 7 5
// 5 2 5
// 6 6 2
// 3 2 0
// 2 0 2
// 1 3 2
/**
* 设置请求资源的进程编号
*/
public void setRequest(){
System.out.println("请输入请求资源的进程编号: ");
num = in.nextInt();
System.out.println("请输入请求各资源的数量");
for (int j = 0; j < 3; j ++){
Request[num][j] = in.nextInt();
}
System.out.println("即进程P"+num+"对各资源的请求Request:" +
"("+Request[num][0]+","+Request[num][1]+","+Request[num][2]+")");
BankerAlgorithm();
}
/**
* 银行家算法
*/
public void BankerAlgorithm(){
boolean T = true;
//判断请求资源是否超出剩余需求资源
if (Request[num][0] <= Need[num][0] && Request[num][1] <= Need[num][1] && Request[num][2] <= Need[num][2]){
//判断需求资源是否超过剩余可用资源
if (Request[num][0] <= Available[0] && Request[num][1] <= Available[1] && Request[num][2] <= Available[2]){
for (int i = 0; i < 3; i ++){
Available[i] -= Request[num][i];
Alloction[num][i] += Request[num][i];
Need[num][i] -= Request[num][i];
}
}else{
System.out.println("当前没有足够的资源可分配,进程P"+num+"需要等待");
T = false;
}
}else{
System.out.println("进程P"+num+"请求已经超过最大需求量Need");
T = false;
}
//如果满足继续贷款条件
if (T == true){
//可用变量打印
printSystemVariable();
System.out.println("现在进入安全算法: ");
SecurityAlgorithm();
}
}
/**
* 安全算法
*/
public void SecurityAlgorithm(){
//初始化Finish
boolean[]finish={false,false,false};
//完成线程数
int count = 0;
//循环圈数
int circle = 0;
//安全序列
int []S=new int[3];
//设置工作向量
for (int i = 0; i < 3; i ++){
Work[i] = Available[i];
}
//是否是头
boolean flag = true;
while(count < 3){
if (flag){
System.out.println("进程 Work Alloction Need Work+Alloction ");
flag = false;
}
//如果放贷过程没有结束并且每种资源当前剩余需求量小于当前可用量,则同意
for (int i = 0; i < 3; i ++){
if (!finish[i] && Need[i][0]<=Work[0]&& Need[i][1]<=Work[1]&& Need[i][2]<=Work[2]){
System.out.print("P"+i+" ");
for (int j = 0; j < 3; j ++){
//当前每一种资源可用量
System.out.print(Work[j]+" ");
}
System.out.print("|");
for (int j = 0; j < 3; j ++){
Work[j] += Alloction[i][j];
//当前已使用资源量
System.out.print(Alloction[i][j]+" ");
}
System.out.print("|");
//放贷过程结束
finish[i] = true;
//添加安全序列
S[count] = i;
count++;
for (int j = 0; j < 3; j ++){
System.out.print(Need[i][j]+" ");
}
System.out.print("|");
for (int j = 0; j < 3; j ++){
System.out.print(Work[j]+" ");
}
System.out.println("|");
}
}
//循环便利次数
circle++;
//如果count==3说明三个线程都满足正好是一个安全队列
if (count == 3){
System.out.println("此时存在一个安全序列: ");
for (int i=0; i< 3; i++){
System.out.print("P"+S[i]+" ");
}
System.out.println("故当前可分配");
break;
}
/**
* 判断完成线程数是否小于循环圈数,避免死锁
*/
if (count < circle){
count=5;
System.out.println("当前系统处于不安全状态,故不存在安全序列");
break;//跳出循环
}
}
}
}
class TestBankerClass{
public static void main(String[] args) {
boolean Choose = true;
String C;
Scanner in = new Scanner(System.in);
BankerClass T = new BankerClass();
System.out.println("这是一个三个进程,初始化系统可用三类资源为{10,8,7}的银行家算法: ");
T.setSystemVariable();
while (Choose){
T.setRequest();
System.out.println("您是否还要进行请求:(y/n)");
C=in.nextLine();
if (C.endsWith("n")){
Choose=false;
}
}
}
}
Java解决银行家问题
最新推荐文章于 2021-12-17 15:34:59 发布