Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的哲学家吃饭导致死锁_Java_Multithreading_Deadlock_Dining Philosopher - Fatal编程技术网

java中的哲学家吃饭导致死锁

java中的哲学家吃饭导致死锁,java,multithreading,deadlock,dining-philosopher,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 ma

我已经用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 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");
    }
}


}
在:

public synchronized void take(int哲学家)


你应该把减少筷子计数器的线移到圈外。

我认为你的筷子.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]--;

}

@stelios看起来你在餐厅也犯了同样的错误#enter()下面的答案可能已经修复了你的僵局,但我担心你使用了
(Math.abs(哲学家-1))%N
。这不会在N上实现响铃。您需要使用
(哲学家-1+N)%N
来正确响铃,因为您需要
0-1
来评估
N-1
。同样
餐厅。退出
可能应该使用
通知所有人
筷子。释放
而不仅仅是
通知
。或许不是。。。我现在不确定(
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");
    }
}


}
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]--;

}