银行家算法

Java银行家(最紧缺的资源优先调用并且释放)算法。>>>>博主的第一篇文章还望指正。

package Bank;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Bank_1 {

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("leftover=");
		builder.append(Arrays.toString(leftover));
		builder.append(", process=");
		builder.append(Arrays.toString(process));
		builder.append(", types=");
		builder.append(types);
		builder.append(", Count=");
		builder.append(Count);
		builder.append(", Scarcity=");
		builder.append(Arrays.toString(Scarcity));
		builder.append("]");
		return builder.toString();
	}

	private static Scanner sc = new Scanner(System.in);
	private int[] leftover = null;
	private Process[] process = null;
	private int types;
	private int Count;
	private int[] Scarcity = null;

	public Bank_1() {
		System.out.print("Please Input The Processs Amounts\t");
		this.Count = sc.nextInt();
		this.process = new Process[Count];
		System.out.print("Please Input the Soures Types\t");
		this.types = sc.nextInt();
		this.Scarcity = new int[types];
		this.leftover = new int[types];
		for (int i = 0; i < Count; i++) {
			Process temp = new Process(types);
			this.process[i] = temp;
		}
	}

	public void Needed() {
		System.out.println("Please Input Each Process Needed, force " + Count + " * " + types);
		for (Process temp : this.process) {
			int[] tempArray = new int[types];
			for (int i = 0; i < types; i++) {
				tempArray[i] = sc.nextInt();
			}
			temp.Needed = Arrays.copyOf(tempArray, tempArray.length);
		}
	}

	public void Allocation() {
		System.out.println("Please Input Each Process Allocation, force " + Count + " * " + types);
		for (Process temp : this.process) {
			int[] tempArray = new int[types];
			for (int i = 0; i < types; i++) {
				tempArray[i] = sc.nextInt();
			}
			temp.Allocation = Arrays.copyOf(tempArray, tempArray.length);
		}
	}

	public int Allocation_Confirm() {
		for (int i = 0; i < this.Count; i++) {
			Process p = this.process[i];
			for (int j = 0; j < types; j++) {
				if (p.Allocation[j] > p.Needed[j]) {
					System.out.println("ReAllocated");
					System.out.println("-----------------------");
					return 0;
				}
			}
		}
		return 1;
	}

	public void LeftoverAndGapAndScarcity() {
		System.out.println("Pelase Input the LeftOvers");
		for (int i = 0; i < types; i++) {
			leftover[i] = sc.nextInt();
		}
		for (Process tempProcess : process) {
			tempProcess.gapConfirm();
		}
		for (int i = 0; i < this.types; i++) {
			for (Process tempProcess : process) {
				this.Scarcity[i] += tempProcess.gap[i];
			}
		}
		System.out.println("-----------------------");
	}

	public static void main(String[] args) {
		Bank_1 bank = new Bank_1();
		bank.Needed();
		for (int flag = 0; flag != 1;) {
			bank.Allocation();
			flag = bank.Allocation_Confirm();
		}
		System.out.println("Allocation Success");
		System.out.println("-----------------------");
		bank.LeftoverAndGapAndScarcity();
		System.out.println("Originnal :---------------------------------------------:");
		for (Process i : bank.process) {
			System.out.println(i);
		}
		System.out.println("-----------------------");
		bank.IsSafe();
		System.out.println();
	}

	public void IsSafe() {
		ArrayList<Integer> arr = new ArrayList<Integer>();
		try {
			for (; !IsDone();) {
				int key = 0;
				ConfirmCanRun();
				key = shouldRun();
				arr.add(key);
				ReleaseTheProcess(key);
				for (Process i : this.process) {
					System.out.println(i);
				}
				System.out.println("-----------------------");

			}
			System.out.println("It's Safe");
			for (int i : arr) {
				System.out.print(i + "----->");
			}
		} catch (Exception e) {
			System.out.println("It's Not Safe ");
		}
	}

	public void ConfirmCanRun() {
		for (Process TempProcess : this.process) {
			int count = 0;
			for (int i = 0; i < TempProcess.sorce_types; i++) {
				if (TempProcess.gap[i] <= leftover[i] && TempProcess.flag != -1) {
					count++;
				}
			}
			if (count == TempProcess.sorce_types) {
				TempProcess.flag = 1;
			}
		}
	}

	public int shouldRun() {
		int MaxValue = -1;
		int key = -1;
		int Scary = IsScarcy();
		if (Scary != -1) {
			for (int i = 0; i < this.Count; i++) {
				if (this.process[i].flag == 1) {
					if (this.process[i].Allocation[Scary] > MaxValue) {
						MaxValue = process[i].Allocation[Scary];
						this.process[i].flag = 0;
						key = i;
					}
				}
			}
			process[key].flag = 2;
		} else {
			int flat = 0;
			for (int i = 0; i < this.Count; i++) {
				if (this.process[i].flag == 1 && flat == 0) {
					this.process[i].flag = 2;
					key = i;
					flat = 1;
				} else if (this.process[i].flag == 1 && flat == 1) {
					this.process[i].flag = 0;
				}
			}
		}
		return key;
	}

	public int IsScarcy() {
		int minValue = 0;
		int key = 0;
		for (int i = 0; i < types; i++) {
			if (this.Scarcity[i] > minValue) {
				key = i;
				minValue = this.Scarcity[i];
			}
		}
		if (key == 0 && minValue == 0) {
			return -1;
		} else {
			return key;
		}
	}

	public boolean IsDone() {
		int count = 0;
		for (Process pro_temp : this.process) {
			if (pro_temp.flag == -1) {
				count++;
			}
		}
		if (count == this.Count) {
			return true;
		} else {
			return false;
		}
	}

	public void ReleaseTheProcess(int key) {
		for (int i = 0; i < this.types; i++) {
			this.leftover[i] += this.process[key].Allocation[i];
			this.Scarcity[i] -= this.process[key].Allocation[i];
			if (this.Scarcity[i] < 0) {
				this.Scarcity[i] = 0;
			}
			this.process[key].Allocation[i] = 0;
			this.process[key].Needed[i] = 0;
			this.process[key].gap[i] = 0;
		}
		this.process[key].flag = -1;
		for (Process proceTemp : this.process) {
			if (proceTemp.flag == 1) {
				proceTemp.flag = 0;
			}
		}
	}
}

class Process {
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Process [source_types=");
		builder.append(sorce_types);
		builder.append(", Needed=");
		builder.append(Arrays.toString(Needed));
		builder.append(", Allocation=");
		builder.append(Arrays.toString(Allocation));
		builder.append(", gap=");
		builder.append(Arrays.toString(gap));
		builder.append(", flag=");
		builder.append(flag);
		builder.append("]");
		return builder.toString();
	}

	int sorce_types;
	int[] Needed = null;
	int[] Allocation = null;
	int[] gap = null;
	/**
	 * -1表示运行结束, 0表示还没开始, 1表示可以运行,2表示需要运行
	 */
	int flag = 0;

	public Process(int sorce_types) {
		this.sorce_types = sorce_types;
		this.Needed = new int[sorce_types];
		this.Allocation = new int[sorce_types];
		this.gap = new int[sorce_types];
	}

	public void gapConfirm() {
		for (int i = 0; i < this.sorce_types; i++) {
			gap[i] = Needed[i] - Allocation[i];
			if (gap[i] < 0) {
				gap[i] = 0;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值