Frage

I have implement dining philosophers problem in java but something goes wrong and it leads to deadlock... Can someone spot why this leads to deadlock?

The philosophers should always be N-1 in the dining room so everyone can eat and prevent starvation.

The main class:

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


}

}

The Chopstick class:

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

The dining room class:

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

}
}

The philosopher class:

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


}
War es hilfreich?

Lösung

in:

public synchronized void take(int philosopher)

you should move the lines that decrease the chopstick counters outside the loop.

Andere Tipps

I think your Chopsticks.take() is wrong. Right now, when a philosopher takes chopsticks in the beginning, it does nothing. Then when he releases chospticks numOfChops for his neighbors increases and is never equal to 2 so they all block in take().

You've put the curly brace from the while too close to the end of take(), this is where it shoud be:

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

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top