导航:首页 > 编程语言 > java银行家算法

java银行家算法

发布时间:2022-08-04 12:06:27

㈠ 求用java语言编写的银行家算法的源代码

import java.util.*; class ThreadTest { static int type = 4, num = 10; //定义资源数目和线程数目 static int[] resource = new int[type]; //系统资源总数 //static int[] Resource = new int[type]; //副本 static Random rand = new Random(); static Bank[] bank = new Bank[num]; //线程组 Bank temp = new Bank(); public void init() { //初始化组中每个线程,随机填充系统资源总数 for(int i = 0; i < type; i++) resource[i] = rand.nextInt(10) + 80; System.out.print("Resource:"); for(int i = 0; i < type; i++) System.out.print(" " + resource[i]); System.out.println(""); for(int i = 0; i < bank.length; i++) bank[i] = new Bank("#" + i); } public ThreadTest4() { init(); } class Bank extends Thread { //银行家算法避免死锁 public int[] max = new int[type], //总共需求量 need = new int[type], //尚需资源量 allocation = new int[type]; //已分配量 private int[] request = new int[type], //申请资源量 Resource = new int[type]; //资源副本 private boolean isFinish = false; //线程是否完成 int[][] table = new int[bank.length][type*4]; //二维资源分配表 private void init() { // 随机填充总共、尚需、已分配量 synchronized(resource) { for(int i = 0; i < type; i++) { max[i] = rand.nextInt(5) + 10; need[i] = rand.nextInt(10); allocation[i] = max[i] - need[i]; resource[i] -= allocation[i]; //从系统资源中减去已分配的 } printer(); for(int i = 0; i < type; i++) { if(resource[i] < 0) { //若出现已分配量超出系统资源总数的错误则退出 System.out.println("The summation of Threads' allocations is out of range!"); System.exit(1); } } } } public Bank(String s) { setName(s); init(); start(); } public Bank() { //none } public void run() { try { sleep(rand.nextInt(2000)); } catch(InterruptedException e) { throw new RuntimeException(e); } while(true) { //程序没有完成时一直不断申请资源 if(askFor() == false) { try { sleep(1000); } catch(InterruptedException e) { throw new RuntimeException(e); } } else tryRequest(); if(noNeed() == true) break; } //休眠一段时间模拟程序运行 try { sleep(1000); } catch(InterruptedException e) { throw new RuntimeException(e); } System.out.println(getName() + " finish!"); synchronized(resource) { //运行结束释放占有资源 for(int i = 0; i < type; i++) { resource[i] += allocation[i]; need[i] = allocation[i] = max[i] = 0; } } } private void printer() { //打印当前资源信息 System.out.print(getName() + " Max:"); for(int i = 0; i < type; i++) System.out.print(" " + max[i]); System.out.print(" Allocation:"); for(int i = 0; i < type; i++) System.out.print(" " + allocation[i]); System.out.print(" Need:"); for(int i = 0; i < type; i++) System.out.print(" " + need[i]); System.out.print(" Available:"); for(int i = 0; i < type; i++) System.out.print(" " + resource[i]); System.out.println(""); } private boolean askFor() { //随机产生申请资源量并检测是否超标 boolean canAsk = false; for(int i = 0; i < type; i++) { request[i] = rand.nextInt(20); //防止申请量超过所需量 if(request[i] > need[i]) request[i] = need[i]; } for(int i = 0; i < type; i++) //防止随机申请资源全为0 if(request[i] > 0) canAsk = true; synchronized(resource) { //锁住可供资源检查是否超标 for(int i = 0; i < type; i++) { if(request[i] > resource[i]) //如果申请资源超过可供资源则等待一段时间后重新申请 return false; } } return canAsk; } private void tryRequest() { //创建副本尝试分配请求 synchronized(resource) { for(int i = 0; i < type; i++) //依然要防止请求量超出范围 if(request[i] > resource[i]) return; for(int i = 0; i < type; i++) { //复制资源量并减去需求量到一个副本上 Resource[i] = resource[i]; Resource[i] -= request[i]; } System.out.print(getName() + " ask for:"); for(int i = 0; i < type; i++) System.out.print(" " + request[i]); System.out.println(""); if(checkSafe() == true) { //如果检查安全则将副本值赋给资源量并修改占有量和需求量 for(int i = 0; i < type; i++) { resource[i] = Resource[i]; allocation[i] += request[i]; need[i] -= request[i]; } System.out.println(getName() + " request succeed!"); } else System.out.println(getName() + " request fail!"); } } private boolean checkSafe() { //银行家算法检查安全性 synchronized(bank) { //将线程资源信息放入二维资源分配表检查安全性,0~ type可用资源/type~type*2所需资源/type* 2~type*3占有资源/type*3~-1可用+占用资源 for(int i = 0; i < bank.length; i++) { for(int j = type; j < type*2; j++) { table[i][j] = bank[i].need[j%type]; } for(int j = type*2; j < type*3; j++) { table[i][j] = bank[i].allocation[j%type]; } } //冒泡排序按需求资源从小到大排 for(int i = 0; i < bank.length; i++) { for(int j = i; j < bank.length-1; j++) { sort(j, 4); } } //进行此时刻的安全性检查 for(int i = 0; i < type; i++) { table[0][i] = Resource[i]; table[0][i+type*3] = table[0][i] + table[0][i+type*2]; if(table[0][i+type*3] < table[1][i+type]) return false; } for(int j = 1; j < bank.length-1; j++) { for(int k = 0; k < type; k++) { table[j][k] = table[j-1][k+type*3]; table[j][k+type*3] = table[j][k] + table[j][k+type*2]; if(table[j][k+type*3] < table[j+1][k+type]) return false; } } } return true; } private void sort(int j, int k) { //递归冒泡排序 int tempNum; if(table[j][k] > table[j+1][k]) { for(int i = type; i < type*2; i++) { tempNum = table[j][i]; table[j][i] = table[j+1][i]; table[j+1][i] = tempNum; } /*temp = bank[j]; bank[j] = bank[j+1]; bank[j+1] = temp;*/ } else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围 sort(j, k+1); } private boolean noNeed() { //是否还需要资源 boolean finish = true; for(int i = 0; i < type; i++) { if(need[i] != 0) { finish = false; break; } } return finish; } } public static void main(String[] args) { ThreadTest t = new ThreadTest(); //后台线程,设定程序运行多长时间后自动结束 new Timeout(30000, "---Stop!!!---"); } }

㈡ 银行家算法的实现

银行家算法是从当前状态出发,逐个按安全序列检查各客户中谁能完成其工作,然后假定其完成工作且归还全部贷款,再进而检查下一个能完成工作的客户。如果所有客户都能完成工作,则找到一个安全序列,银行家才是安全的。

♦ 与预防死锁的几种方法相比较,限制条件少,资源利用程度提高了。

♦ 缺点:该算法要求客户数保持固定不变,这在多道程序系统中是难以做到的;该算法保证所有客户在有限的时间内得到满足,但实时客户要求快速响应,所以要考虑这个因素;由于要寻找一个安全序列,实际上增加了系统的开销.

Banker algorithm 最重要的一点是:保证操作系统的安全状态!这也是操作系统判断是否分配给一个进程资源的标准!那什么是安全状态?举个小例子,进程 P 需要申请 8 个资源(假设都是一样的),已经申请了 5 个资源,还差 3 个资源。若这个时候操作系统还剩下 2 个资源。很显然,这个时候操作系统无论如何都不能再分配资源给进程 P 了,因为即使全部给了他也不够,还很可能会造成死锁。若这个时候操作系统还有 3 个资源,无论 P 这一次申请几个资源,操作系统都可以满足他,因为操作系统可以保证 P 不死锁,只要他不把剩余的资源分配给别人,进程 P 就一定能顺利完成任务。 为什么银行家算法是可行的呢?这里需要严格的证明一下。不管任何时候,操作系统分配资源的时候都可以保证当前接受资源的进程不会陷入死锁,因为操作系统总是可以满足该进程需要的资源的。假设有 n 个进程 {p1, p2, p3, … pn} ,最后一个分配到资源的是 pi , pi 还需要 mi 个资源,假设此时操作系统还有 m 个资源剩余。那么很显然 m>=mi !而且如果之后操作系统又把资源分配给其他进程了,假设是 pj , pj 还需要 mj 个资源,同理可知 m>=mj !也就是说在所有的进程中,还需要的资源数总是有小于 m 的!这样就可以保证资源数永远不会为 0 ,即使可能暂时性为 0 。另外,还需要保证资源数不会减少!而且,所有已经分配到资源的进程总有一天会归还它所拥有的资源!根据操作系统再分配的时候的状态即可判定。

㈢ java实现银行家算法中的一个 +=不理解 请求大神解答

Http协议Java Web发servlet/jsp些基础理解面内容先要解Http协议

Http协议基本request/response模型请求/响应模型通俗讲问答模式:
浏览器向服务器发起request请求问;

服务器收请求返response响应答

说Servlet/JSPrequestresonse两象清楚吧其实Java WebHttp协议两东西抽象Java类型已

接说Java Web发session
Http没状态协议说原始Http协议浏览器request请求间没关系通俗说说句忘句实际要想发Java Web应用应该让些请求间关系需要request请求间创建些联系session其实"文翻译错通俗讲:要想顺利交谈需要说句想起句
所些建立联系request请求属于某session题目问:前请求意思

面说说session技术实现细节吧:(致应问题三面)
(1)实际Java Web应用session占用服务器段内存空间保存联系request请求间需要保存共享变量;
(2)部session实现同客户相同浏览器段间(称作session超间)内请求作状态共享保持打同浏览器比IE及Chrome启同session且关闭浏览器session随销毁;
(3)session通getAttribute()setAttribute()进行共享变量获取设置说要想保存状态需要用

㈣ 在C++中,编写的银行家算法中有以下的语句,麻烦帮忙解释这3个语句,并用java实现,怎么实现,谢谢先!!

在某时刻系统中所有进程可以排列一个安全序列:,刚称此时,系统是安全的.
所谓安全序列是指对于P2,都有它所需要剩余资源数量不大于系统掌握的剩余的空间资源与所有Pi(j<i)所占的资源之和.
2.不安全状态可能产生死锁.
目前状态 最大需求 尚需
P1 3 9 6
P2 5 10 5
P3 2 4 2

在每一次进程中申请的资源,判定一下,若实际分配的话,之后系统是否安全.
3.银行家算法的思路:
1),进程一开始向系统提出最大需求量.
2),进程每次提出新的需求(分期贷款)都统计是否超出它事先提出的最大需求量.
3),若正常,则判断该进程所需剩余剩余量(包括本次申请)是否超出系统所掌握的
剩余资源量,若不超出,则分配,否则等待.
4.银行家算法的数据结构.
1),系统剩余资源量A[n],其中A[n]表示第I类资源剩余量.
2),各进程最大需求量,B[m][n],其中B[j][i]表示进程j对i
类资源最大需求.
3),已分配资源量C[m][n],其中C[j][i]表示系统j程已得到的第i资源的数量.
4),剩余需求量.D[m][n],其中D[j][i]对第i资源尚需的数目.
5.银行家算法流程:当某时刻,某进程时,提出新的资源申请,系统作以下操作:
1),判定E[n]是否大于D[j][n],若大于,表示出错.
2),判定E[n]是否大于系统剩余量A[n],若大于,则该进程等待.
3),若以上两步没有问题,尝试分配,即各变量作调整.
4),按照安全性推测算法,判断,分配过后,系统是否安全,若安全,则实际分配,否则,撤消分配,让进程等待.
6."安全性检测"算法
1),先定义两个变量,用来表示推算过程的数据.
F[n]=A[n],表示推算过程中,系统中剩余资源量的变化.
J[n]=False表示推算过程中各进程是否假设"已完成"
2),流程:
在"剩余"的进程中(在推算)过程中,一些进程假设已完成,查找D[j][n]<=F[n]的进程,找到后令J[j]=True
(假设该进程完成),F[n]+D[j][n](该进程所占资源释放),如此循环执行.
若最后,所有的F[n]=True(在推算过程中,所有进程均可以完成),则表示(分配过后)系统是安全的,否则系统是不安全的.

