有关Java的银行家算法

      如果你有更好的建议,感谢你能给我提出来吗?

package com.java.bankAlogrithm;


import javax.swing.JOptionPane;

public class Bank {
 private final int MAXRESOURCE=200;//系统最大的资源数
 private int available[];//可利用的资源
 private int max[][];//最大需求矩阵
 private int allocation[][];//以分配的资源
 private int need[][];//需求的资源
 private boolean finish[];//true  表示完成       false  表示完成
 private int p[];//记录安全序列
 private int process;//进程数
 private int source;//资源数
 private String str;//方便全部显示到面板上
 
 //开始输入必要的数据;
 public Bank(){
  //首先要确定进程数目和资源种类

  process=Integer.parseInt(JOptionPane.showInputDialog("请输入进程的数目"));
  source=Integer.parseInt(JOptionPane.showInputDialog("请输入资源种类"));
  
  
  //输入最大需求矩阵

  JOptionPane.showMessageDialog(null, "请输入一个"+process+"行"+source+"列的最大需求矩阵");
  max=new int[process][source];
  for(int i=0;i<process;i++){
   for(int j=0;j<source;j++){
    String sle=JOptionPane.showInputDialog("请输入第"+i+"个进程"+"第"+j+"种最大需求资源");
    max[i][j]=Integer.parseInt(sle);
   }
  }
  //输入分配矩阵
 
  
  JOptionPane.showMessageDialog(null, "请输入一个"+process+"行"+source+"列的分配矩阵");
  allocation=new int[process][source];
  for(int i=0;i<process;i++){
   for(int j=0;j<source;j++){
    String sle=JOptionPane.showInputDialog("请输入第"+i+"个进程"+"第"+j+"种分配资源");
    allocation[i][j]=Integer.parseInt(sle);
   }
  }
  
  //输入可用资源

  JOptionPane.showMessageDialog(null, "请输入可用资源");
  available=new int[MAXRESOURCE];
  for(int i=0;i<source;i++){
   String sle=JOptionPane.showInputDialog("请输入第"+i+"种可用资源");
   available[i]=Integer.parseInt(sle);
  }
  //计算尚需资源
  need=new int[process][source];
  for(int i=0;i<process;i++){
   for(int j=0;j<source;j++){
    need[i][j]=max[i][j]-allocation[i][j];
    if(need[i][j]<0){
     JOptionPane.showMessageDialog(null, "输入错误,需求资源不能是复数");
    }
   }
  }
  
  print();
 }
 
 //打印输入的序列
 public void print(){
  str+="进程     max/t/tallocation/t  need/t/tavailable/n";
  str+="P0  ";
  for (int i = 0; i <source; i++) {
   str+=max[0][i]+"   ";
  }
  str+="  ";
  for (int i = 0; i <source; i++) {
   str+=allocation[0][i]+"   ";
  }
  str+="  ";
  for (int i = 0; i <source; i++) {
   str+=need[0][i]+"   ";
  }
  str+="  ";
  for (int i = 0; i <source; i++) {
   str+=available[i]+"   ";
  }
  str+="/n";
  for (int i = 1; i < process; i++) {
   str+="P"+i+"  ";
   for (int j = 0; j < source; j++) {
    str+=max[i][j]+"   ";
   }
   str+="  ";
   for (int j = 0; j < source; j++) {
    str+=allocation[i][j]+"   ";
   }
   str+="  ";
   for (int j = 0; j < source; j++) {
    str+=need[i][j]+"   ";
   }
   str+="/n";
  }
 }
 
