Java语言实现银行家算法

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

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执行完毕,归还资源
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值