java中的餐饮哲学家导致僵局

时间:2011-12-20 10:03:56

标签: java multithreading deadlock dining-philosopher

我在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();


}

}

Chopstick类:

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");
    }
}


}

2 个答案:

答案 0 :(得分:2)

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

你把大括号放在了接近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]--;

}

答案 1 :(得分:1)

in:

public synchronized void take(int philosopher)

你应该移动减少筷子计数器在线圈外的线条。