银行家算法实现--Java

1.进程类(Process.java)

package com.color.lz;

public class Process {
	//进程编号
	private int id;
	//该进程对资源还需要的量
	private int[] need;
	//已分配给该进程的资源
	private int[] allocated;
	//该进程是否运行完毕资源
	private boolean finish;
	//该经常是否能得到目前need数量的资源分配
	private boolean tempAllo;
	public Process() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Process(int id) {
		super();
		this.id = id;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getNeed(int i) {
		return need[i];
	}
	public void setNeed(int i, int need) {
		this.need[i]=need;
	}
	public int[] getNeed() {
		return need;
	}
	public void setNeed(int[] need) {
		this.need = need;
	}
	public int[] getAllocated() {
		return allocated;
	}
	public void setAllocated(int[] allocated) {
		this.allocated = allocated;
	}
	public boolean isFinish() {
		return finish;
	}
	public void setFinish(boolean finish) {
		this.finish = finish;
	}
	public boolean getTempAllo() {
		return tempAllo;
	}
	public void setTempAllo(boolean tempAllo) {
		this.tempAllo = tempAllo;
	}

}

2.资源类(Resource.java)

package com.color.lz;

public class Resource {
	//资源编号
	private int id;
	//资源的最大量
	private int max;
	//可用资源
	private int available;
	public Resource() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Resource(int id, int max, int available) {
		super();
		this.id = id;
		this.max = max;
		this.available = available;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getMax() {
		return max;
	}
	public void setMax(int max) {
		this.max = max;
	}
	public int getAvailable() {
		return available;
	}
	public void setAvailable(int available) {
		this.available = available;
	}
}

3.Banker类(实现类:Banker.java)

package com.color.lz;
import java.util.Arrays;
import java.util.Scanner;

public class Banker {
	private   int pro_count;
	private   int res_count;
	private Process[] processes;
	private Resource[]resources;
	private int available[];
	private int  runProcesses;
	private int[]request;
	private static Scanner scanner=new Scanner(System.in);
	public Banker() {
		super();
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		Banker banker=new Banker();
		banker.initResource();
		banker.initProcess();
		banker.scheduleProcess();
		scanner.close();
	}
	//初始化available数组
	public void initAvailable() {
		for (int i = 0; i < res_count; i++) {
			available[i]=resources[i].getAvailable();
		}
	}
	/**
	 * 根据资源可用数组对资源的可用量进行更改
	 */
	public void setResAvailable() {
		for (int i = 0; i < res_count; i++) {
			resources[i].setAvailable(available[i]);
		}
	}
	/**
	 * 分配资源给进程
	 * @param pro_id
	 */
	public void allocateResToPro(int pro_id) {
		//更新进程的需求量
		processes[pro_id].setNeed(subArray(processes[pro_id].getNeed(), request));
		//更新进程的分配量
		processes[pro_id].setAllocated(addArray(processes[pro_id].getAllocated(), request));
		//更新可用资源数量
		subArray(available, request);
		//更新每种资源的数量
		setResAvailable();
		if(isZeroInArr(processes[pro_id].getNeed())) {
			System.out.println(pro_id+"号进程已经运行完毕");
			processes[pro_id].setFinish(true);
			runProcesses--;
			available=addArray(available,processes[pro_id].getAllocated());
		}
	}
	/**
	 * 第一步检测:请求量是否大于需求量
	 * @param pro_id
	 * @return
	 */
	public boolean checkReqWithNeed(int pro_id) {
		int []need=processes[pro_id].getNeed();
		for (int i = 0; i < res_count; i++) {
			if(request[i]>need[i]) return false;
		}
		return true;
	}
	
	/**
	 * 第二步检测:请求量是否大于可用量
	 * @return
	 */
	public boolean checkReqWithAvailable() {
		return compareArr(request, available);
	}
	
	/**
	 * 算法的核心关键部分
	 * 第三步检测:是否会进入不安全状态
	 * @param pro_id
	 * @return
	 */
	public boolean isEnterNotSafe(int pro_id,int[] tempArr) {
		for (int i = 0; i < pro_count; i++) {
			processes[i].setTempAllo(false);
		}
		subArray(tempArr, request);
		addArray(processes[pro_id].getAllocated(), request);
		subArray(processes[pro_id].getNeed(), request);
		tempAllo(pro_id,tempArr);
		//复原
		subArray(processes[pro_id].getAllocated(), request);
		addArray(processes[pro_id].getNeed(), request);
		for (int i = 0; i < pro_count; i++) {
			if(!processes[i].getTempAllo()) {
				return false;
			}
		}
		return true;
	}
	/**
	 * 进行临时分配
	 */
	public void tempAllo(int pro_id,int[] tempArr) {
		for (int i = 0; i < pro_count; i++) {
			if(processes[i].getTempAllo())continue;
			if(processes[i].isFinish())continue;
			if(compareArr(processes[i].getNeed(), tempArr)) {
				addArray(tempArr,processes[i].getAllocated());
				processes[i].setTempAllo(true);
				tempAllo(pro_id, tempArr);
			}
		}
		return;
	}
	/**
	 * 当a数组中每一个元素都小于等于b中时,返回true;否则返回false
	 * @param a
	 * @param b
	 * @return
	 */
	public boolean compareArr(int a[], int b[]) {
		for (int i = 0; i < b.length; i++) {
			if(a[i]>b[i])return false;
		}
		return true;
	}
	public boolean isBlowZero(int[] a) {
		System.out.println(Arrays.toString(a)+"是否低于0");
		for (int i = 0; i < a.length; i++) {
			if(a[i]<0)return true;
		}
		return false;
	}
	/**
	 * 该方法的功能是完成两个数组的减操作,并返回a数组
	 * @param pro_id
	 */
	public int[] subArray(int[]a,int[]b) {
		for (int i = 0; i < b.length; i++) {
			a[i]-=b[i];
		}
		return a;
	}
	
