风云小站 » 『 求助专区 』 » 如何用java实现银行家算法?
本页主题: 如何用java实现银行家算法? 打印 | 加为IE收藏 | 复制链接 | 收藏主题 | 上一主题 | 下一主题

nic496
级别: 初级会员


精华: 0
发帖: 44
威望: 167 点
风云币: 5467 元
专家分: 0 分
在线时间:10(小时)
注册时间:2006-11-01
最后登录:2007-03-24

 如何用java实现银行家算法?

请问如何才能用java实现银行家算法,请大家帮帮我啊!
每个人都有自己的舞台
顶端 Posted: 2006-11-08 14:36 | [楼 主]
sniper54
级别: 管理员


精华: 6
发帖: 7030
威望: 385 点
风云币: 142430 元
专家分: 101 分
论坛群: 管理团队
在线时间:2219(小时)
注册时间:2006-05-27
最后登录:2024-05-28

 

java   Bank   10   3   5   6   2   来执行  
public   class   Bank{  
public   static   void   main(String[]   args){  
//   first   decide   on   the   capital   and   the   line   credits,  
//   then  
//   read   from   the   user   client   id   and   what   he   wishes  
//   if   it   leads   to   a   safe   state   grant   it,   otherwise   not.  
Banker   b;  
int   clients,   capital;  
try   {  
//   requires   start   capital   and   number   of   clients.  
capital   =   Integer.parseInt(args[0]);  
clients   =   Integer.parseInt(args[1]);  
b=   new   Banker(capital,clients);  
//   start   the   credit   lines  
for(int   i   =0;   i<clients;i++){  
 
b.startCreditLine(Integer.parseInt(args[i+2]),i);  
b.show();  
}  
}catch   (NumberFormatException   e)   {}  
}  
}  
 
======================================  
import   java.awt.*;  
import   java.awt.event.*;  
 
public   class   Banker   extends   Frame{  
 
int   capital,   available,   clients,   currentClient;  
int[]   max,   granted,   need;  
 
public   Banker(int   startCapital,   int   numClients){  
capital   =   startCapital;  
available   =   capital;  
clients   =   numClients;  
currentClient=-1;  
max   =   new   int[clients];  
granted   =   new   int[clients];  
need   =   new   int[clients];  
for(int   i   =   0;   i<clients;i++){  
max=0;  
granted=0;  
need=0;  
}  
setUp();  
}  
 
public   void   startCreditLine(int   x,   int   client){  
max[client]=x;  
need[client]=x;  
}  
 
private   boolean   safe(){  
boolean[]   finished   =   new   boolean[clients];  
int   workingCapital   =   available;  
int   candidate   =   0;  
 
//   if   what   he   asked   for   is   enough   for   him   to   be   satisfied,  
//   we   can   just   wait   for   him   to   finish   and   return   what   he   has  
//   so   it   is   safe.  
 
if(need[currentClient]==0)   return   true;  
 
if(need[currentClient]<0){  
System.out.println("The   banker   cannot   give   you   more   than   what   remains   for   your   max");  
return   false;  
}  
 
if   (workingCapital<0){  
System.out.println("The   banker   cannot   give   you   more   than   what   he   has   available");  
return   false;  
}  
 
for(int   i   =   0;   i<clients;   i++)  
finished=false;  
 
candidate   =   nextCandidate(finished,workingCapital);  
while   (candidate<clients){  
//   a   candidate   we   can   treat!  
//   he   can   finish   and   we   can   get   back   what   he   had.  
finished[candidate]=true;  
workingCapital   =   workingCapital   +   need[candidate];  
candidate=nextCandidate(finished,workingCapital);  
}  
return   (allSatisfied(finished));  
}  
 
private   int   nextCandidate(boolean[]   f,   int   wc){  
for(int   i   =   0;   i<f.length;   i++){  
if   (f==false   &&   need<=wc){  
return   i;  
}  
}  
return   f.length;  
}  
 
private   boolean   allSatisfied(boolean[]   f){  
for(int   i=0;   i<f.length;   i++)  
if   (!f)   return   false;  
return   true;  
}  
 
 
private   void   grant(int   x,   int   client){  
granted[client]=granted[client]+x;  
available   =   available   -x;  
need[client]=need[client]-x;  
}  
 
private   void   setUp(){  
TextField   client,   credit;  
Label   cl,cr;  
Panel   p,q;  
client=new   TextField(10);  
credit=   new   TextField(10);  
cl   =   new   Label("Client   :   ");  
cr   =   new   Label("Requires:   ");  
p=new   Panel();  
q=new   Panel();  
p.add(cl);  
p.add(client);  
q.add(cr);  
q.add(credit);  
add("North",p);  
add("South",q);  
pack();  
client.addActionListener(new   ClientListener());  
credit.addActionListener(new   CreditListener());  
}  
 
private   class   ClientListener   implements   ActionListener{  
public   void   actionPerformed(ActionEvent   e){  
try{  
currentClient   =   Integer.parseInt(e.getActionCommand());  
System.out.println("client   :   "+currentClient);  
System.out.println("max   :   "+max[currentClient]);  
System.out.println("granted   :   "+granted[currentClient]);  
System.out.println("need   :   "+need[currentClient]);  
System.out.println("available   :   "+available);  
}catch   (NumberFormatException   ne)   {}  
}  
}  
 
private   class   CreditListener   implements   ActionListener{  
public   void   actionPerformed(ActionEvent   e){  
int   ammount;  
try{  
ammount   =   Integer.parseInt(e.getActionCommand());  
grant(ammount,currentClient);  
if   (!safe()){  
grant(-ammount,currentClient);  
System.out.println("not   granted   "+   ammount);  
System.out.println("available   :   "+available);  
}  
else{  
System.out.println("granted   "   +   ammount   +   "   to   client   "   +   currentClient);  
System.out.println("available   :   "+available);  
}  
 
}catch   (NumberFormatException   ne)   {}  
}  
}  
}
CLWIND.COM
ALL THE BEST FOR YOU!
顶端 Posted: 2006-11-08 16:37 | 1 楼
sniper54
级别: 管理员


精华: 6
发帖: 7030
威望: 385 点
风云币: 142430 元
专家分: 101 分
论坛群: 管理团队
在线时间:2219(小时)
注册时间:2006-05-27
最后登录:2024-05-28

 

有点小错。。你改改。。。
CLWIND.COM
ALL THE BEST FOR YOU!
顶端 Posted: 2006-11-08 16:39 | 2 楼
nic496
级别: 初级会员


精华: 0
发帖: 44
威望: 167 点
风云币: 5467 元
专家分: 0 分
在线时间:10(小时)
注册时间:2006-11-01
最后登录:2007-03-24

 

真是太谢谢你了
每个人都有自己的舞台
顶端 Posted: 2006-11-08 22:12 | 3 楼
帖子浏览记录 版块浏览记录
风云小站 » 『 求助专区 』
感谢,曾经的版主
Total 0.012510(s) query 6, Time now is:12-28 13:34, Gzip enabled 渝ICP备20004412号-1

Powered by PHPWind v6.3.2 Certificate Code © 2003-07 PHPWind.com Corporation
Skin by Chen Bo