java怎么實(shí)現(xiàn)銀行家算法-創(chuàng)新互聯(lián)

本篇內(nèi)容主要講解“java怎么實(shí)現(xiàn)銀行家算法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“java怎么實(shí)現(xiàn)銀行家算法”吧!

10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有朗縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

本文實(shí)例為大家分享了java實(shí)現(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];   //存放請(qǐng)求資源  static int thread; //線程號(hào)  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(){//輸入測(cè)試線程號(hào)且輸出結(jié)果  try{  String xiancheng = JOptionPane.showInputDialog(jpane,"請(qǐng)輸入申請(qǐng)資源的線程:");  thread = (int) Integer.parseInt(xiancheng);  }catch(Exception e){   int response = JOptionPane.showConfirmDialog(jpane, "請(qǐng)輸入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, "請(qǐng)輸入0~4:");    getThread();   }else{    for(int i=0;i<3;i++){     String requestR = JOptionPane.showInputDialog(jpane,"請(qǐng)輸入申請(qǐng)的第"+(i+1)+"種資源(若不申請(qǐng)則填0)");    try{    request[i]=Integer.parseInt(requestR);}    catch(Exception e){     JOptionPane.showConfirmDialog(jpane, "請(qǐng)輸入申請(qǐng)的第"+(i+1)+"種資源(若不申請(qǐng)則填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+"線程申請(qǐng)的資源超出其需要的資源,請(qǐng)重新輸入");     getThread();    }else{     if(request[0]> available[0]||request[1]> available[1]||request[2]> available[2]){     JOptionPane.showMessageDialog(jpane,thread+"線程申請(qǐng)的資源大于系統(tǒng)資源,請(qǐ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;//安全序列下標(biāo)  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;//安全序列下標(biāo)   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];    //計(jì)算各個(gè)線程擁有資源    allocation[thread2][i]-=request[i];    //重新計(jì)算需求    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];    //計(jì)算各個(gè)線程分配后擁有資源    allocation[thread2][i]+=request[i];    //重新計(jì)算需求    need[thread2][i]-=request[i];   }   }}

到此,相信大家對(duì)“java怎么實(shí)現(xiàn)銀行家算法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)站欄目:java怎么實(shí)現(xiàn)銀行家算法-創(chuàng)新互聯(lián)
瀏覽路徑:http://bm7419.com/article46/dgoghg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、全網(wǎng)營(yíng)銷推廣響應(yīng)式網(wǎng)站、網(wǎng)站營(yíng)銷微信公眾號(hào)、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化