#include "malloc.h"
#include "stdio.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available
{
int value;
struct available *next;
};
struct need
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*availabletemp,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2,*pathtemp;
char c;
printf("\nPlease enter the type of sources the system has:\n");
scanf("%d",&colum);
printf("Please enter the number of processes now in the memory:\n");
scanf("%d",&row);
printf("Please enter the allocation array:\n");
for(i=0;i<row;i++)
{
printf("The allocation for process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c system resource allocated:\n",'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("Please enter the max array:\n");
for(i=0;i<row;i++)
{
printf("The max needed from process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c maxium system resource may needed:\n",'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
if(status==1)
{
maxhead->next=maxium2;
status++;
}
maxium1->next=maxium2;
maxium1=maxium2;
}
}
}
maxium2->next=NULL;
status=0;
printf("Please enter the available array now exists in the system:\n");
for (j=0;j<colum;j++)
{
printf("The type %c available system resource number:\n",'A'+j);
if(status==0)
{
avahead=available1=available2=(struct available*)malloc(avalen);
workhead=work1=work2=(struct available*)malloc(avalen);
available1->next=available2->next=NULL;
work1->next=work2->next=NULL;
scanf("%d",&available1->value);
work1->value=available1->value;
status++;
}
else
{
available2=(struct available*)malloc(avalen);
work2=(struct available*)malloc(avalen);
scanf("%d,%d",&available2->value);
work2->value=available2->value;
if(status==1)
{
avahead->next=available2;
workhead->next=work2;
status++;
}
available1->next=available2;
available1=available2;
work1->next=work2;
work1=work2;
}
}
available2->next=NULL;
work2->next=NULL;
status=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<row;i++)
for (j=0;j<colum;j++)
{
if(status==0)
{
needhead=need1=need2=(struct need*)malloc(needlen);
need1->next=need2->next=NULL;
need1->value=maxtemp->value-alloctemp->value;
status++;
}
else
{
need2=(struct need *)malloc(needlen);
need2->value=(maxtemp->value)-(alloctemp->value);
if(status==1)
{
needhead->next=need2;
status++;
}
need1->next=need2;
need1=need2;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
status=0;
for(i=0;i<row;i++)
{
if(status==0)
{
finihead=finish1=finish2=(struct finish*)malloc(finilen);
finish1->next=finish2->next=NULL;
finish1->stat=0;
status++;
}
else
{
finish2=(struct finish*)malloc(finilen);
finish2->stat=0;
if(status==1)
{
finihead->next=finish2;
status++;
}
finish1->next=finish2;
finish1=finish2;
}
}
finish2->next=NULL; /*Initialization compleated*/
status=0;
processtest=0;
for(temp=0;temp<row;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;i<row;i++)
{
worktemp1=worktemp;
if(finishtemp->stat==0)
{
for(j=0;j<colum;j++,needtemp=needtemp->next,worktemp=worktemp->next)
if(needtemp->value<=worktemp->value)
processtest++;
if(processtest==colum)
{
for(j=0;j<colum;j++)
{
worktemp1->value+=alloctemp->value;
worktemp1=worktemp1->next;
alloctemp=alloctemp->next;
}
if(status==0)
{
pathhead=path1=path2=(struct path*)malloc(pathlen);
path1->next=path2->next=NULL;
path1->value=i;
status++;
}
else
{
path2=(struct path*)malloc(pathlen);
path2->value=i;
if(status==1)
{
pathhead->next=path2;
status++;
}
path1->next=path2;
path1=path2;
}
finishtemp->stat=1;
}
else
{
for(t=0;t<colum;t++)
alloctemp=alloctemp->next;
finishtemp->stat=0;
}
}
else
for(t=0;t<colum;t++)
{
needtemp=needtemp->next;
alloctemp=alloctemp->next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->next;
}
}
path2->next=NULL;
finishtemp=finihead;
for(temp=0;temp<row;temp++)
{
if(finishtemp->value==0)
{
printf("\nWARNING,the system is in nonsafe status!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\nThe system is in safe status!\n");
printf("\nThe safe sequence is: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
}

㈤ Java语言期末课程设计“操作系统中银行家算法的实现”

有电子版可以参考。

㈥ java 银行算法

import java.util.*;

class ThreadTest {
static int type = 4, num = 10; //定义资源数目和线程数目
static int[] resource = new int[type]; //系统资源总数
//static int[] Resource = new int[type]; //副本
static Random rand = new Random();
static Bank[] bank = new Bank[num]; //线程组
Bank temp = new Bank();

public void init() {
//初始化组中每个线程,随机填充系统资源总数
for(int i = 0; i < type; i++)
resource[i] = rand.nextInt(10) + 80;
System.out.print("Resource:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
for(int i = 0; i < bank.length; i++)
bank[i] = new Bank("#" + i);
}
public ThreadTest4() {
init();
}

class Bank extends Thread {
//银行家算法避免死锁
public int[]
max = new int[type], //总共需求量
need = new int[type], //尚需资源量
allocation = new int[type]; //已分配量
private int[]
request = new int[type], //申请资源量
Resource = new int[type]; //资源副本
private boolean isFinish = false; //线程是否完成
int[][] table = new int[bank.length][type*4]; //二维资源分配表

private void init() {
// 随机填充总共、尚需、已分配量
synchronized(resource) {
for(int i = 0; i < type; i++) {
max[i] = rand.nextInt(5) + 10;
need[i] = rand.nextInt(10);
allocation[i] = max[i] - need[i];
resource[i] -= allocation[i]; //从系统资源中减去已分配的
}
printer();
for(int i = 0; i < type; i++) {
if(resource[i] < 0) {
//若出现已分配量超出系统资源总数的错误则退出
System.out.println("The summation of Threads' allocations is out of range!");
System.exit(1);
}
}
}
}

public Bank(String s) {
setName(s);
init();
start();
}
public Bank() {
//none
}

public void run() {
try {
sleep(rand.nextInt(2000));
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
while(true) {
//程序没有完成时一直不断申请资源
if(askFor() == false) {
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
else
tryRequest();
if(noNeed() == true)
break;
}
//休眠一段时间模拟程序运行
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " finish!");
synchronized(resource) {
//运行结束释放占有资源
for(int i = 0; i < type; i++) {
resource[i] += allocation[i];
need[i] = allocation[i] = max[i] = 0;
}
}
}

private void printer() {
//打印当前资源信息
System.out.print(getName() + " Max:");
for(int i = 0; i < type; i++)
System.out.print(" " + max[i]);
System.out.print(" Allocation:");
for(int i = 0; i < type; i++)
System.out.print(" " + allocation[i]);
System.out.print(" Need:");
for(int i = 0; i < type; i++)
System.out.print(" " + need[i]);
System.out.print(" Available:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
}
private boolean askFor() {
//随机产生申请资源量并检测是否超标
boolean canAsk = false;
for(int i = 0; i < type; i++) {
request[i] = rand.nextInt(20);
//防止申请量超过所需量
if(request[i] > need[i])
request[i] = need[i];
}
for(int i = 0; i < type; i++) //防止随机申请资源全为0
if(request[i] > 0)
canAsk = true;
synchronized(resource) {
//锁住可供资源检查是否超标
for(int i = 0; i < type; i++) {
if(request[i] > resource[i])
//如果申请资源超过可供资源则等待一段时间后重新申请
return false;
}
}
return canAsk;
}
private void tryRequest() {
//创建副本尝试分配请求
synchronized(resource) {
for(int i = 0; i < type; i++)
//依然要防止请求量超出范围
if(request[i] > resource[i])
return;
for(int i = 0; i < type; i++) {
//复制资源量并减去需求量到一个副本上
Resource[i] = resource[i];
Resource[i] -= request[i];
}
System.out.print(getName() + " ask for:");
for(int i = 0; i < type; i++)
System.out.print(" " + request[i]);
System.out.println("");
if(checkSafe() == true) {
//如果检查安全则将副本值赋给资源量并修改占有量和需求量
for(int i = 0; i < type; i++) {
resource[i] = Resource[i];
allocation[i] += request[i];
need[i] -= request[i];
}
System.out.println(getName() + " request succeed!");
}
else
System.out.println(getName() + " request fail!");
}
}
private boolean checkSafe() {
//银行家算法检查安全性
synchronized(bank) {
//将线程资源信息放入二维资源分配表检查安全性,0~type可用资源/type~type*2所需资源/type*2~type*3占有资源/type*3~-1可用+占用资源
for(int i = 0; i < bank.length; i++) {
for(int j = type; j < type*2; j++) {
table[i][j] = bank[i].need[j%type];
}
for(int j = type*2; j < type*3; j++) {
table[i][j] = bank[i].allocation[j%type];
}
}
//冒泡排序按需求资源从小到大排
for(int i = 0; i < bank.length; i++) {
for(int j = i; j < bank.length-1; j++) {
sort(j, 4);
}
}
//进行此时刻的安全性检查
for(int i = 0; i < type; i++) {
table[0][i] = Resource[i];
table[0][i+type*3] = table[0][i] + table[0][i+type*2];
if(table[0][i+type*3] < table[1][i+type])
return false;
}
for(int j = 1; j < bank.length-1; j++) {
for(int k = 0; k < type; k++) {
table[j][k] = table[j-1][k+type*3];
table[j][k+type*3] = table[j][k] + table[j][k+type*2];
if(table[j][k+type*3] < table[j+1][k+type])
return false;
}
}
}
return true;
}
private void sort(int j, int k) {
//递归冒泡排序
int tempNum;
if(table[j][k] > table[j+1][k]) {
for(int i = type; i < type*2; i++) {
tempNum = table[j][i];
table[j][i] = table[j+1][i];
table[j+1][i] = tempNum;
}
/*temp = bank[j];
bank[j] = bank[j+1];
bank[j+1] = temp;*/
}
else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围
sort(j, k+1);
}
private boolean noNeed() {
//是否还需要资源
boolean finish = true;
for(int i = 0; i < type; i++) {
if(need[i] != 0) {
finish = false;
break;
}
}
return finish;
}
}

public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//后台线程,设定程序运行多长时间后自动结束
new Timeout(30000, "---Stop!!!---");
}
}
希望对你能有所帮助。

㈦ 求JAVA语言的银行家算法

银行家算法

一.程序说明:

本算法有3个进程,3类资源。初始可用资源向量为Available{10,8,7},然后设置各进程的最大需求矩阵MAX以及分配矩阵Alloction,由此算出需求矩阵Need。然后判断当前系统资源分配是否处于安全状态,否则结束进程。最后,在当前分配资源后的系统安全时,选择一进程,并请求各类所需资源矩阵Request,尝试分配并修改资源分配状况,判断此进程请求是否该分配,进而进入安全算法判断是否能形成一个安全序列,如果有则分配,否则不分配。

二.程序代码:

算法类:

package bankerclass;

import java.util.Scanner;

public class BankerClass {

int[] Available = {10, 8, 7};

int[][] Max = new int[3][3];

int[][] Alloction = new int[3][3];

int[][] Need = new int[3][3];

int[][] Request = new int[3][3];

int[] Work = new int[3];

int num = 0;//进程编号

Scanner in = new Scanner(System.in);

public BankerClass() {

// Max={{6,3,2},{5,6,1},{2,3,2}};

}

public void setSystemVariable(){//设置各初始系统变量,并判断是否处于安全状态。

setMax();

setAlloction();

printSystemVariable();

SecurityAlgorithm();

}

public void setMax() {//设置Max矩阵

System.out.println("请设置各进程的最大需求矩阵Max:");

for (int i = 0; i < 3; i++) {

System.out.println("请输入进程P" + i + "的最大资源需求量:");

for (int j = 0; j < 3; j++) {

Max[i][j] = in.nextInt();

}

}

}

public void setAlloction() {//设置已分配矩阵Alloction

System.out.println("请设置请各进程分配矩阵Alloction:");

for (int i = 0; i < 3; i++) {

System.out.println("晴输入进程P" + i + "的分配资源量:");

for (int j = 0; j < 3; j++) {

Alloction[i][j] = in.nextInt();

}

}

System.out.println("Available=Available-Alloction.");

System.out.println("Need=Max-Alloction.");

for (int i = 0; i < 3; i++) {//设置Alloction矩阵

for (int j = 0; j < 3; j++) {

Available[i] = Available[i] - Alloction[j][i];

}

}

for (int i = 0; i < 3; i++) {//设置Need矩阵

for (int j = 0; j < 3; j++) {

Need[i][j] = Max[i][j] - Alloction[i][j];

}

}

}

public void printSystemVariable(){

System.out.println("此时资源分配量如下:");

System.out.println("进程 "+" Max "+" Alloction "+" Need "+" Available ");

for(int i=0;i<3;i++){

System.out.print("P"+i+" ");

for(int j=0;j<3;j++){

System.out.print(Max[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Alloction[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Need[i][j]+" ");

}

System.out.print("| ");

if(i==0){

for(int j=0;j<3;j++){

System.out.print(Available[j]+" ");

}

}

System.out.println();

}

}

public void setRequest() {//设置请求资源量Request

System.out.println("请输入请求资源的进程编号:");

num= in.nextInt();//设置全局变量进程编号num

System.out.println("请输入请求各资源的数量:");

for (int j = 0; j < 3; j++) {

Request[num][j] = in.nextInt();

}

System.out.println("即进程P" + num + "对各资源请求Request:(" + Request[num][0] + "," + Request[num][1] + "," + Request[num][2] + ").");

BankerAlgorithm();

}

public void BankerAlgorithm() {//银行家算法

boolean T=true;

if (Request[num][0] <= Need[num][0] && Request[num][1] <= Need[num][1] && Request[num][2] <= Need[num][2]) {//判断Request是否小于Need

if (Request[num][0] <= Available[0] && Request[num][1] <= Available[1] && Request[num][2] <= Available[2]) {//判断Request是否小于Alloction

for (int i = 0; i < 3; i++) {

Available[i] -= Request[num][i];

Alloction[num][i] += Request[num][i];

Need[num][i] -= Request[num][i];

}

} else {

System.out.println("当前没有足够的资源可分配,进程P" + num + "需等待。");

T=false;

}

} else {

System.out.println("进程P" + num + "请求已经超出最大需求量Need.");

T=false;

}

if(T==true){

printSystemVariable();

System.out.println("现在进入安全算法:");

SecurityAlgorithm();

}

}

public void SecurityAlgorithm() {//安全算法

boolean[] Finish = {false, false, false};//初始化Finish

int count = 0;//完成进程数

int circle=0;//循环圈数

int[] S=new int[3];//安全序列

for (int i = 0; i < 3; i++) {//设置工作向量

Work[i] = Available[i];

}

System.out.println("进程 "+" Work "+" Alloction "+" Need "+"Work+Available ");

while (count < 3) {

for (int i = 0; i < 3; i++) {

if (Finish[i]==false&&Need[i][0]<=Work[0]&&Need[i][1]<=Work[1]&&Need[i][2]<=Work[2]) {//判断条件

System.out.print("P"+i+" ");

for (int k = 0; k < 3; k++){

System.out.print(Work[k]+" ");

}

System.out.print("| ");

for (int j = 0; j<3;j++){

Work[j]+=Alloction[i][j];

}

Finish[i]=true;//当当前进程能满足时

S[count]=i;//设置当前序列排号

count++;//满足进程数加1

for(int j=0;j<3;j++){

System.out.print(Alloction[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Need[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Work[j]+" ");

}

System.out.println();

}

}

circle++;//循环圈数加1

if(count==3){//判断是否满足所有进程需要

System.out.print("此时存在一个安全序列:");

for (int i = 0; i<3;i++){//输出安全序列

System.out.print("P"+S[i]+" ");

}

System.out.println("故当前可分配!");

break;//跳出循环

}

if(count<circle){//判断完成进程数是否小于循环圈数

count=5;

System.out.println("当前系统处于不安全状态,故不存在安全序列。");

break;//跳出循环

}

}

}

}

主类:

package bankerclass;

import java.util.Scanner;

public class TestBankerClass {

public static void main(String[] args) {

// TODO code application logic here

boolean Choose = true;

String C;

Scanner in = new Scanner(System.in);

BankerClass T = new BankerClass();

System.out.println("这是一个三个进程,初始系统可用三类资源为{10,8,7}的银行家算法:");

T.setSystemVariable();

while (Choose == true) {

T.setRequest();

System.out.println("您是否还要进行请求:y/n?");

C = in.nextLine();

if (C.endsWith("n")) {

Choose = false;

}

}

}

}

三.随机运行过程

1.

run:

这是一个三个进程,初始系统可用三类资源为{10,8,7}的银行家算法:

请设置各进程的最大需求矩阵Max:

请输入进程P0的最大资源需求量:

8 7 5

请输入进程P1的最大资源需求量:

5 2 5

请输入进程P2的最大资源需求量:

6 6 2

请设置请各进程分配矩阵Alloction:

晴输入进程P0的分配资源量:

3 2 0

晴输入进程P1的分配资源量:

2 0 2

晴输入进程P2的分配资源量:

1 3 2

Available=Available-Alloction.

Need=Max-Alloction.

此时资源分配量如下:

进程 Max Alloction Need Available

P0 8 7 5 | 3 2 0 | 5 5 5 | 4 3 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 3 2 | 5 3 0 |

进程 Work Alloction Need Work+Available

P1 4 3 3 | 2 0 2 | 3 2 3 | 6 3 5

P2 6 3 5 | 1 3 2 | 5 3 0 | 7 6 7

P0 7 6 7 | 3 2 0 | 5 5 5 | 10 8 7

此时存在一个安全序列:P1 P2 P0 故当前可分配!

请输入请求资源的进程编号:

0

请输入请求各资源的数量:

1 0 0

即进程P0对各资源请求Request:(1,0,0).

此时资源分配量如下:

进程 Max Alloction Need Available

P0 8 7 5 | 4 2 0 | 4 5 5 | 3 3 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 3 2 | 5 3 0 |

现在进入安全算法:

进程 Work Alloction Need Work+Available

P1 3 3 3 | 2 0 2 | 3 2 3 | 5 3 5

P2 5 3 5 | 1 3 2 | 5 3 0 | 6 6 7

P0 6 6 7 | 4 2 0 | 4 5 5 | 10 8 7

此时存在一个安全序列:P1 P2 P0 故当前可分配!

您是否还要进行请求:y/n?

y

请输入请求资源的进程编号:

2

请输入请求各资源的数量:

0 1 0

即进程P2对各资源请求Request:(0,1,0).

此时资源分配量如下:

进程 Max Alloction Need Available

P0 8 7 5 | 4 2 0 | 4 5 5 | 3 2 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 4 2 | 5 2 0 |

现在进入安全算法:

进程 Work Alloction Need Work+Available

P1 3 2 3 | 2 0 2 | 3 2 3 | 5 2 5

P2 5 2 5 | 1 4 2 | 5 2 0 | 6 6 7

P0 6 6 7 | 4 2 0 | 4 5 5 | 10 8 7

此时存在一个安全序列:P1 P2 P0 故当前可分配!

您是否还要进行请求:y/n?

n

成功生成(总时间:1 分钟 38 秒)

㈧ java程序死锁问题,怎么解决

在 IBM Bluemix 云平台上开发并部署您的下一个应用。
开始您的试用
Java 语言通过 synchronized 关键字来保证原子性,这是因为每一个 Object 都有一个隐含的锁,这个也称作监视器对象。在进入 synchronized 之前自动获取此内部锁,而一旦离开此方式,无论是完成或者中断都会自动释放锁。显然这是一个独占锁,每个锁请求之间是互斥的。相对于众多高级锁 (Lock/ReadWriteLock 等),synchronized 的代价都比后者要高。但是 synchronzied 的语法比较简单,而且也比较容易使用和理解。Lock 一旦调用了 lock() 方法获取到锁而未正确释放的话很有可能造成死锁,所以 Lock 的释放操作总是跟在 finally 代码块里面,这在代码结构上也是一次调整和冗余。Lock 的实现已经将硬件资源用到了极致,所以未来可优化的空间不大,除非硬件有了更高的性能,但是 synchronized 只是规范的一种实现,这在不同的平台不同的硬件还有很高的提升空间,未来 Java 锁上的优化也会主要在这上面。既然 synchronzied 都不可能避免死锁产生,那么死锁情况会是经常容易出现的错误,下面具体描述死锁发生的原因及解决方法。
死锁描述
死锁是操作系统层面的一个错误,是进程死锁的简称,最早在 1965 年由 Dijkstra 在研究银行家算法时提出的,它是计算机操作系统乃至整个并发程序设计领域最难处理的问题之一。
事实上,计算机世界有很多事情需要多线程方式去解决,因为这样才能最大程度上利用资源,才能体现出计算的高效。但是,实际上来说,计算机系统中有很多一次只能由一个进程使用的资源的情况,例如打印机,同时只能有一个进程控制它。在多通道程序设计环境中,若干进程往往要共享这类资源,而且一个进程所需要的资源还很有可能不止一个。因此,就会出现若干进程竞争有限资源,又推进顺序不当,从而构成无限期循环等待的局面。我们称这种状态为死锁。简单一点描述,死锁是指多个进程循环等待它方占有的资源而无限期地僵持下去的局面。很显然,如果没有外力的作用,那么死锁涉及到的各个进程都将永远处于封锁状态。
系统发生死锁现象不仅浪费大量的系统资源,甚至导致整个系统崩溃,带来灾难性后果。所以,对于死锁问题在理论上和技术上都必须予以高度重视。
银行家算法
一个银行家如何将一定数目的资金安全地借给若干个客户,使这些客户既能借到钱完成要干的事,同时银行家又能收回全部资金而不至于破产。银行家就像一个操作系统,客户就像运行的进程,银行家的资金就是系统的资源。
银行家算法需要确保以下四点:
当一个顾客对资金的最大需求量不超过银行家现有的资金时就可接纳该顾客;
顾客可以分期贷款, 但贷款的总数不能超过最大需求量;
当银行家现有的资金不能满足顾客尚需的贷款数额时,对顾客的贷款可推迟支付,但总能使顾客在有限的时间里得到贷款;
当顾客得到所需的全部资金后,一定能在有限的时间里归还所有的资金。

㈨ 急需用Java能编通的银行家算法和多线程,并注释哈 帮帮忙大神们帮帮忙

这是我大三的课程设计题目唉。。不知道还能不能找到了。。

阅读全文

与java银行家算法相关的资料

热点内容
农行app怎么开网银 浏览:649
java迭代器遍历 浏览:301
闽政通无法请求服务器是什么 浏览:48
怎么做积木解压神器 浏览:203
王者荣耀解压玩具抽奖 浏览:49
12位是由啥加密的 浏览:868
程序员编迷你世界代码 浏览:895
php取现在时间 浏览:246
单片机高吸收 浏览:427
怎么区分五代头是不是加密喷头 浏览:244
hunt测试服务器是什么意思 浏览:510
2013程序员考试 浏览:641
毕业论文是pdf 浏览:736
服务器跑网心云划算吗 浏览:471
单片机定时器计数初值的计算公式 浏览:801
win7控制台命令 浏览:567
猫咪成年app怎么升级 浏览:692
360有没有加密软件 浏览:315
清除cisco交换机配置命令 浏览:751
华为删除交换机配置命令 浏览:473