Java编写银行家算法模拟程序

Java编写银行家算法模拟程序,第1张

目录

前言

一、思路

二、代码

总结


前言

编写一个银行家算法的模拟程序。改程序应该能够循环检查每一个提出请求的银行客户,并且能判断这一请求是否安全。请把有关请求和相应决定的列表输出到一个文件中。


一、思路

按照流程图实现即可:

 

 

二、代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Banker {
    static int n, m, k; // n 进程个数 m 资源个数
    static Scanner sc = new Scanner(System.in);
    static int available[], max[][], need[][], allocation[][], request[];
    static ArrayList> requestList = new ArrayList<>();
    static ArrayList decisionList = new ArrayList<>();

    public static void main(String[] args) {
        Bank();
    }

    public static void Bank() {
        init();

        while(true) {
            ArrayList req = new ArrayList<>();
            System.out.println("Input process number and its request vector: ");
            k = sc.nextInt();
            req.add(k);//线程号存放在 0 index
            for(int i = 1;i <= m;i++) {
                request[i] = sc.nextInt();
                req.add(request[i]);
            }

            requestList.add(req);

            if(!vectorLess(request, need[k])) {
                System.out.println("请求资源大于所需资源 错误!");
                return;
            }

            if(!vectorLess(request, available)) {
                System.out.println("请求资源大于所有资源 错误!");
                return;
            }

            //更新
            for(int i = 1;i <= m;i++) {
                available[i] -= request[i];
                allocation[k][i] += request[i];
                need[k][i] -= request[i];
            }

            if(!safe()) {
                System.out.println("request不安全");
                //回复更新
                for(int i = 1;i <= m;i++) {
                    available[i] += request[i];
                    allocation[k][i] -= request[i];
                    need[k][i] += request[i];
                }
                decisionList.add(false);
            }else {
                System.out.println("request安全 分配成功");
                decisionList.add(true);
            }

            System.out.println("是否进行下一次request?(Y/N): ");
            char choose = sc.next().charAt(0);
            if(choose == 'N') {
                break;
            }
        }

        for(int i = 0;i < requestList.size();i++) {
            ArrayList req = requestList.get(i);
            System.out.printf("第%d次请求, 线程%d: request: ", i + 1, req.get(0)); //index 0 是线程号
            for (int j = 1;j < req.size();j++) {
                System.out.print(req.get(j) + " ");
            }
            if(decisionList.get(i)){
                System.out.println("决定: 分配成功");
            }else {
                System.out.println("决定: 分配失败");
            }
        }
    }

    public static boolean safe() {
        int work[] = Arrays.copyOf(available, m + 1);
        boolean finish[] = new boolean[n + 1];

        for(int i = 1;i <= n;i++) {
            if(!finish[i] && vectorLess(need[k], work)) {
                finish[i] = true;
                for(int j = 1;j <= m;j++) {
                    work[j] += allocation[k][j];
                }
                continue;
            }
        }

        for(int i = 1;i <= n;i++) {
            if(!finish[i]) return false;
        }

        return true;
    }

    //进程 k request
    private static boolean vectorLess(int vector[], int vector1[]) {
        for(int i = 1;i <= m;i++) {
            if(vector[i] > vector1[i]) return false;
        }

        return true;
    }

    public static void init() {
        System.out.println("输入线程数量, 资源数量: ");
        n = sc.nextInt();
        m = sc.nextInt();
        available = new int[m + 1];
        max = new int[n + 1][m + 1];
        need = new int[n + 1][m + 1];
        allocation = new int[n + 1][m + 1];
        request = new int[m + 1];

        System.out.println("Input max Matrix: ");
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                max[i][j] = sc.nextInt();
            }
        }

        System.out.println("Input Allocation Matrix: ");
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                allocation[i][j] = sc.nextInt();
            }
        }

        //算出need矩阵
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                need[i][j] = max[i][j] - allocation[i][j];
            }
        }

        System.out.println("Input Available Vector: ");
        for(int i = 1;i <= m;i++) {
            available[i] = sc.nextInt();
        }
    }
}


总结

1. 没有做输出到文件夹中的功能,因为没有必要

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/739035.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存