Java银行家算法

实验存档,代码特别烂。。

测试.java

package operating.test;

import operating.entity.bank.Bank;
import operating.entity.bank.EngineeringTeam;

public class TestBank {
    public static void main(String[] args) {
        Bank bank = new Bank(10);
        for (int i = 0; i != 3; ++i) {
            new Thread(new EngineeringTeam(5, bank)).start();
        }
    }
}

/

银行.java

package operating.entity.bank;

import java.util.ArrayList;

public class Bank {
    /**
     * 工程队数量
     */
    private static final int NUM = 3;
    /**
     * 当前可用的资金数目
     */
    private int available;
    /**
     * 已分配矩阵
     */
    private int[] allocation = new int[NUM];
    /**
     * 需求矩阵
     */
    private int[] need = new int[NUM];

    public Bank(int available) {
        this.available = available;
    }

    /**
     * 向银行借钱
     * @param id 工程队id
     * @param request 工程队需求资金
     * @return 一个布尔值,如果借钱成功返回真,否则返回假
     */
    public synchronized boolean borrow(int id, int request) {
        System.out.println("银行还剩 " + available + " 万元。");
        if (request <= need[id]) {
            if (request <= available) {
                int temp1 = available;
                int temp2 = allocation[id];
                int temp3 = need[id];
                // 试探借钱
                available = available - request;
                allocation[id] = allocation[id] + request;
                need[id] = need[id] - request;
                // 安全检查
                if (check()) {
                    System.out.println("工程队" + id + ": 借 " + request + " 万元成功。");
                    return true;
                } else {
                    // 不能借,撤销操作
                    available = temp1;
                    allocation[id] = temp2;
                    need[id] = temp3;
                }
            }
        }
        System.out.println("工程队" + id + ": 借 " + request + " 万元失败。");
        return false;
    }

    /**
     * 安全检查
     * @return 当前状态为安全状态返回真,否则返回假。
     */
    private boolean check() {
        int work = available;
        boolean[] finish = new boolean[NUM];
        for (int i = 0; i != finish.length; ++i) {
            finish[i] = false;
        }

        for (int i = 0; i != NUM; ++i) {
            for (int j = 0; j != NUM; ++j) {
                if (!finish[j] && need[j] <= work) {
                    work = work + allocation[j];
                    finish[j] = true;
                }
            }
        }

        boolean flag = true;
        for (int i = 0; i != NUM; ++i) {
            flag = flag && finish[i];
        }
        return flag;
    }

    /**
     * 添加工程队
     * @param team
     */
    public void addTeam(EngineeringTeam team) {
        int id = team.getId();
        need[id] = team.getNeed();
    }

    /**
     * 工程队还钱
     * @param money
     */
    public void payBack(int money) {
        available = available + money;
    }
}

/

借钱的工程队.java

package operating.entity.bank;

import org.omg.CORBA.INTERNAL;

public class EngineeringTeam implements Runnable {
    /**
     * 工程队总数
     */
    private static int total = 0;
    /**
     * 工程队 id
     */
    private int id;
    /**
     * 完成工程还需要的资金数量
     */
    private int need;
    /**
     * 目标资金
     */
    private int max;
    /**
     * 目标银行
     */
    private Bank bank;

    public EngineeringTeam(int need, Bank bank) {
        id = total++;
        this.need = max = need;
        this.bank = bank;
        this.bank.addTeam(this);
    }

    public int getId() {
        return id;
    }

    public int getNeed() {
        return need;
    }

    /**
     * 不停借钱直到完成
     */
    @Override
    public void run() {
        while (need > 0) {
            System.out.println("工程队" + id + ": 还缺 " + need + " 万元。");
            // 随机借钱
            int request = (int)(Math.random()*need) + 1;
            if (bank.borrow(id, request)) need -= request;

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        bank.payBack(max);
        System.out.println("【----工程队" + id + ": 完成任务。----】");
    }
}

 

    原文作者:xkfx
    原文地址: https://www.cnblogs.com/xkxf/p/7994912.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