银行家算法JAVA实现--实验3

1.创建一个进程类Process

                                  ---------制作不易,看到即是鼓励
                                  加油!!!!
/**
 * @author
 * @create 86153-2021
 */
public class Process {
    int A;
    int B;
    int C;

    String name;

    private int sign=0;



    public Process(int a, int b, int c, String name) {
        A = a;
        B = b;
        C = c;
        this.name = name;

    }

    public int getSign() {
        return sign;
    }

    public void setSign(int sign) {
        this.sign = sign;
    }

    @Override
    public String toString() {
        return "Process{" +
                "A=" + A +
                ", B=" + B +
                ", C=" + C +
                ", name='" + name + '\'' +
                ", sign=" + sign +
                '}';
    }
}

2.主方法

资源的数量是可以添加的,这个类可以改变

/**
 * @author
 * @create 86153-2021
 */
public class Bank {
    /**
     * a表示计算机资源,比较资源是否可以满足进程
     * @param a
     * @param b
     * @return
     */
    public boolean compareProcess(Process a, Process b){
        boolean fage=false;
        if(a.A>=b.A){
            if(a.B>=b.B){
                if(a.C>=b.C){
                    fage=true;
                }
            }
        }
        return fage;
    }

    /**
     * 返回一个进程(是由后两个进程属性相加得到的)
     * @param a
     * @param b
     * @param c
     * @return
     */
    public Process addProcess(Process a,Process b,Process c){
        a.A=b.A+c.A;
        a.B=b.B+c.B;
        a.C=b.C+c.C;
        return a;
    }



    /**
     * 主方法
     * @param args
     */
    public static void main(String[] args) {
        //已经分配
        Process p0=new Process(0,1,0,"p0");
        Process p1=new Process(2,0,0,"p1");
        Process p2=new Process(3,0,2,"p2");
        Process p3=new Process(2,1,1,"p3");
        Process p4=new Process(0,0,2,"P4");

        //Need
        Process pN0=new Process(7,4,3,"pN0");
        Process pN1=new Process(1,2,2,"pN1");
        Process pN2=new Process(6,0,0,"pN2");
        Process pN3=new Process(0,1,1,"pN3");
        Process pN4=new Process(4,3,1,"pN4");

        //Available
        Process pc=new Process(3,3,2,"pc");


        Process [][] list={{p0,pN0,},{p1,pN1},{p2,pN2},{p3,pN3},{p4,pN4}};

        Bank bank=new Bank();


        for (int i=0;i<list.length;++i){

            if(list[i][0].getSign()!=1){

                if(bank.compareProcess(pc,list[i][1])){//表示计算机可以提供资源

                    bank.addProcess(pc,pc,list[i][0]);

                    list[i][0].setSign(1);//下次碰到1,就直接跳过

                    System.out.println(list[i][0].name);
                    System.out.println(list[i][0].toString());
                    System.out.println(list[i][1].toString());
                    System.out.println(pc.toString());

                    i=-1;
                }
            }

        }




    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值