主题:[讨论]java哲学家就餐问题 求程序改进
//第一个
class Zhexuejia extends Thread{
public Menage menage;
public Zhexuejia(){
menage=new Menage();
}
}
//第二个
class Zhexuejia1 extends Zhexuejia{
publicvoidrun(){
menage.get(menage.k1);
menage.get(menage.k5);
System.out.println("1chiguole");
menage.put();
}
}
//第三个
class Zhexuejia2 extendsZhexuejia{
public void run(){
menage.get(menage.k1);
menage.get(menage.k2);
System.out.println("2 chi guo le");
menage.put();
}
}
//第四个
class Zhexuejia3 extends Zhexuejia{
public void run(){
menage.get(menage.k2);
menage.get(menage.k3);
System.out.println("3 chi guo le");
menage.put();
}
}
//第五个
class Zhexuejia4 extends Zhexuejia{
public void run(){
menage.get(menage.k3);
menage.get(menage.k4);
System.out.println("4 chi guo le");
menage.put();
}
}
//第六个
class Zhexuejia5 extends Zhexuejia{
public void run(){
menage.get(menage.k4);
menage.get(menage.k5);
System.out.println("5 chi guo le");
menage.put();
}
}
//第七个
class Menage{
private boolean biaoji=false;
public String k1="kuai zi 1";
public String k2="kuai zi 2";
public String k3="kuai zi 3";
public String k4="kuai zi 4";
public String k5="kuai zi 5";
public synchronized String get(Stringc){
while(biaoji==false){
try{
wait();
}catch(InterruptedExceptione){}
}
biaoji=false;
notifyAll();
returnc;
}
public synchronized void put(){
while(biaoji==true){
try{
wait();
}catch(InterruptedExceptione){}
}
biaoji=true;
notifyAll();
}
}
//第八个
public class A3{
public static void main(String[]args){
Zhexuejia1a=new Zhexuejia1();
Zhexuejia2b=new Zhexuejia2();
Zhexuejia3c=new Zhexuejia3();
Zhexuejia4d=new Zhexuejia4();
Zhexuejia5e=new Zhexuejia5();
a.start();
b.start();
c.start();
d.start();
e.start();
}
}
想叫大家帮忙该下,编译是没问题,但是没有结果出来
可能的话帮忙加
写一程序防止死锁的发生,让每位哲学家都有吃的机会。并在屏幕上打印出有多少哲学家在思考,在工作,在吃。