44
55
66public class Banker {
7- int available [] = new int []{3 ,3 ,2 };//可得到的资源
8- int max [][] = new int [][]{{7 ,5 ,3 },{3 ,2 ,2 },{9 ,0 ,2 },{2 ,2 ,2 },{4 ,3 ,3 }};//每个进程最大资源数
9- int allocation [][] = new int [][]{{0 ,1 ,0 },{2 ,0 ,0 },{3 ,0 ,2 },{2 ,1 ,1 },{0 ,0 ,2 }};//每个进程目前拥有的资源数
10- int need [][] = new int [][]{{7 ,4 ,3 },{1 ,2 ,2 },{6 ,0 ,0 },{0 ,1 ,1 },{4 ,3 ,1 }};//每个进程需要的资源数
7+ int available [] = new int []{3 ,3 ,2 };//可得到的资源
8+ int max [][] = new int [][]{{7 ,5 ,3 },{3 ,2 ,2 },{9 ,0 ,2 },{2 ,2 ,2 },{4 ,3 ,3 }};//每个进程最大资源数
9+ int allocation [][] = new int [][]{{0 ,1 ,0 },{2 ,0 ,0 },{3 ,0 ,2 },{2 ,1 ,1 },{0 ,0 ,2 }};//每个进程目前拥有的资源数
10+ int need [][] = new int [][]{{7 ,4 ,3 },{1 ,2 ,2 },{6 ,0 ,0 },{0 ,1 ,1 },{4 ,3 ,1 }};//每个进程需要的资源数
1111
12- void showData ()//展示数据输出每个进程的相关数据
12+ void showData ()//展示数据输出每个进程的相关数据
1313 {
14- System .out .println ("进程号 Max Allocation Need " );
14+ System .out .println ("进程号 Max Allocation Need " );
1515 System .out .println (" A B C A B C A B C" );
1616 for (int i = 0 ;i <5 ;i ++)
1717 {
@@ -23,19 +23,19 @@ public class Banker{
2323 }
2424 }
2525
26- boolean change (int inRequestNum ,int inRequest []){//分配数据
26+ boolean change (int inRequestNum ,int inRequest []){//分配数据
2727 int requestNum = inRequestNum ;
2828 int request [] = inRequest ;
29- // for(int i=0;i<3;i++)System.out.println("修改前available"+available[i]);
29+ // for(int i=0;i<3;i++)System.out.println("修改前available"+available[i]);
3030 if (!(request [0 ]<=need [requestNum ][0 ]&&request [1 ]<=need [requestNum ][1 ]&&request [2 ]<=need [requestNum ][2 ])){
31- System .out .println ("请求的资源数超过了所需要的最大值,分配错误" );
31+ System .out .println ("请求的资源数超过了所需要的最大值,分配错误" );
3232 return false ;
3333 }
3434 if ((request [0 ]<=available [0 ]&&request [1 ]<=available [1 ]&&request [2 ]<=available [2 ])==false ){
35- System .out .println ("尚无足够资源分配,必须等待" );
35+ System .out .println ("尚无足够资源分配,必须等待" );
3636 return false ;
3737 }
38- for (int i = 0 ;i <3 ;i ++){//试分配数据给请求的线程
38+ for (int i = 0 ;i <3 ;i ++){//试分配数据给请求的线程
3939
4040 available [i ] = available [i ]-request [i ];
4141
@@ -44,13 +44,13 @@ public class Banker{
4444 need [requestNum ][i ] = need [requestNum ][i ] - request [i ];
4545
4646 }
47- boolean flag = checkSafe (available [0 ],available [1 ],available [2 ]);//进行安全性检查并返回是否安全
47+ boolean flag = checkSafe (available [0 ],available [1 ],available [2 ]);//进行安全性检查并返回是否安全
4848
4949 if (flag ==true ){
50- System .out .println ("能够安全分配" );
50+ System .out .println ("能够安全分配" );
5151 return true ;
5252 }else {
53- System .out .println ("不能够安全分配" );
53+ System .out .println ("不能够安全分配" );
5454 for (int i = 0 ;i <3 ;i ++){
5555 available [i ] = available [i ]+request [i ];
5656 allocation [requestNum ][i ] = allocation [requestNum ][i ] - request [i ];
@@ -68,15 +68,15 @@ boolean checkSafe(int a,int b,int c){
6868 work [2 ] = c ;
6969 int i =0 ;
7070 boolean finish [] = new boolean [5 ];
71- while (i <5 )//寻找一个能够满足的认为完成后才去执行下一进程
71+ while (i <5 )//寻找一个能够满足的认为完成后才去执行下一进程
7272 {
73- if (finish [i ]==false &&need [i ][0 ]<=work [0 ]&&need [i ][1 ]<=work [1 ]&&need [i ][2 ]<=work [2 ]){//找到满足的修改work值,然后i=0,重新从开始的为分配的中寻找
74- System .out .println ("分配成功的是" +i );
73+ if (finish [i ]==false &&need [i ][0 ]<=work [0 ]&&need [i ][1 ]<=work [1 ]&&need [i ][2 ]<=work [2 ]){//找到满足的修改work值,然后i=0,重新从开始的为分配的中寻找
74+ System .out .println ("分配成功的是" +i );
7575 for (int m = 0 ;m <3 ;m ++)
7676 work [m ] =work [m ] + allocation [i ][m ];
7777 finish [i ] = true ;
7878 i =0 ;
79- }else //如果没有找到直接i++
79+ }else //如果没有找到直接i++
8080 i ++;
8181 }
8282
@@ -110,29 +110,29 @@ public static void main(String[] args)
110110
111111 String choice = new String ();
112112
113- while (true )//循环进行分配
113+ while (true )//循环进行分配
114114
115115 {
116116
117- System .out .println ("请输入要请求的进程号(0--4):" );
117+ System .out .println ("请输入要请求的进程号(0--4):" );
118118
119119 requestNum = s .nextInt ();
120120
121- System .out .print ("请输入请求的资源数目" );
121+ System .out .print ("请输入请求的资源数目" );
122122
123123 for (int i = 0 ;i <3 ;i ++)
124124
125125 {
126126
127- System .out .println (source [i ]+"资源的数目:" );
127+ System .out .println (source [i ]+"资源的数目:" );
128128
129129 request [i ] = s .nextInt ();
130130
131131 }
132132
133133 bank .change (requestNum , request );
134134
135- System .out .println ("是否再请求分配(y/n)" );
135+ System .out .println ("是否再请求分配(y/n)" );
136136
137137 choice = s .next ();
138138
0 commit comments