資源簡介
資源有三種,線程數為5,適合各種課程設計作業和參考學習
代碼片段和文件信息
package?com.cch.entity;??
??
import?java.util.Scanner;??
??
public?class?Banker?{??
????static?int?available[]?=?new?int[3];?//?資源數??
????static?int?max[][]?=?new?int[5][3];?//?最大需求??
????static?int?allocation[][]?=?new?int[5][3];?//?分配??
????static?int?need[][]?=?new?int[5][3];?//?需求??
????static?int?request[]?=?new?int[3];?//?存放請求??
????Scanner?scanner?=?new?Scanner(System.in);??
????int?thread;?//?線程號??
??
????//?初始化??
????public?void?getData()?{??
????????System.out.println(“請輸入ABC三類資源的數目:“);??
????????//?輸入ABC三類資源數量??
????????for?(int?i?=?0;?i?3;?i++)?{??
????????????available[i]?=?scanner.nextInt();??
????????}??
????????//?輸入進程對三類資源的最大需求??
????????for?(int?i?=?0;?i?5;?i++)?{??
????????????System.out.println(“請輸入進程“?+?i?+?“對ABC三類資源的最大需求“);??
????????????for?(int?j?=?0;?j?3;?j++)?{??
????????????????max[i][j]?=?scanner.nextInt();??
????????????}??
????????}??
????????//?輸入進程分配的三類資源數??
????????for?(int?i?=?0;?i?5;?i++)?{??
????????????System.out.println(“請輸入進程“?+?i?+?“已分配的ABC三類資源數“);??
????????????for?(int?j?=?0;?j?3;?j++)?{??
????????????????allocation[i][j]?=?scanner.nextInt();??
????????????}??
????????}??
????????//?計算進程還需要的三類資源數??
????????for?(int?i?=?0;?i?5;?i++)?{??
????????????for?(int?j?=?0;?j?3;?j++)?{??
????????????????need[i][j]?=?max[i][j]?-?allocation[i][j];??
????????????}??
????????}??
????????//?重新計算available??
????????for?(int?i?=?0;?i?3;?i++)?{??
????????????for?(int?j?=?0;?j?5;?j++)?{??
????????????????available[i]?-=?allocation[j][i];??
????????????}??
????????}??
????}??
??
????//?用戶輸入要申請資源的線程和申請的資源,并進行判斷??
????public?void?getThread()?{??
????????System.out.println(“請輸入申請資源的線程“);??
????????int?thread?=?scanner.nextInt();?//?線程??
????????if?(thread?0?||?thread?>?4)?{??
????????????System.out.println(“該線程不存在請重新輸入“);??
????????????getThread();??
????????}?else?{??
????????????this.thread?=?thread;??
????????????System.out.println(“請輸入申請的資源(三種,若某種資源不申請則填0)“);??
????????????for?(int?i?=?0;?i?3;?i++)?{??
????????????????request[i]?=?scanner.nextInt();??
????????????}??
????????????if?(request[0]?>?need[thread][0]?||?request[1]?>?need[thread][1]?||?request[2]?>?need[thread][2])?{??
????????????????System.out.println(thread?+?“線程申請的資源超出其需要的資源,請重新輸入“);??
????????????????getThread();??
????????????}?else?{??
????????????????if?(request[0]?>?available[0]?||?request[1]?>?available[1]?||?request[2]?>?available[2])?{??
????????????????????System.out.println(thread?+?“線程申請的資源大于系統資源,請重新輸入“);??
????????????????????getThread();??
????????????????}??
????????????}??
????????????changeData(thread);??
????????????if?(check(thread))?{??
????????????????getThread();??
????????????}?else?{??
????????????????recoverData(thread);??
????????????????getThread();??
????????????}??
??
????????}??
????}??
??
????//?thread線程請求響應后,試探性分配資源??
????public?
評論
共有 條評論