您的位置:首页 > 编程语言 > Java开发

Java语言实现银行家算法

2020-01-11 12:52 423 查看

在王道操作系统复习上学习了银行家算法
自己简单地实现了一下
资源类:

public class Resources {
//可用各资源数量
private int A;
private int B;
private int C;

public Resources(int a, int b, int c) {
A = a;
B = b;
C = c;
}

public int getA() {
return A;
}
public void setA(int a) {
A = a;
}
public int getB() {
return B;
}
public void setB(int b) {
B = b;
}
public int getC() {
return C;
}
public void setC(int c) {
C = c;
}

public synchronized void update(int a,int b,int c){
setA(this.A+a);
setB(this.B+b);
setC(this.C+c);
//System.out.println("当前可用资源数:A:"+A+",B:"+B+",C:"+C);
}
}

线程1:

public class P0 extends Thread{
private Resources resources;
//需要各类资源的最大值
private int maxA;
private int maxB;
private int maxC;
//已分配各类资源个数
private int alloA;
private int alloB;
private int alloC;
//需要各资源的个数
private int needA;
private int needB;
private int needC;
//构造方法
public P0(int maxA, int maxB, int maxC, int alloA, int alloB, int alloC,Resources resources) {
this.maxA = maxA;
this.maxB = maxB;
this.maxC = maxC;
this.alloA = alloA;
this.alloB = alloB;
this.alloC = alloC;
needA=maxA-alloA;
needB=maxB-alloB;
needC=maxC-alloC;
this.resources=resources;
}

@Override
public void run() {
System.out.println("P0需要资源个数分别为:A:"+needA+",B:"+needB+",C:"+needC);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true){
if(resources.getA()>=needA&&resources.getB()>=needB&&resources.getC()>=needC){
System.out.println("P0执行完毕,归还资源");
resources.update(alloA,alloB,alloC);
break;
}
}
}
}

线程2:

public class P1 extends Thread{
private Resources resources;
//需要各类资源的最大值
private int maxA;
private int maxB;
private int maxC;
//已分配各类资源个数
private int alloA;
private int alloB;
private int alloC;
//需要各资源的个数
private int needA;
private int needB;
private int needC;
//构造方法
public P1(int maxA, int maxB, int maxC, int alloA, int alloB, int alloC, Resources resources) {
this.maxA = maxA;
this.maxB = maxB;
this.maxC = maxC;
this.alloA = alloA;
this.alloB = alloB;
this.alloC = alloC;
needA=maxA-alloA;
needB=maxB-alloB;
needC=maxC-alloC;
this.resources=resources;
}

@Override
public void run() {
System.out.println("P1需要资源个数分别为:A:"+needA+",B:"+needB+",C:"+needC);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true){
if(resources.getA()>=needA&&resources.getB()>=needB&&resources.getC()>=needC){
System.out.println("P1执行完毕,归还资源");
resources.update(alloA,alloB,alloC);
break;
}
}
}
}

其余都大同小异
测试类:

public class Test {
public static void main(String[] args) {
Resources resources=new Resources(3,3,2);
P0 p0=new P0(7,5,3,0,1,0,resources);
P1 p1=new P1(3,2,2,2,0,0,resources);
P2 p2=new P2(9,0,2,3,0,2,resources);
P3 p3=new P3(2,2,2,2,1,1,resources);
P4 p4=new P4(4,3,3,0,0,2,resources);
p0.start();
p1.start();
p2.start();
p3.start();
p4.start();
}
}

运行结果:

P0需要资源个数分别为:A:7,B:4,C:3
P3需要资源个数分别为:A:0,B:1,C:1
P4需要资源个数分别为:A:4,B:3,C:1
P2需要资源个数分别为:A:6,B:0,C:0
P1需要资源个数分别为:A:1,B:2,C:2
P1执行完毕,归还资源
P3执行完毕,归还资源
P2执行完毕,归还资源
P0执行完毕,归还资源
P4执行完毕,归还资源
  • 点赞
  • 收藏
  • 分享
  • 文章举报
西安陈萍萍 发布了41 篇原创文章 · 获赞 118 · 访问量 1万+ 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: