2

我在java中实现了哲学家就餐问题,但是出了点问题,导致死锁......有人能发现为什么这会导致死锁吗?

哲学家在餐厅里应该永远是 N-1,这样每个人都可以吃饭并防止饥饿。

主要类:

public class DiningPhilosophers {

public static int N = 5;
//Philosopher philosopher[];
//Chopsticks chopsticks;
//DiningRoom diningroom;


public static void main(String[] args) {
    Philosopher[] philosopher = new Philosopher[N];
    Chopsticks chopsticks = new Chopsticks(N);
    DiningRoom diningroom = new DiningRoom(N);

    for(int i = 0;i<N;i++)
        philosopher[i] = new Philosopher(i,10,20,30,100,chopsticks,diningroom);

    for(int i = 0;i<N;i++)
        philosopher[i].start();


}

}

筷子类:

class Chopsticks{


private int numOfChops[];
private int N;

public Chopsticks(int N){
    this.N = N;
    numOfChops = new int[N];
    for(int i = 0;i<N;i++)
        numOfChops[i] = 2;

}

public synchronized void take(int philosopher){
    while(numOfChops[philosopher] != 2){
        try{
            wait();
        }catch(InterruptedException e){

        }
        //System.out.println("philosopher "+philosopher+"taking");
        numOfChops[(philosopher+1)%N]--;
        numOfChops[(Math.abs(philosopher-1))%N]--;
    }
}

public synchronized void release(int philosopher){
    //System.out.println("philosopher "+philosopher+"releasing");
    numOfChops[(philosopher+1)%N]++;
    numOfChops[(Math.abs(philosopher-1))%N]++;
    notifyAll();
}
}

食堂课:

class DiningRoom{
private int vacancy;
private int n;

public DiningRoom(int N){
    this.n = N;
    vacancy = n -1;

}

public synchronized void enter(){
    while(vacancy == 0){
        try{
            wait();
        }catch(InterruptedException e){             
        }           
        vacancy--;          
    }
}

public synchronized void exit(){

    vacancy++;
    notify();

}
}

哲学家班:

class Philosopher extends Thread{

int i;
int minThinkTime,maxThinkTime,minEatTime,maxEatTime;
private Chopsticks c;
private DiningRoom d;

public Philosopher(int index,int minThinkTime,int maxThinkTime,int minEatTime,int maxEatTime, Chopsticks chopsticks, DiningRoom diningroom){
    this.i = index;
    this.minThinkTime = minThinkTime;
    this.maxThinkTime = maxThinkTime;
    this.minEatTime = minEatTime;
    this.maxEatTime = maxEatTime;
    this.c = chopsticks;
    this.d = diningroom;
} 

public void think(){
    try{
        System.out.println(i+" Philosopher is thinking!");
        Thread.sleep((int)(Math.random()*(maxThinkTime - minThinkTime))+minThinkTime);

    }catch(InterruptedException e){

    }
}

public void eat(){
    try{
        System.out.println(i+" Philosopher is eating!");
        Thread.sleep((int)(Math.random()*(maxEatTime - minEatTime))+minEatTime);

    }catch(InterruptedException e){

    }
}
public void run() {
    while (true) {
         think();
         System.out.println("pholosopher "+i+"entering");
         d.enter();
         c.take(i);
         eat();
         c.release(i);
         d.exit();
         System.out.println("pholosopher "+i+"exiting");
    }
}


}
4

2 回答 2

2

我认为您的 Chopsticks.take() 是错误的。现在,当一个哲学家开始拿起筷子时,它什么也不做。然后,当他为他的邻居释放 chospticks numOfChops 时,他的邻居会增加并且永远不会等于 2,所以他们都会在 take() 中阻塞。

您已经将大括号从 while 太靠近 take() 的末尾放置,这应该是:

public synchronized void take(int philosopher){
    while(numOfChops[philosopher] != 2){
        try{
            wait();
        }catch(InterruptedException e){

        }
     }
    //System.out.println("philosopher "+philosopher+"taking");
    numOfChops[(philosopher+1)%N]--;
    numOfChops[(Math.abs(philosopher-1))%N]--;

}
于 2011-12-20T10:17:22.810 回答
1

在:

public synchronized void take(int philosopher)

您应该将减少筷子计数器的线移到循环之外。

于 2011-12-20T10:13:50.997 回答