安卓APP下载
|
风云墙二十周年
|
邮箱找密
|
无图版
|
风格切换
|
帮助
|
Home首页
|
论坛首页
»
您尚未
登录
注册
|
推荐
|
搜索
|
社区服务
|
银行
|
论坛群
|
转贴助手
社区服务
风格切换
newclwind
wind
20th
风云小站|Connecting Lives With Infinite New Discoveries
»
求助专区
»
如何用java实现银行家算法?
新 帖
本页主题:
如何用java实现银行家算法?
打印
|
加为IE收藏
|
复制链接
|
收藏主题
|
上一主题
|
下一主题
nic496
级别:
初级会员
精华:
0
发帖:
44
威望:
167 点
风云币:
5967 元
专家分:
0 分
在线时间:10(小时)
注册时间:2006-11-01
最后登录:2007-03-24
小
中
大
引用
推荐
编辑
只看
复制
如何用java实现银行家算法?
请问如何才能用java实现银行家算法,请大家帮帮我啊!
每个人都有自己的舞台
顶端
Posted: 2006-11-08 14:36 |
[楼 主]
sniper54
级别:
管理员
精华:
6
发帖:
7036
威望:
385 点
风云币:
142954 元
专家分:
101 分
论坛群:
管理团队
在线时间:2219(小时)
注册时间:2006-05-27
最后登录:2026-06-04
小
中
大
引用
推荐
编辑
只看
复制
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) {}
}
}
}
CLWIN
D
.COM
ALL THE BEST FOR YOU!
顶端
Posted: 2006-11-08 16:37 |
1 楼
sniper54
级别:
管理员
精华:
6
发帖:
7036
威望:
385 点
风云币:
142954 元
专家分:
101 分
论坛群:
管理团队
在线时间:2219(小时)
注册时间:2006-05-27
最后登录:2026-06-04
小
中
大
引用
推荐
编辑
只看
复制
有点小错。。你改改。。。
CLWIN
D
.COM
ALL THE BEST FOR YOU!
顶端
Posted: 2006-11-08 16:39 |
2 楼
nic496
级别:
初级会员
精华:
0
发帖:
44
威望:
167 点
风云币:
5967 元
专家分:
0 分
在线时间:10(小时)
注册时间:2006-11-01
最后登录:2007-03-24
小
中
大
引用
推荐
编辑
只看
复制
真是太谢谢你了
每个人都有自己的舞台
顶端
Posted: 2006-11-08 22:12 |
3 楼
帖子浏览记录
版块浏览记录
快速跳至
|- 灌水乐园
|- 心情驿站
|- 市场交流区
|- 高级交易区
|- 市场劳务区
>> AD大区
|- 非常AD区
|- 论坛收集区
|- 资源补档区
|- AD原创区
>> 技术大区
|- 应用交流
|- 求助专区
>> 论坛服务区
|- 公告建议区
|- 活动宣传区
|- 会员服务区
>> 历史存档区
风云小站|Connecting Lives With Infinite New Discoveries
»
求助专区
Total 0.035238(s) query 6, Time now is:06-05 07:44, Gzip enabled
渝ICP备20004412号-1
Powered by
PHPWind
v6.3.2
Certificate
Code © 2003-08
PHPWind.com
Corporation