资源简介
使用管程方式模拟哲学家就问题。
用一个输入变量控制是否有左撇子哲学家,如果有,其数量由随机数生成;
模拟程序分为两种情况:
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
评论
共有 条评论