資源簡介
哲學家就餐:Java多線程實例圖形版
用圖形用戶界面表示哲學家就餐問題

代碼片段和文件信息
import?java.awt.*;???????????????????
import?java.awt.event.*;?
import?javax.swing.*;?
import?javax.swing.ImageIcon;
//////////////////////////////////////////////////
//筷子類?
class?Bamboo?{???????????????????
???boolean?bState;???????????????//互斥信號量
???JLabel?bLabel;???????????????????????//顯示筷子的JLabel
???ImageIcon?emptyImagebambooImage;????????????????//被用時為empty圖片,否則為筷子圖片???
???
???public?Bamboo(JLabel?bLabel?ImageIcon?image)?{??????
???????emptyImage=new?ImageIcon(“空的.jpg“);?
???????this.bLabel=bLabel;???
???????this.bambooImage=image;?
???????bState=true;????????????????????//一開始都設筷子為可用狀態
???}?
???
???public?synchronized?void?gainBamboo()?{???????????
???????while(!bState)?{??????????????????//如果此筷子已被用,則等待????
???????????try?{?
???????????????wait();?
???????????}catch(Exception?e)?{?
???????? ???e.printStackTrace();
???????????}?
???????}?
???????bLabel.setIcon(emptyImage);?????????//獲得筷子,并把筷子所代表?的圖片設為空??????
???????bState=false;??????????????????????//把此筷子的狀態設為已被用了????????
???}?
???
???public?synchronized?void?takeBamboo()?{??????????//放下筷子,把筷子狀態設為可用
???????bState=true;???????????????????
???????bLabel.setIcon(bambooImage);??????????????????
???????notify();?????????????????????????????//喚醒其他在等待的線程,如果有等待此筷子的線程,則可以繼續判斷
???}?
}?
?
//////////////////////////////////////////////////////////
//哲學家類,
class?Zexuejia?extends?Thread?{???????
int?thinkSpeed?eatSpeed;?//?思考,吃飯的時間
Bamboo?left?right;?//?哲學家的左右筷子
int?num;?//?第幾號哲學家
JLabel?zxjLable?leftLable?rightLable?zxjLeftLabel?zxjRightLabel;??//?zxjLable為哲學家設置狀態的,
?????????????????????????????????????????????????????????????????????????//?leftLablerightLable為哲學家的左右兩根,
????????????????????????????????????????????????????????????????????//?zxjLeftLabelzxjRightLabel為哲學家拿起兩筷子后的
ImageIcon?leftImage?rightImage;?????//左右兩個筷子的圖片
Zexuejia(int?num?JLabel?zLabel?Bamboo?left?JLabel?lLabel
Bamboo?right?//?哲學家構造函數
JLabel?rLabel?ImageIcon?lImage?ImageIcon?rImage?JLabel?zLLabel
JLabel?zRLabel)?{
this.num?=?num;
this.zxjLable?=?zLabel;
this.left?=?left;
this.leftLable?=?lLabel;
this.right?=?right;
this.rightLable?=?rLabel;
this.leftImage?=?lImage;
this.rightImage?=?rImage;
this.zxjRightLabel?=?zRLabel;
this.zxjLeftLabel?=?zLLabel;
}?
???public?void?run()?{
ImageIcon?thinkImage?=?new?ImageIcon(“thinking.gif“);
ImageIcon?huangryImage?=?new?ImageIcon(“huangry.gif“);
ImageIcon?eatImage?=?new?ImageIcon(“eating.gif“);
ImageIcon?empty?=?new?ImageIcon(“empty.jpg“);
while?(true)?{
zxjLable.setIcon(thinkImage);
do?{
thinkSpeed?=?(int)?(Math.random()?*?10000);?//?獲得隨機的思考時間
}?while?(thinkSpeed?1500);
try?{
sleep(thinkSpeed);?//?以線程睡眠的方式讓哲學家思考一些時間
}?catch?(Exception?e)?{
}
zxjLable.setIcon(huangryImage);
left.gainBamboo();?//?獲得左邊的筷子
zxjLeftLabel.setIcon(leftImage);
right.gainBamboo();?//?獲得右邊的筷子
zxjRightLabel.s
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????10732??2007-12-14?14:51??哲學家就餐:Java多線程實例圖形版\bamboo.gif
?????文件??????51967??2008-01-16?15:36??哲學家就餐:Java多線程實例圖形版\eating.gif
?????文件??????52110??2008-01-16?15:36??哲學家就餐:Java多線程實例圖形版\huangry.gif
?????文件???????5411??2008-01-16?15:24??哲學家就餐:Java多線程實例圖形版\thinking.gif
?????文件???????9419??2008-07-05?09:23??哲學家就餐:Java多線程實例圖形版\Zxjfr
?????目錄??????????0??2008-10-31?10:11??哲學家就餐:Java多線程實例圖形版
-----------?---------??----------?-----??----
???????????????129639????????????????????6
- 上一篇:Java實現一個簡單的數據庫系統
- 下一篇:java實現p2p通信
評論
共有 條評論