有人能把哲学家吃饭问题,改为哲学家喝酒问题吗?

duanran 2004-05-06 04:40:36
吃饭问题原码如下

我吃是想把fork改成瓶子,但是不同是瓶子有一个就可以喝,fork要有两个才可以吃



import java.awt.*;
import java.applet.*;

public class Diners extends Applet {

PhilCanvas display;
Thread[] phil= new Thread[PhilCanvas.NUMPHILS];
Fork[] fork = new Fork[PhilCanvas.NUMPHILS];
Scrollbar slider;
Button restart;
Button freeze;
boolean fixed = false;

public void init() {
setLayout(new BorderLayout());
String s = getParameter("Version");
fixed = s.equals("FIXED");
display = new PhilCanvas(this);
display.setSize(300,320);
add("Center",display);
slider = new Scrollbar(Scrollbar.HORIZONTAL, 50, 5, 0, 100);
restart = new Button("Restart");
freeze = new Button("Freeze");
Panel p1 = new Panel();
p1.setLayout(new BorderLayout());
p1.add("Center",slider);
p1.add("East",restart);
p1.add("West",freeze);
add("South",p1);
}

Thread makePhilosopher(Diners d, int id, Fork left, Fork right) {
if (fixed)
return new FixedPhilosopher(d,id,left,right);
else
return new Philosopher(d,id,left,right);
}

public int sleepTime() {
return (slider.getValue()*(int)(100*Math.random()));
}

public int eatTime() {
return (slider.getValue()*(int)(50*Math.random()));
}

public void start() {
for (int i =0; i<display.NUMPHILS; ++i)
fork[i] = new Fork(display,i);
for (int i =0; i<display.NUMPHILS; ++i){
phil[i] = makePhilosopher(this,i,
fork[(i-1+display.NUMPHILS)% display.NUMPHILS],
fork[i]);
phil[i].start();
}
}

public void stop() {
for (int i =0; i<display.NUMPHILS; ++i) {
phil[i].interrupt();
phil[i].stop(); //required for java 1.1
}
}

public boolean handleEvent(Event event) {
if (event.id != event.ACTION_EVENT) {
return super.handleEvent(event);
} else if(event.target==restart) {
if (display.deadlocked()) {
stop();
slider.setValue(50);
start();
}
display.thaw();
return true;
} else if (event.target == freeze) {
display.freeze();
return true;
} else
return super.handleEvent(event);
}
}
...全文
57 9 点赞 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dlxu 2004-05-07
不都差不多阿
  • 打赏
  • 举报
回复
ai92 2004-05-07
喝酒问题有什么意义吗??
没有,这个不会有什么意思
规定每个哲学家只要拿起他左边或右边的酒就开始喝,这样最多就一个人不能喝
就等待
一点也不难解决阿
你能不能说的明白点
  • 打赏
  • 举报
回复
bbblllyyy 2004-05-07
20分让大家帮你看那么长的程序..估计很难办...
  • 打赏
  • 举报
回复
holy_phoenix 2004-05-07
应该自己想问题,不要把forum当作分析、设计、实现、维护程序一条龙服务的地方,只有你自己深入理解自己要干什么以后,再来forum咨询你实在无法弄明白的疑难点!
  • 打赏
  • 举报
回复
duanran 2004-05-06
这是我从网上整的程序,,
看明白一点
你要我怎么写注释呀

这个就是很普遍的concurrent 多线程的问题
  • 打赏
  • 举报
回复
BigBangBug 2004-05-06
没有注释的程序没人会帮你的!就算你自己,等过了3个月,你也不知道这程序是怎么运行的
  • 打赏
  • 举报
回复
duanran 2004-05-06
又帮忙的吗?
  • 打赏
  • 举报
回复
duanran 2004-05-06


import java.awt.*;

class Philosopher extends Thread {

int identity;
boolean stopRequested = false;
PhilCanvas view;
Diners controller;
Fork left;
Fork right;

Philosopher(Diners controller, int identity, Fork left, Fork right) {
this.controller = controller;
this.view = controller.display;
this.identity = identity;
this.left = left;
this.right = right;
}

public void run() {
while (!stopRequested) {
try {
//thinking
view.setPhil(identity,view.THINKING);
sleep(controller.sleepTime());
//hungry
view.setPhil(identity,view.HUNGRY);
//get forks
if (identity%2 == 0) {
left.get();
view.setPhil(identity,view.GOTLEFT);
} else {
right.get();
view.setPhil(identity,view.GOTRIGHT);
}
sleep(500);
//if (identity%2 == 0)
// right.get();
//else
// left.get();
//eating
view.setPhil(identity,view.EATING);
sleep(controller.eatTime());
right.put();
left.put();
} catch (java.lang.InterruptedException e) {}
}
}


public void stopRequested() {
stopRequested = true;
}
}
  • 打赏
  • 举报
回复
duanran 2004-05-06


import java.awt.*;

class Fork {

private boolean taken=false;
private PhilCanvas display;
private int identity;

Fork(PhilCanvas disp, int id)
{ display = disp; identity = id;}

synchronized void put() {
taken=false;
display.setFork(identity,taken);
notify();
}

synchronized void get()
throws java.lang.InterruptedException {
while (taken) wait();
taken=true;
display.setFork(identity,taken);
}
}







