Merci pour vos réponses !
J'avais mal compris l'usage du verrou en effet. J'en avais un pour chaque philosophe, ce qui n'est pas logique.
Merci beaucoup pour vos réponses !
Voici la version finale de mon code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Monitor{
static int nombreDePhilosophes = 5;
static int[] etats = new int[nombreDePhilosophes];
static Lock lock = new ReentrantLock();
static Condition[] eat = new Condition[Monitor.nombreDePhilosophes];
public Monitor(){
for(int j =0;j<nombreDePhilosophes;j++){
eat[j] = lock.newCondition();
}
}
public static void main(String[] args) {
new Monitor();
for (int i = 0; i < nombreDePhilosophes; i++) {
etats[i]=0;//everyone starts thinking
new Thread(new Philo(i)).start();
}
}
static void prendre_une_fourchette(int i) {
try {
lock.lock();
etats[i]=2;//wants to eat
if(etats[(i-1+nombreDePhilosophes)%nombreDePhilosophes] !=1 && etats[(i+1+nombreDePhilosophes)%nombreDePhilosophes] !=1){
//si les 2 voisins ne mangent pas, je mange
System.out.println("Philo "+ i+" a ses 2 fourchettes.");
etats[i]=1;
}else{
try {
eat[i].await();
System.out.println("Philo "+ i+" a ses 2 fourchettes.");
etats[i]=1;//mange
} catch (InterruptedException e) {
}
}
}finally{
lock.unlock();
}
manger(i);
}
static void poser_fourchette(int i) {
lock.lock();
try{
etats[i]=0;//ne mange plus
if(etats[(i-2+nombreDePhilosophes)%nombreDePhilosophes] !=1 && etats[(i-1+nombreDePhilosophes)%nombreDePhilosophes]==2){
for(int j =0;j<nombreDePhilosophes;j++){
System.out.println("Etat Philo "+j+" "+etats[j]);
}
System.out.println("Philo "+ i +" signale à Philo "+(i-1+nombreDePhilosophes)%nombreDePhilosophes +" que sa fourchette de droite est libre.");
eat[(i-1+nombreDePhilosophes)%nombreDePhilosophes].signal();
}
if(etats[(i+2+nombreDePhilosophes)%nombreDePhilosophes] !=1 && etats[(i+1+nombreDePhilosophes)%nombreDePhilosophes]==2 ){
for(int j =0;j<nombreDePhilosophes;j++){
System.out.println("Etat Philo "+j+" "+etats[j]);
}
System.out.println("Philo "+ i +" signale à Philo "+(i+1+nombreDePhilosophes)%nombreDePhilosophes +" que sa fourchette de gauche est libre.");
eat[(i+1+nombreDePhilosophes)%nombreDePhilosophes].signal();
}
}finally{
lock.unlock();
}
pense(i);
}
static void pense(int i) {
System.out.println("Philosophe "+i+" est en train de penser.");
long wait=(long)(Math.random()*1000);
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
}
}
static void manger(int i) {
System.out.println("Philosophe "+i+" est en train de manger ");
long wait=(long)(Math.random()*1000);
Monitor.etats[i]=1;
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
}
}
} |
et
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public class Philo implements Runnable {
int i;
public Philo(int i) {
this.i=i;
}
public void run() {
new Philo(i);
System.out.println("Le philosophe "+i+" est arrivé");
while(true){
Monitor.prendre_une_fourchette(i);
Monitor.poser_fourchette(i);
}
}
} |