資源簡介
使用管程方式模擬哲學家就問題。
用一個輸入變量控制是否有左撇子哲學家,如果有,其數量由隨機數生成;
模擬程序分為兩種情況:
1.可能發生死鎖的情況;
2.沒有死鎖發生的情況。
可輸出資源分配給哲學家的當前狀態和歷史狀態;
具備圖形界面,動態顯示哲學家就餐的過程O(∩_∩)O~

代碼片段和文件信息
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.util.Scanner;
import?javax.swing.*;
class?Monitor?{
public?int?philStates[]?=?new?int[5];?//?0=thinking1=hungry2=eating
public?boolean?fork[]?=?new?boolean[5];?//?true=usefalse=free
public?DinersPhilosopher?dp;?//?change?the?icons
public?Monitor(DinersPhilosopher?dp)?{
this.dp?=?dp;
int?i;
for?(i?=?0;?i?5;?i++)?{
philStates[i]?=?0;
fork[i]?=?false;
}
}
public?synchronized?void?get_left_fork(int?i)?{
if?(fork[i])?{
philStates[i]?=?1;
dp.jl[i].setIcon(dp.pic[philStates[i]]);
System.out.println(“【P“?+?i?+?“】waiting?for?【left_fork“?+?i
+?“】...hungry?now...“);
}
while?(fork[i])?{
try?{
wait();
}?catch?(InterruptedException?e)?{
}
}
fork[i]?=?true;
if?(!dp.lh[i])?{
philStates[i]?=?2;
dp.jl[i].setIcon(dp.pic[philStates[i]]);
}
System.out.println(“【P“?+?i?+?“】get?【left_fork“?+?i?+?“】“);
}
public?synchronized?void?get_right_fork(int?i)?{
if?(fork[(i?+?1)?%?5])?{
philStates[i]?=?1;
dp.jl[i].setIcon(dp.pic[philStates[i]]);
System.out.println(“【P“?+?i?+?“】waiting?for?【right_fork“?+?(i?+?1)
+?“】...hungry?now...“);
}
while?(fork[(i?+?1)?%?5])?{
try?{
wait();
}?catch?(InterruptedException?e)?{
}
}
fork[(i?+?1)?%?5]?=?true;
if?(dp.lh[i])?{
philStates[i]?=?2;
dp.jl[i].setIcon(dp.pic[philStates[i]]);
}
System.out.println(“【P“?+?i?+?“】get?【right_fork“?+?(i?+?1)?+?“】“);
}
public?synchronized?void?get_forks(int?i)?{
if?(fork[i]?||?fork[(i?+?1)?%?5])?{
System.out.println(“【P“?+?i?+?“】waiting?for?【forks“?+?i?+?“、“
+?(i?+?1)?+?“】...hungry?now...“);
}
while?(fork[i]?||?fork[(i?+?1)?%?5])?{
philStates[i]?=?1;//?hungry
dp.jl[i].setIcon(dp.pic[philStates[i]]);
try?{
wait();
}?catch?(InterruptedException?e)?{
}
}
fork[i]?=?true;
fork[(i?+?1)?%?5]?=?true;
philStates[i]?=?2;//?eating
dp.jl[i].setIcon(dp.pic[philStates[i]]);
System.out.println(“【P“?+?i?+?“】get?【forks“?+?i?+?“、“?+?(i?+?1)
+?“】...eating?now...“);
}
public?synchronized?void?release_forks(int?i)?{
fork[i]?=?false;
fork[(i?+?1)?%?5]?=?false;
philStates[i]?=?0;//?thinking
dp.jl[i].setIcon(dp.pic[philStates[i]]);
System.out.println(“【P“?+?i?+?“】release?【forks“?+?i?+?“、“?+?(i?+?1)
+?“】...thinking?now...“);
notify();
}
}
public?class?DinersPhilosopher?extends?Jframe?{
public?JLabel?jl[]?=?new?JLabel[5];
public?String[]?name?=?{?“philosopher1“?“philosopher2“?“philosopher3“
“philosopher4“?“philosopher5“?};
public?ImageIcon?pic[]?=?new?ImageIcon[3];
public?Monitor?m;
public?philosopher?phil[];
public?boolean?lh[]?=?new?boolean[5];
public?DinersPhilosopher()?{
pic[0]?=?new?ImageIcon(getClass().getResource(“think.png“));
pic[1]?=?new?ImageIcon(getClass().getResource(“hungry.png“));
pic[2]?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-11-24?15:56??DinersPhilosopher\
?????文件?????????301??2013-11-24?15:56??DinersPhilosopher\.classpath
?????文件?????????393??2013-11-24?15:56??DinersPhilosopher\.project
?????目錄???????????0??2013-11-24?15:56??DinersPhilosopher\.settings\
?????文件?????????598??2013-11-24?15:56??DinersPhilosopher\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2013-11-24?19:54??DinersPhilosopher\bin\
?????文件?????????789??2013-11-26?20:43??DinersPhilosopher\bin\DinersPhilosopher$1.class
?????文件?????????793??2013-11-26?20:43??DinersPhilosopher\bin\DinersPhilosopher$2.class
?????文件????????4097??2013-11-26?20:43??DinersPhilosopher\bin\DinersPhilosopher.class
?????文件????????2774??2013-11-26?20:43??DinersPhilosopher\bin\Monitor.class
?????文件???????16001??2013-11-24?16:35??DinersPhilosopher\bin\eat.png
?????文件???????19328??2013-11-24?16:35??DinersPhilosopher\bin\hungry.png
?????文件????????2080??2013-11-26?20:43??DinersPhilosopher\bin\philosopher.class
?????文件???????16264??2013-11-24?16:35??DinersPhilosopher\bin\think.png
?????目錄???????????0??2013-11-24?16:35??DinersPhilosopher\src\
?????文件????????7504??2013-11-26?20:43??DinersPhilosopher\src\DinersPhilosopher.java
?????文件???????16001??2013-11-24?16:35??DinersPhilosopher\src\eat.png
?????文件???????19328??2013-11-24?16:35??DinersPhilosopher\src\hungry.png
?????文件???????16264??2013-11-24?16:35??DinersPhilosopher\src\think.png
評論
共有 條評論