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能編通的銀行家演算法和多線程,並注釋哈 幫幫忙大神們幫幫忙
這是我大三的課程設計題目唉。。不知道還能不能找到了。。