栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

前言

一、思路

二、代码

总结


前言

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


一、思路

按照流程图实现即可:

 

 

二、代码
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. 没有做输出到文件夹中的功能,因为没有必要

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/839594.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号