sémaphore sur les compatiments

This commit is contained in:
Rochas
2024-12-09 22:15:57 +01:00
parent 06bf38a576
commit c5e73329b6
7 changed files with 115 additions and 55 deletions

View File

@@ -2,35 +2,44 @@ public class Employe_du_buffet extends Thread{
public Buffet buffet;
public Employe_du_buffet(Buffet buffet){
this.buffet = buffet;
this.setDaemon(true);
}
/*
* vas faire le tour de tout les buffet
* si un a moins de 100g, si il n'est pas libre il wait dessus, sinon il le remplis
* avant de se réendormir il vérifit qu'aucun compartiment n'as été vidé pendant dans son dot
*/
public void run(){
synchronized(buffet){
while(Thread.currentThread().isDaemon()){
for(int i = 0; i<buffet.length();i++){
Compartiment compartiment = buffet.getCompartiment(i);
while(Thread.currentThread().isDaemon()){
for(int i = 0; i<buffet.length();i++){
Compartiment compartiment = buffet.getCompartiment(i);
try {
compartiment.getSemaphore().acquire();
if(compartiment.getQuantite()<100){
try {
while(!compartiment.isLibre()){
compartiment.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Employe Buffer : remplie de stand " + compartiment.getName() + ", il ne restait plus que " + compartiment.getQuantite() +" g");
compartiment.remplir();
}
}
try {
buffet.wait();
} catch (Exception e) {
e.printStackTrace();
} finally{
compartiment.getSemaphore().release();
}
}
synchronized(buffet){
if(!this.buffet.isEmpty()){
try {
buffet.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}