java實(shí)現(xiàn)銀行家算法(Swing界面)
java代碼實(shí)現(xiàn)了銀行家算法,界面寫的個人認(rèn)為還是較為細(xì)致的,完整的實(shí)現(xiàn)了找安全序列等算法功能,可作為參考學(xué)習(xí)銀行家算法。
直接上代碼:①界面展示方法:
public void ShowFrame() { this.setSize(500, 350); //大小 this.setAlwaysOnTop(true); this.setResizable(false);//不可拖動 this.setLayout(new BorderLayout()); this.setTitle("lly_banktest"); jp1=new JPanel(); String s[]= {"Allocation","Max","Available","Request"}; jcb1=new JComboBox(s); jp1.add(jcb1); jp1.add(new JLabel("PID:")); jtf1=new JTextField(3); jp1.add(jtf1); jp1.add(new JLabel("A:")); jtf2=new JTextField(3); jp1.add(jtf2); jp1.add(new JLabel("B:")); jtf3=new JTextField(3); jp1.add(jtf3); jp1.add(new JLabel("C:")); jtf4=new JTextField(3); jp1.add(jtf4); jb1=new JButton("確定"); jp1.add(jb1); jb1.addActionListener(this); this.add(jp1,"South"); jta1= new JTextArea(); //顯示文件本域 ShowData(); //顯示數(shù)據(jù) jta1.setLineWrap(true); //自動適應(yīng) int r,g,b; jta1.setBackground(Color.white); jta1.setEditable(false); this.add(jta1,"Center"); // Font f=new Font("Dialog",Font.BOLD,12); // jp1.setBackground(new java.awt.Color(128,255,128)); } public void ShowData(){ jta1.setText(" Max \tAllocation Need \tAvailable\n"); jta1.append("\n"+" 資源: " + " A B C " +" A B C " + " A B C " +" A B C"); jta1.append("\n 進(jìn)程\n "+pname[0]+" " + +Max[0][0]+" "+Max[0][1]+" " +Max[0][2]+" " + " "+Allocation[0][0]+" "+Allocation[0][1] +" "+Allocation[0][2]+" " + " "+Need[0][0]+" "+Need[0][1] +" "+Need[0][2]+" " + " "+Available[0]+" "+Available[1]+ " "+Available[2]); for(int i=1;i<5;i++) { jta1.append("\n\n "+pname[i]+" " + " "+Max[i][0]+" "+Max[i][1]+" "+Max[i][2]+" " + " "+Allocation[i][0]+" "+Allocation[i][1] +" "+Allocation[i][2]+" " + " "+Need[i][0]+" "+Need[i][1] +" "+Need[i][2]+" " ); } jtf1.setText(""); jtf2.setText(""); jtf3.setText(""); jtf4.setText(""); }
截圖:
②算法實(shí)現(xiàn)代碼:
public void myAllocation(int i) //分配資源 { for (int j=0;j<sno;j++) { Available[j]=Available[j]-Request[j]; Allocation[i][j]=Allocation[i][j]+Request[j]; Need[i][j]=Need[i][j]-Request[j]; } } public boolean judge(int i, int Request[] ) //初步檢查是否有足夠資源 { boolean choice=false; for(int j=0;j<sno;j++){ if(Request[j]>Need[i][j]) break; if(Request[j]>Available[j]) break; else choice=true; } return choice; } public boolean SaftyCheck(int p) //安全性檢查 { int k = 0; boolean b=true; Work=new int[sno]; //定義工作向量并賦初值 Finish=new boolean[pno]; for(int i=0;i<sno;i++) { Work[i]=Available[i]; } for(int i=0;i<pno;i++) Finish[i]=false; //初值為false Finish[p]=true; //初次檢查 for(int j=0;j<sno;j++)//釋放資源 { Work[j]=Work[j]+Allocation[p][j]; } temp[k++]=p; boolean found = false;//標(biāo)記是否找到安全進(jìn)程 while(k<pno-1){ //遍歷查找安全序列 for(int i=0;i<pno;i++) { boolean flag=true;//標(biāo)記是否有足夠資源 if(Finish[i]) continue ; for(int j=0;j<sno;j++) { if(Need[i][j]>Work[j]) //資源不足,退出 { flag=false; break; } } if(flag) //找到資源 { temp[k++]=i;//存儲安全序列 Finish[i]=true; found=true; for(int j=0;j<sno;j++)//釋放資源 Work[j]=Work[j]+Allocation[i][j]; } } if(found) { found=false; } else break;//遍歷,試分配失敗跳出 } for(int i=0;i<pno;i++){ //若存在false,則跳出 if(!Finish[i]) { b=false; break; } } return b; }
截圖:
③事件響應(yīng)函數(shù):
public void actionPerformed(ActionEvent e) { //事件響應(yīng)函數(shù) if(e.getSource()==jb1){//按下“確定” if(jcb1.getSelectedItem()=="Request"){ int p=0; try{ p=Integer.parseInt(jtf1.getText()); Request[0]=Integer.parseInt(jtf2.getText()); Request[1]=Integer.parseInt(jtf3.getText()); Request[2]=Integer.parseInt(jtf4.getText()); }catch(Exception d) { JOptionPane.showMessageDialog(this, "您輸入有誤!請重新輸入!"); ShowData(); return; } if(p>4) //限定輸入進(jìn)程ID范圍 { JOptionPane.showMessageDialog(this, "PID在0-4之間!"); jtf1.setText(""); return; } if(judge(p,Request))//初步分配檢查 { if(SaftyCheck(p)){//安全性檢查 ShowData(); jta1.append("\n\n 通過安全性檢查!安全序列為:"); for(int i=0;i<pno;i++)//打印安全序列 jta1.append("P"+String.valueOf(temp[i])+" "); jta1.append(" 批準(zhǔn)請求,資源已分配!"); myAllocation(p);//檢查到安全序列才分配 }else //不安全 { ShowData(); //jta1.append("\n\n 找不到安全序列! 不批準(zhǔn)請求!"); JOptionPane.showMessageDialog(this, "找不到安全序列! 不批準(zhǔn)請求!"); } }else{ ShowData(); jta1.append("\n\n 系統(tǒng)資源不足!"); } } /*** * 選擇avaliable時! */ else if(jcb1.getSelectedItem()=="Available"){//設(shè)置可用資源 try{ Available[0]= Integer.parseInt(jtf2.getText()); Available[1]=Integer.parseInt(jtf3.getText()); Available[2]=Integer.parseInt(jtf4.getText()); }catch(Exception d) { JOptionPane.showMessageDialog(this, "您輸入有誤!請重新輸入!"); ShowData(); return; } ShowData(); jta1.append("\n\n 可用資源設(shè)置成功!"); } /** * 選擇Max時! */ else if(jcb1.getSelectedItem()=="Max"){//設(shè)置最大需求源 int p = 0; try{ p =Integer.parseInt(jtf1.getText()); if(p>4) { JOptionPane.showMessageDialog(this, "進(jìn)程ID在0-4之間!"); jtf1.setText(""); return; } int Maxago[][]= new int[pno][sno]; //暫存最大需求 Max[p][0]=Integer.parseInt(jtf2.getText()); Max[p][1]=Integer.parseInt(jtf3.getText()); Max[p][2]=Integer.parseInt(jtf4.getText()); for(int j=0;j<sno;j++){ //判斷設(shè)置是否合理 int temp =Max[p][j]-Allocation[p][j]; if(temp>=0) Need[p][j]=temp; else { JOptionPane.showMessageDialog(this, "最大需求過小!請重新輸入!"); Max=Maxago; ShowData(); return; } } }catch(Exception d) { JOptionPane.showMessageDialog(this, "輸入有誤!請重新輸入!"); ShowData(); return; } ShowData(); jta1.append("\n\n 最大需求設(shè)置成功!"); } /** * 選擇Allocation時! */ else if(jcb1.getSelectedItem()=="Allocation"){//設(shè)置已分配資源 int p = 0; try{ p =Integer.parseInt(jtf1.getText()); if(p>4) { JOptionPane.showMessageDialog(this, "進(jìn)程ID在0-4之間!"); jtf1.setText(""); return; } Allocation[p][0]=Integer.parseInt(jtf2.getText()); Allocation[p][1]=Integer.parseInt(jtf3.getText()); Allocation[p][2]=Integer.parseInt(jtf4.getText());} catch(Exception d) { JOptionPane.showMessageDialog(this, "輸入有誤!請重新輸入!"); ShowData(); return; } ShowData(); jta1.append("\n\n 已分配資源設(shè)置成功!"); } }
截圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:淺談java字符串比較到底應(yīng)該用==還是equals
欄 目:Java
下一篇:java代碼實(shí)現(xiàn)銀行管理系統(tǒng)
本文標(biāo)題:java實(shí)現(xiàn)銀行家算法(Swing界面)
本文地址:http://mengdiqiu.com.cn/a1/Java/8850.html
您可能感興趣的文章
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時鐘
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10淺談Java中真的只有值傳遞么
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題


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