java實現(xiàn)銀行家算法
本文實例為大家分享了java實現(xiàn)銀行家算法的具體代碼,供大家參考,具體內(nèi)容如下
import java.util.Arrays; import javax.swing.JOptionPane; public class Banker_Dijkstra { static int available[]={3,3,2}; //可利用資源數(shù) static int max[][]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};; //每線程最大需求 static int allocation[][]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}}; //已分配資源 static int need[][]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}}; //需求 static int request[]=new int[3]; //存放請求資源 static int thread; //線程號 static JOptionPane jpane = new JOptionPane(); // static boolean m; public static void main(String[] argv){ int n = 0 ; Banker_Dijkstra bd = new Banker_Dijkstra(); for(int i=0;i<5;i++){ if(bd.safeState(i)){ JOptionPane.showMessageDialog(jpane, "系統(tǒng)狀態(tài)安全"); n=1; break; }else{ n=2; continue; } } if(n==1){ bd.getThread(); } else if(n==2){ JOptionPane.showMessageDialog(jpane, "系統(tǒng)狀態(tài)不安全"); } } protected void getThread(){//輸入測試線程號且輸出結(jié)果 try{ String xiancheng = JOptionPane.showInputDialog(jpane,"請輸入申請資源的線程:"); thread = (int) Integer.parseInt(xiancheng); }catch(Exception e){ int response = JOptionPane.showConfirmDialog(jpane, "請輸入0~4:",null, JOptionPane.YES_NO_OPTION); // 處理異常 if(response==0){ getThread(); }else if(response ==1){ System.exit(0); } } if(thread<0||thread>4){ JOptionPane.showMessageDialog(jpane, "請輸入0~4:"); getThread(); }else{ for(int i=0;i<3;i++){ String requestR = JOptionPane.showInputDialog(jpane,"請輸入申請的第"+(i+1)+"種資源(若不申請則填0)"); try{ request[i]=Integer.parseInt(requestR);} catch(Exception e){ JOptionPane.showConfirmDialog(jpane, "請輸入申請的第"+(i+1)+"種資源(若不申請則填0)",null,JOptionPane.YES_NO_OPTION); } } if(request[0]>need[thread][0]||request[1]>need[thread][1]||request[2]>need[thread][2]){ JOptionPane.showMessageDialog(jpane,thread+"線程申請的資源超出其需要的資源,請重新輸入"); getThread(); }else{ if(request[0]> available[0]||request[1]> available[1]||request[2]> available[2]){ JOptionPane.showMessageDialog(jpane,thread+"線程申請的資源大于系統(tǒng)資源,請重新輸入"); getThread(); } } // 分配資源 allocateData(thread); // 判斷 繼續(xù)模擬選擇與處理 int tag=0; if(check(thread)){ try{ String str = JOptionPane.showInputDialog(jpane,"是/否 繼續(xù)模擬?( 1/0 ):"); tag = Integer.parseInt(str); }catch(Exception e){ JOptionPane.showMessageDialog(jpane, "繼續(xù) 輸入(數(shù)值) 1,不繼續(xù) 輸入(數(shù)值) 0 !"); } if(tag==1){ recoverData(thread); getThread(); } else{ if( (JOptionPane.YES_NO_OPTION)==JOptionPane.CANCEL_OPTION)System.exit(0); if((JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) recoverData(thread); } }else{ recoverData(thread); getThread(); } } } // 安全算法 private boolean check(int thread2) { boolean[] finish = new boolean[5]; Arrays.fill(finish, false); int[] work = new int[3]; int[] queue = new int[5]; int q=0;//安全序列下標 for(int i = 0;i<3;i++){ work[i] = available[i]; } int tT = thread2; while(tT<5){ for(int R=0;R<3;R++){ if((!(finish[tT]==false))||(!(work[R]>=need[tT][R]))){ tT++; break; }else{ if(R==2){ for(int m =0;m<3;m++){ work[m] += allocation[tT][m]; } for(int s:work){ System.out.print(s+" "); } System.out.println(""); finish[tT] = true; queue[q] = tT; q++; tT =0; } } } } for(int p =0;p<5;p++){ if(finish[p]==false){ JOptionPane.showMessageDialog(jpane, "安全序列生成失敗"); return false; } } JOptionPane.showMessageDialog(jpane, "安全序列:"+queue[0]+","+queue[1]+"," +queue[2]+","+queue[3]+","+queue[4]); return true; } private boolean safeState(int thread3){ boolean[] finish = new boolean[5]; Arrays.fill(finish, false); int[] work = new int[3]; int[] queue = new int[5]; int q=0;//安全序列下標 for(int i = 0;i<3;i++){ work[i] = available[i]; } int tT = thread3; while(tT<5){ for(int R=0;R<3;R++){ if((!(finish[tT]==false))||(!(work[R]>=need[tT][R]))){ tT++; break; } else{ if(R==2){ for(int m =0;m<3;m++){ work[m] += allocation[tT][m]; } finish[tT] = true; queue[q] = tT; q++; tT =0; } } // if((finish[tT]==false)&&(work[R]>=need[tT][R])){ // for(int m =0;m<3;m++){ // work[m] += allocation[tT][m];} // finish[tT] = true; // queue[q] = tT; // q++; // tT=0; // }else{ // tT++; // break; // } } } for(int p =0;p<5;p++){ if(finish[p]==false){ return false; } } return true; } private void recoverData(int thread2) { // 生成失敗則重置已分配資源 for(int i=0;i<3;i++){ //重新調(diào)整系統(tǒng)資源數(shù) available[i]+=request[i]; //計算各個線程擁有資源 allocation[thread2][i]-=request[i]; //重新計算需求 need[thread2][i]+=request[i]; } } private void allocateData(int thread2) { //分配 for(int i=0;i<3;i++){ //重新調(diào)整可用系統(tǒng)資源數(shù) available[i]-=request[i]; //計算各個線程分配后擁有資源 allocation[thread2][i]+=request[i]; //重新計算需求 need[thread2][i]-=request[i]; } } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:java中堆內(nèi)存與棧內(nèi)存的知識點總結(jié)
欄 目:Java
下一篇:springboot日期轉(zhuǎn)換器實現(xiàn)實例解析
本文標題:java實現(xiàn)銀行家算法
本文地址:http://mengdiqiu.com.cn/a1/Java/8853.html
您可能感興趣的文章
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10利用Java實現(xiàn)復制Excel工作表功能
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導出excel透視表代碼實例
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10基于Java驗證jwt token代碼實例
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當前時間
- 01-10淺談Java中真的只有值傳遞么
- 01-10Java動態(tài)顯示當前日期和時間
- 01-10如何解決線程太多導致java socket連接池出現(xiàn)的問題


閱讀排行
本欄相關
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10利用Java實現(xiàn)復制Excel工作表功能
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導出excel透視表代碼實例
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當前時間
- 01-10基于Java驗證jwt token代碼實例
- 01-10Java動態(tài)顯示當前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什