correction bug compartiment (init libre a true, et boucle while avec un ||, manquait aussi un notifyAll après avoir rempli un compartiment)

This commit is contained in:
trochas
2024-12-08 11:57:09 +01:00
parent 9f6dabf199
commit 13fe8abe71
5 changed files with 8 additions and 9 deletions

View File

@@ -28,7 +28,7 @@ public class Client extends Thread {
synchronized(compartiment){
int r =new Random().nextInt(MAX_PORTION);
try{
while(r > compartiment.getQuantite() && !compartiment.isLibre()){ //todo compartiment déjà occupé ?
while(r > compartiment.getQuantite() || !compartiment.isLibre()){
compartiment.wait();
}
} catch (InterruptedException e){

View File

@@ -5,12 +5,14 @@ public class Compartiment{
private boolean libre;
Compartiment(String name){
quantite_courant = 500;
this.quantite_courant = 500;
this.name = name;
this.libre = true;
}
synchronized void remplir(){
quantite_courant = QUANTITE_MAX;
notifyAll();
}
synchronized void servir(int qte){

View File

@@ -31,16 +31,15 @@ public class Cuisinier extends Thread {
}
public void faire_cuire(Client client){
System.out.println("Cuisinier : fait cuire le plat de " + client.getNameClient()) ;
System.out.println("# Cuisinier : fait cuire le plat de " + client.getNameClient()) ;
try{
long temps_cuire =new Random().nextInt(300 - 100) + 100;
Thread.sleep(temps_cuire);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ;
System.out.println("# Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ;
stand.nextClient();
stand.finir_cuit();
}
}

View File

@@ -16,9 +16,9 @@ public class Employe_du_buffet extends Thread{
buffet[i].wait();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("Employe Buffer : remplie de stand " + i);
System.out.println("Employe Buffer : remplie de stand " + buffet[i].getName() + ", il ne restait plus que " + buffet[i].getQuantite() +" g");
buffet[i].remplir();
}
}

View File

@@ -46,10 +46,8 @@ public class Restaurant {
return (MAX_CLIENTS>nbClient);
}
public synchronized void ajouterClient(){
if(nbClient<MAX_CLIENTS){
nbClient++;
}
}
public synchronized void diminuerClient(){
nbClient--;