import java.awt.*;

class PhilCanvas extends Canvas {

Diners controller;

static final int NUMPHILS = 5;
static final int THINKING = 0;
static final int HUNGRY = 1;
static final int GOTRIGHT = 2;
static final int EATING =3;
static final int GOTLEFT = 4;

Image[] imgs = new Image[5];


double [] philX = new double[NUMPHILS];
double [] philY = new double[NUMPHILS];
int [] state = new int [NUMPHILS];
boolean[] redraw = new boolean[NUMPHILS];

double [] chopX = new double[NUMPHILS];
double [] chopY = new double[NUMPHILS];
boolean[] untable= new boolean[NUMPHILS];

boolean wasdeadlocked = false;

boolean frozen = false;

PhilCanvas(Diners controller) {
super();
this.controller = controller;

MediaTracker mt;
mt = new MediaTracker(this);

imgs[0] = controller.getImage(controller.getDocumentBase(), "image/thinking.gif");
mt.addImage(imgs[0], 0);
imgs[1] = controller.getImage(controller.getDocumentBase(), "image/hungry.gif");
mt.addImage(imgs[1], 1);
imgs[2] = controller.getImage(controller.getDocumentBase(), "image/gotright.gif");
mt.addImage(imgs[2], 2);
imgs[3] = controller.getImage(controller.getDocumentBase(), "image/eating.gif");
mt.addImage(imgs[3], 3);
imgs[4] = controller.getImage(controller.getDocumentBase(), "image/gotleft.gif");
mt.addImage(imgs[4], 4);

try {
mt.waitForID(0);
mt.waitForID(1);
mt.waitForID(2);
mt.waitForID(3);
mt.waitForID(4);
} catch (java.lang.InterruptedException e) {
System.out.println("Couldn't load one of the images");
}
initPlacing();
}

Image offscreen;
Dimension offscreensize;
Graphics offgraphics;

void backdrop() {
Dimension d = size();
if ((offscreen == null) || (d.width != offscreensize.width)
|| (d.height != offscreensize.height)) {
offscreen = createImage(d.width, d.height);
offscreensize = d;
offgraphics = offscreen.getGraphics();
offgraphics.setFont(new Font("Helvetica",Font.BOLD,18));
}
offgraphics.setColor(Color.lightGray);
offgraphics.fillRect(0, 0, size().width, size().height);
for (int i = 0; i < NUMPHILS; i++) {
redraw[i] = true;
}
}

void drawtable() {
offgraphics.setColor(Color.red);
offgraphics.fillOval(105,105,90,90);
offgraphics.setColor(Color.black);
for(int i=0; i<NUMPHILS; i++) {
if(untable[i]) offgraphics.fillOval((int)chopX[i],(int)chopY[i],10,10);
//offgraphics.drawString(String.valueOf(i),(int)chopX[i],(int)chopY[i]);
}
}


public void paint(Graphics g) {
backdrop();
update(g);
}

public void update(Graphics g) {
if (wasdeadlocked) {
backdrop();
wasdeadlocked=false;
}
for (int i = 0; i < NUMPHILS; i++) {
if (redraw[i]) {
philPaint(offgraphics,i);
redraw[i] = false;
}
}
drawtable();
if (deadlocked()) {
offgraphics.setColor(Color.black);
offgraphics.drawString("DEADLOCKED",90,130);
wasdeadlocked=true;
}
g.drawImage(offscreen, 0, 0, null);
}

void philPaint(Graphics g,int i) {
g.setColor(Color.lightGray);
g.fillRect((int)philX[i], (int)philY[i],imgs[0].getWidth(this),imgs[0].getHeight(this));
g.drawImage(imgs[state[i]], (int)philX[i], (int)philY[i], this);
// g.setColor(Color.black);
// g.drawString(String.valueOf(i),(int)philX[i]+10, (int)philY[i]+10);
}


synchronized void setPhil(int id,int s) throws java.lang.InterruptedException{
while (frozen) wait();
state[id] = s;
redraw[id] = true;
repaint();
}

synchronized void freeze(){
frozen = true;
}

synchronized void thaw() {
frozen = false;
notifyAll();
}

synchronized void setFork(int id, boolean taken) {
untable[id]= !taken;
}

boolean deadlocked(){
int i=0;
while(i<NUMPHILS && state[i]==GOTRIGHT) ++i;
return i==NUMPHILS;
}

void initPlacing() {
double x, y;
double radius = 105.0;
double centerAdj = 100.0;
double radians;

for (int i = 0; i < NUMPHILS; i++) {
radians = i*(2.0 * Math.PI /(double)NUMPHILS);
philX[i] = Math.sin(radians) * radius + centerAdj;
philY[i] = Math.cos(radians) * radius + centerAdj;
}

radius = 35;
centerAdj = 145;

for (int i = 0; i < NUMPHILS; i++) {
radians = i*(2 * Math.PI /(double)NUMPHILS) + Math.PI/5;
chopX[i] = Math.sin(radians) * radius + centerAdj;
chopY[i] = Math.cos(radians) * radius + centerAdj;
untable[i] = true;
}
}
}
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2004-05-06 04:40
社区公告
暂无公告