類ThreadTest {
static int type = 4,num = 10;//定義資源和線程的數量。
static int[]resource = new int[type];//總系統資源
//static int[]copy resource = new int[type];//復制
靜態隨機rand = new Random();
靜態銀行[]銀行=新銀行[編號];//線程組
Bank temp = new Bank();
public void init() {
//初始化組中的每個線程,隨機填充系統資源總數。
for(int I = 0;我& lt類型;i++)
resource[I]= rand . nextint(10)+80;
system . out . print(" Resource:");
for(int I = 0;我& lt類型;i++)
system . out . print("+resource[I]);
system . out . println(" ");
for(int I = 0;我& lt銀行.長度;i++)
bank[i] =新銀行(" # "+I);
}
公共線程測試4() {
init();
}
類庫擴展線程{
//避免死鎖的銀行家算法
公共int[]
Max = new int[type],//總需求
Need = new int[type],//仍然需要的資源量。
allocation = new int[type];//分配數量
私有int[]
Request = new int[type],//請求的資源量。
copy resource = new int[type];//資源復制
private boolean isFinish = false//線程完成了嗎?
int[][]table = new int[bank . length][type * 4];//二維資源分配表
私有void init() {
//隨機填寫總數量、剩余數量、分配數量。
同步(資源){
for(int I = 0;我& lt類型;i++) {
max[I]= rand . nextint(5)+10;
need[I]= rand . nextint(10);
分配[i] =最大[i] -需要[I];
資源[i] -=分配[I];//從系統資源中減去分配的資源
}
printer();
for(int I = 0;我& lt類型;i++) {
if(resource[I]& lt;0) {
//如果存在分配的數量超過系統總資源的錯誤,則退出。
System.out.println("線程分配總和超出範圍!");
system . exit(1);
}
}
}
}
公共銀行(字符串){
集合名稱;
init();
start();
}
公共銀行(){
//無
}
公共無效運行(){
嘗試{
睡眠(rand . nextint(2000));
}
catch(中斷異常e) {
拋出新的runtime exception(e);
}
while(true) {
//在程序沒有完成的情況下繼續申請資源。
if(askFor() == false) {
嘗試{
睡眠(1000);
}
catch(中斷異常e) {
拋出新的runtime exception(e);
}
}
其他
try request();
if(noNeed() == true)
打破;
}
//睡眠壹段時間,模擬程序運行。
嘗試{
睡眠(1000);
}
catch(中斷異常e) {
拋出新的runtime exception(e);
}
system . out . println(getName()+" finish!");
同步(資源){
//運行後釋放占用的資源。
for(int I = 0;我& lt類型;i++) {
資源[i] +=分配[I];
need[I]= allocation[I]= max[I]= 0;
}
}
}
私有void打印機(){
//打印當前資源信息
system . out . print(getName()+" Max:");
for(int I = 0;我& lt類型;i++)
system . out . print("+max[I]);
system . out . print(" Allocation:");
for(int I = 0;我& lt類型;i++)
system . out . print("+allocation[I]);
system . out . print(" Need:");
for(int I = 0;我& lt類型;i++)
system . out . print("+need[I]);
system . out . print(" Available:");
for(int I = 0;我& lt類型;i++)
system . out . print("+resource[I]);
system . out . println(" ");
}
私有布爾askFor() {
//隨機生成申請的資源量,檢測是否超標。
布爾canAsk = false
for(int I = 0;我& lt類型;i++) {
request[I]= rand . nextint(20);
//防止應用程序超出所需的金額。
if(request[I]& gt;需要[我])
請求[i] =需要[I];
}
for(int I = 0;我& lt類型;I++) //防止隨機應用資源全部為0。
if(request[I]& gt;0)
canAsk = true
同步(資源){
//鎖定可用資源,檢查是否超標。
for(int I = 0;我& lt類型;i++) {
if(request[I]& gt;資源[i])
//如果應用的資源超過可用資源,請等待壹段時間,然後重新應用。
返回false
}
}
返回canAsk
}
私有void tryRequest() {
//創建副本並嘗試分配請求。
同步(資源){
for(int I = 0;我& lt類型;i++)
//還是要防止請求數量超出範圍。
if(request[I]& gt;資源[i])
返回;
for(int I = 0;我& lt類型;i++) {
//復制資源數量,減去需求數量得到壹個副本。
copy resource[I]= resource[I];
copy resource[I]-= request[I];
}
system . out . print(getName()+" ask for:");
for(int I = 0;我& lt類型;i++)
system . out . print("+request[I]);
system . out . println(" ");
if(checkSafe() == true) {
//如果檢查了安全性,則將復制值賦給資源數量,並修改占用數量和需求數量。
for(int I = 0;我& lt類型;i++) {
resource[I]= copy resource[I];
分配[i] +=請求[I];
need[I]-= request[I];
}
System.out.println(getName() +"請求成功!");
}
其他
System.out.println(getName() +"請求失敗!");
}
}
私有布爾checkSafe() {
//銀行家算法檢查安全性。
同步(銀行){
//將線程資源信息放入二維資源分配表中檢查安全性。0~type可用資源/type ~ type * 2所需資源/type * 2 ~ type * 3占用資源/type * 3 ~-1可用+占用資源。
for(int I = 0;我& lt銀行.長度;i++) {
for(int j = type;j & lt類型* 2;j++) {
表[i][j] =銀行[i]。需要[j % type];
}
for(int j = type * 2;j & lt類型* 3;j++) {
表[i][j] =銀行[i]。分配[j % type];
}
}
//冒泡排序按照需求資源從小到大排列。
for(int I = 0;我& lt銀行.長度;i++) {
for(int j = I;j & ltbank . length-1;j++) {
sort(j,4);
}
}
//此時進行安全檢查。
for(int I = 0;我& lt類型;i++) {
table[0][I]= copy resource[I];
表[0][I+類型*3] =表[0][i] +表[0][I+類型* 2];
if(table[0][I+type * 3]& lt;表[1][I+類型])
返回false
}
for(int j = 1;j & ltbank . length-1;j++) {
for(int k = 0;k & lt類型;k++) {
表[j][k] =表[j-1][k+type * 3];
表[j][k+type*3] =表[j][k] +表[j][k+type * 2];
if(table[j][k+type * 3]& lt;表[j+1][k+類型])
返回false
}
}
}
返回true
}
私有空排序(int j,int k) {
//遞歸冒泡排序
int tempNum
if(table[j][k]& gt;表[j+1][k]) {
for(int I = type;我& lt類型* 2;i++) {
tempNum = table[j][I];
表[j][i] =表[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]& amp;& ampk & ltType*2) //當這個資源量相同時,遞歸排序下壹個資源量,防止超出範圍。
sort(j,k+1);
}
私有布爾noNeed() {
//還需要資源嗎?
布爾完成=真;
for(int I = 0;我& lt類型;i++) {
如果(需要[我]!= 0) {
finish = false
打破;
}
}
返回完成;
}
}
公共靜態void main(String[] args) {
ThreadTest t = new ThreadTest();
//後臺線程,設置程序運行多長時間後自動結束。
新超時(30000,”-停止!!!- ");
}
}