	/**
	 * 该方法的功能是完成两个数组的加操作
	 * @param a
	 * @param b
	 * @return
	 */
	public int[] addArray(int[]a,int[]b) {
		for (int i = 0; i < b.length; i++) {
			a[i]+=b[i];
		}
		return a;
	}
	
	/**
	 * 该方法判断数组元素是否都为0
	 * @param a
	 * @return
	 */
	public boolean  isZeroInArr(int[] a) {
		for (int j = 0; j < a.length; j++) {
			if(a[j]!=0)return false;
		}
		return true;
	}
	/**
	 * 该方法完成进程对资源的请求
	 * @param pro_id
	 */
	public void inputReq(int pro_id) {
		request=new int[res_count];
		for (int i = 0; i < res_count; i++) {
			System.out.println("请输入"+pro_id+"号进程对"+i+"号资源的请求量:");
			request[i]=scanner.nextInt();
		}
	}
	/**
	 * 该方法显示哪些进程还在运行中
	 */
	public void showRunPro() {
		System.out.println("以下进程仍在就绪或者运行中:");
		System.out.println("系统剩余可用资源为:"+Arrays.toString(available));
		System.out.println("进程编号"+"                           "+"还需资源数量"+"                           "+"已获得资源量");
		for (int i = 0; i < processes.length; i++) {
			if(!processes[i].isFinish()) {
				System.out.print(processes[i].getId());
				System.out.print("       	 ");
				System.out.print(Arrays.toString(processes[i].getNeed()));
				System.out.print("           ");
				System.out.print(Arrays.toString(processes[i].getAllocated()));
				System.out.println();
			}
		}
	}
	public boolean checkProID(int pro_id) {
		if(pro_id<0||pro_id>=pro_count)return false;
		else if(processes[pro_id].isFinish())return false;
		return true;
	}
	public  void scheduleProcess() {
		//调度进程
		while(true) {
			//当未运行完毕的进程为0时,退出调度
			if(runProcesses==0) {
				System.out.println("进程调度完毕!");
				break;
			}
			showRunPro();
			System.out.println();
			System.out.println("请输入调度的进程编号:");
			int pro_id=scanner.nextInt();
			//进程不存在
			if(!checkProID(pro_id)) {
				System.out.println("该进程不存在或已运行完毕!");
				continue;
			}
			//进程存在
			
			//请求资源
			inputReq(pro_id);
			boolean canAllocate=true;
			int[] tempArr=new int[res_count];
			tempArr=Arrays.copyOf(available, res_count);
			//三步检测
			if(!checkReqWithNeed(pro_id)) {
				System.out.println("请求量大于需求量!");
				canAllocate=false;
			} else if(!checkReqWithAvailable()) {
				System.out.println("请求量大于可用量!");
				canAllocate=false;
			}else if(!isEnterNotSafe(pro_id,tempArr)) {
				System.out.println("系统会进入不安全状态");
				canAllocate=false;
			}
			if(canAllocate) {
				allocateResToPro(pro_id);
			}
		}
	}
	public   void initProcess() {
/*		//初始化进程
		System.out.println("请输入有几个进程:");
		pro_count=scanner.nextInt();
		runProcesses=pro_count;
		processes=new Process[pro_count];
		for (int i = 0; i < pro_count; i++) {
			processes[i]=new Process(i);
			processes[i].setAllocated(new int[res_count]);
			processes[i].setNeed(new int[res_count]);
			for (int j = 0; j < res_count; j++) {
				System.out.println("请输入"+i+"号进程对"+j+"号资源的需求量");
				processes[i].setNeed(j, scanner.nextInt());
			}
		}*/
		
		pro_count=runProcesses=5;
		Process p0=new Process(0);
		Process p1=new Process(1);
		Process p2=new Process(2);
		Process p3=new Process(3);
		Process p4=new Process(4);
		p0.setNeed(new int[] {5,7,7});
		p1.setNeed(new int[] {3,4,5});
		p2.setNeed(new int[] {3,4,2});
		p3.setNeed(new int[] {5,2,5});
		p4.setNeed(new int[] {3,5,4});
		p0.setAllocated(new int[3]);
		p1.setAllocated(new int[3]);
		p2.setAllocated(new int[3]);
		p3.setAllocated(new int[3]);
		p4.setAllocated(new int[3]);
		processes= new Process[]{p0,p1,p2,p3,p4};
	}

	public   void initResource() {
/*		//初始化资源
		System.out.println("请输入有几种资源:");
		res_count=scanner.nextInt();	
		available=new int[res_count];
		resources=new Resource[res_count];
		for (int i = 0; i < res_count; i++) {
			resources[i]=new Resource();
			System.out.println("请输入"+i+"号资源的 可用数量:");
			available[i]=scanner.nextInt();
		}*/
		res_count=3;
		available=new int[] {10,8,9};
		Resource r0=new Resource(0,10,10);
		Resource r1=new Resource(1,10,10);
		Resource r2=new Resource(2,10,10);
		resources=new Resource[] {r0,r1,r2};
		setResAvailable();
	}
}

注:上诉代码我经过了一些测试,没发现问题,但是我感觉还充满了不足,希望各位江湖好友能热心指出,谢谢!

    原文作者:银行家问题
    原文地址: https://blog.csdn.net/LZCDTU/article/details/84071116
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