>>分享数据结构和算法相关的知识和技术 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 19583 个阅读者 刷新本主题
 * 贴子主题:  银行家算法范例 回复文章 点赞(0)  收藏  
作者:javathinker    发表时间:2020-03-15 13:23:30     消息  查看  搜索  好友  复制  引用

import java.io.*;
public class bank
{
  int MAX_PROCESS = 5; //最大进程数
  int MAX_COURCE = 3; //最大资源类别

   int[] Available=new int[MAX_COURCE]; //可利用资源向量
  int[][] Max= new int[MAX_PROCESS][MAX_COURCE]; //最大需求矩阵
  int[][] Allocation=new int[MAX_PROCESS][MAX_COURCE]; //分配矩阵
  int[][] Need=new int[MAX_PROCESS][MAX_COURCE]; //需求矩阵

   int Request_PROCESS; //发出请求的进程
  int[][] Request_COURCE = new int[MAX_PROCESS][MAX_COURCE]; //线程请求资源的数量

   public bank()
  {
  }

   public void InitState()
  {
    System.out.println("初始化当前状态......");
    Max[0][0] = 7;Max[0][1] = 5;Max[0][2] = 3;
    Allocation[0][0] = 0;Allocation[0][1] = 1;Allocation[0][2] = 0;
    Need[0][0] = 7;Need[0][1] = 4;Need[0][2] = 3;

     //初始化进程二
    Max[1][0] = 3;Max[1][1] = 2;Max[1][2] = 2;
    Allocation[1][0] = 2;Allocation[1][1] = 0;Allocation[1][2] = 0;
    Need[1][0] = 1;Need[1][1] = 2;Need[1][2] = 2;

     //初始化进程三
    Max[2][0] = 9;Max[2][1] = 0;Max[2][2] = 2;
    Allocation[2][0] = 3;Allocation[2][1] = 0;Allocation[2][2] = 2;
    Need[2][0] = 6;Need[2][1] = 0;Need[2][2] = 0;

     //初始化进程四
    Max[3][0] = 2;Max[3][1] = 2;Max[3][2] = 2;
    Allocation[3][0] = 2;Allocation[3][1] = 1;Allocation[3][2] = 1;
    Need[3][0] = 0;Need[3][1] = 1;Need[3][2] = 1;

     //初始化进程五
    Max[4][0] = 4;Max[4][1] = 3;Max[4][2] = 3;
    Allocation[4][0] = 0;Allocation[4][1] = 0;Allocation[4][2] = 2;
    Need[4][0] = 4;Need[4][1] = 3;Need[4][2] = 1;

     //初始化可用资源
    Available[0] = 3;Available[1] = 3;Available[2] = 2;

   }

   public void bankmethod()
  {
    try
    {
      System.out.println("请输入请求资源的进程(0-4):");
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      Request_PROCESS = Integer.parseInt(reader.readLine());

       System.out.println("请输入请求资源的数量:");

       for(int i=0;i<MAX_COURCE;i++)
      {
        Request_COURCE[Request_PROCESS][i] = Integer.parseInt(reader.readLine());
       }
    }catch(IOException e){}

     for(int i=0;i<MAX_COURCE;i++)
    {
      //判断Request<=Need
      if( Request_COURCE[Request_PROCESS][i] > Need[Request_PROCESS][i])
      {
        System.out.println("请求资源大于需求,输入错误。:");
        return;
       }

       //判断Request<=Available
      if(Request_COURCE[Request_PROCESS][i] > Available[i])
      {
        System.out.println("请求资源大于可用资源,此进程进入等待状态。");
        System.out.println("请继续下一进程........");
        return;
      }
    }

     //修改Available, Allocation, Need
    for(int i=0;i<MAX_COURCE;i++)
    {
      Available[i] = Available[i] - Request_COURCE[Request_PROCESS][i];
      Allocation[Request_PROCESS][i] = Allocation[Request_PROCESS][i] + Request_COURCE[Request_PROCESS][i];
      Need[Request_PROCESS][i] = Need[Request_PROCESS][i] - Request_COURCE[Request_PROCESS][i];
    }

     if(IsSafe()) //判断当前状态是否是安全状态
    {
      System.out.println("系统仍处于安全状态,请求资源成功。");
      System.out.println("请继续........");
    }
    else
    {
      //非安全状态
      System.out.println("系统进入不安全状态,请求资源失败。");
      for(int i=0;i<MAX_COURCE;i++)
      {
        Available[i] = Available[i] + Request_COURCE[Request_PROCESS][i];
        Allocation[Request_PROCESS][i] = Allocation[Request_PROCESS][i] - Request_COURCE[Request_PROCESS][i];
        Need[Request_PROCESS][i] = Need[Request_PROCESS][i] + Request_COURCE[Request_PROCESS][i];
      }
      System.out.println("请继续........");
    }
  }

   public boolean IsSafe()
  {
    int[] work = new int[3];
    boolean[] Finish = {false,false,false,false,false};
    for(int i=0;i<MAX_COURCE;i++)
    {
      work[i] = Available[i];
    }
    //寻找满足Finish[i]=false,Need[i,j]<=Work[j]的进程
    int i = 0;
    do
    {
      boolean flag = true;

       //是否Need[i][j]<=work[j]
      for(int j=0;j<MAX_COURCE;j++)
      {
        if(Need[i][j]>work[j])
        {
          flag = false;
          break;
        }
       }

       //是否Finish[i]=false且Need[i,j]<=Work[j]
      if(Finish[i]==false && flag)
      {
        for(int j=0;j<MAX_COURCE;j++)
        {
          work[j] = work[j] + Allocation[i][j];
        }
        Finish[i] = true;

         i = -1; //重新遍历未结束的进程
      }

     }while(++i<MAX_PROCESS);

     i = 0;
    while(Finish[i]==true)
    {
      if(i == 4) return true; //是安全状态,返回true
      i++;
    }

     return false; //非安全状态,返回false

   }

   /**
  * @param args the command line arguments
  */

  public static void main(String[] args)
  {
    bank mybank = new bank();
    mybank.InitState();
    while(true)
    {
      mybank.bankmethod();
    }
  }
}

                                  ----------------------------
原文链接:https://blog.51cto.com/maple/55189

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-03-20 11:28:18 重新编辑]
网站系统异常


系统异常信息
Request URL: http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=2876

java.lang.NullPointerException

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员