 //检测安全序列是否安全
 public void isSafe() {
 
  int[] work = new int[source];
  for (int i = 0; i < work.length; i++) {
   work[i] = available[i];
  }
  finish = new boolean[process];
  for (int i = 0; i < process; i++) {//开始把进程全部置未分配状态  都为false;
   finish[i] = false;
  }
  p = new int[process];
  
  int num = 0;//对每个进程都要把所有资源都进行比较
  int count = 0;//记录可以分配的序列
  int count1 = 0;//记录所有序列是否分配
  
   for(int i=0;i<process-1;i++){
   for (int j = 0; j < process; j++) {
    if (finish[j] == false) {
     for (int k = 0; k < source; k++) {
      if (need[j][k] <= work[k]) {//比较一个进程的各种资源是否满足条件
       num++;
      }
     }
     if (num == source) {//如果一个进程所有资源都满足条件need<work,则找到了一个进程满足
      for (int m = 0; m< source; m++) {
       work[m] = work[m] + allocation[j][m];
      }
      finish[j] = true;//找到一个进程满足
      p[count++] = j;//记录找到的是第几个进程
     }
    }
    num = 0;//必须把它清零,重新来找下个资源种类的每种是否都满足条件
   }
  }
  
  //记录有多少个序列;
  for (int i = 0; i < p.length; i++) {
   if (finish[i] == true) {
    count1++;
   }
  }
  if (count1 == process) {//如果序列里面总数等于总共有多少程序,就找到了安全的序列。并且输出。反之没有找到
   
   str+="/n/n";
    str+="存在一个安全序列,安全序列为:";
   for (int i = 0; i < p.length; i++) {
    str+="P" + p[i] + " ";
   }
   str+="/n/n";
  } else {
   str+="系统处于不安全状态!";
  }
  
  JOptionPane.showMessageDialog(null, str);
 }
 
 //判断是否请求资源还是直接退出
 public void identify(){
  
  boolean flag=true;
  while(flag){
  String sle=JOptionPane.showInputDialog("------------请求操作--------------/n"+
    "是否还要继续,yes:继续做请求资源情况   No:退出");
  
  switch(sle.toLowerCase().charAt(0)){
  case 'y':
   request();
   break;
  case 'n':
   JOptionPane.showMessageDialog(null,"谢谢使用!");
   flag=false;
   break;
   default:
    JOptionPane.showMessageDialog(null,"输入错误,请重新输入");
   }
  }
  
 }
 
 public void request(){
  int count2=0;
  String sle=JOptionPane.showInputDialog("请输入要申请的第几个进程,进程是从0开始的");
  int process1=Integer.parseInt(sle);
  
  
  JOptionPane.showMessageDialog(null,"输入要请求的资源的");
  int[] request=new int[source];
  for(int i=0;i<source;i++){
   String sle1=JOptionPane.showInputDialog("请输入第"+process1+"个进程第"+i+"种请求资源");
   request[i]=Integer.parseInt(sle1);
   str+="第"+i+"个请求资源为:";
   str+=Integer.parseInt(sle1)+","+"/n";
  }
  
  
  //判断是否可以分配
  for(int i=0;i<source;i++){
   if(request[i]<=need[process1][i]&&request[i]<=available[i]){
    count2++;
   }
  }
  
  if(count2==source){
   for(int j=0;j<source;j++){
    allocation[process1][j]+=request[j];
    need[process1][j]-=request[j];
    available[j]-=request[j];
   }
   
   str+="~~~~~~~~~~~试分配如下~~~~~~~~~~~~~~/n";
   print();
   isSafe();
  }else{
   JOptionPane.showMessageDialog(null,"不能进行试分配,也就找不到安全序列");
  }
 }
 
 
 public static void main(String[] args) {
  boolean flag=true;
  while(flag){
  String sle=JOptionPane.showInputDialog("~~~~~~~~~~银行家算法~~~~~~~~~~~"+"/n"+
       "1-进入程序      2-退出");
  int data=Integer.parseInt(sle);
  if(data==1){
   Bank bank=new Bank();
   bank.isSafe();
   bank.identify();
  }else if(data==2){
   flag=false;
  }else{
   JOptionPane.showMessageDialog(null, "输入有误,请重新输入");
  }
  }
 }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值