le Client utilise un sméaphore pour cuir son plat

This commit is contained in:
trochas
2024-12-08 13:38:07 +01:00
parent d414136443
commit c92ba8c0f4
4 changed files with 9 additions and 80 deletions

View File

@@ -82,16 +82,9 @@ public class Client extends Thread {
}
public void cuir_au_stand(Stand_de_cuisson stand){
stand.ajouter_client(this);
System.out.println(this.getNameClient() + " : attend pour faire cuir son plat");
synchronized(stand){
try{
while(stand.containsClient(this)){
stand.wait();
}
}catch (InterruptedException e){
e.printStackTrace();
}
stand.faire_cuire_plat(this);
}
System.out.println(this.getNameClient() + " : le plat est cuit");

View File

@@ -1,35 +1,11 @@
import java.util.Random;
public class Cuisinier extends Thread {
private Stand_de_cuisson stand;
public Cuisinier(Stand_de_cuisson stand_de_cuisson){
this.stand= stand_de_cuisson;
public Cuisinier(){
this.setDaemon(true);
}
public void run(){
while(Thread.currentThread().isDaemon()){
attendreClient();
}
}
private void attendreClient(){
Client client = stand.getClient();
synchronized(stand){
try{
while(client==null){
stand.wait();
client= stand.getClient();
}
}
catch(InterruptedException e){
e.printStackTrace();
}
}
faire_cuire(client);
}
public void faire_cuire(Client client){
System.out.println("# Cuisinier : fait cuire le plat de " + client.getNameClient()) ;
try{
@@ -39,7 +15,5 @@ public class Cuisinier extends Thread {
e.printStackTrace();
}
System.out.println("# Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ;
stand.nextClient();
stand.finir_cuit();
}
}

View File

@@ -7,27 +7,8 @@ public class Restaurant {
public Employe_du_buffet employeB;
public Stand_de_cuisson stand_de_cuisson;
public Cuisinier cuisinier;
/*
private Client[] clients= new Client[MAX_CLIENTS+5];
Restaurant(){
nbClient = 0;
for(int i = 0 ; i<buffet.length; i++){
this.buffet[i] = new Compartiment();
}
this.employeB = new Employe_du_buffet(buffet);
this.stand_de_cuisson = new Stand_de_cuisson();
this.cuisinier = new Cuisinier(stand_de_cuisson);
employeB.start();
cuisinier.start();
for(int i=0; i<clients.length; i++){
clients[i] = new Client(this);
clients[i].start();
}
}
*/
public Restaurant(){
this.nbClient=0;
this.buffet[0] = new Compartiment("poisson");
@@ -35,12 +16,11 @@ public class Restaurant {
this.buffet[2] = new Compartiment("légume");
this.buffet[3] = new Compartiment("nouille");
this.stand_de_cuisson = new Stand_de_cuisson();
this.employeB = new Employe_du_buffet(buffet);
this.cuisinier = new Cuisinier(stand_de_cuisson);
this.cuisinier = new Cuisinier();
this.stand_de_cuisson = new Stand_de_cuisson(cuisinier);
this.employeB.start();
this.cuisinier.start();
}
public synchronized boolean libre(){
return (MAX_CLIENTS>nbClient);

View File

@@ -1,30 +1,12 @@
import java.util.LinkedList;
import java.util.Queue;
public class Stand_de_cuisson {
private Queue<Client> clients = new LinkedList<Client>();
private Cuisinier cuisinier;
//get le client en tête de queue
public synchronized Client getClient(){
return clients.peek();
Stand_de_cuisson(Cuisinier cuisinier){
this.cuisinier = cuisinier;
}
//supprime le client en tête de queue
public synchronized void nextClient(){
clients.poll();
}
public synchronized void finir_cuit(){
notifyAll();
}
public synchronized void ajouter_client(Client client){
clients.add(client);
notifyAll();
}
public synchronized boolean containsClient(Client client){
return this.clients.contains(client);
public synchronized void faire_cuire_plat(Client client){
this.cuisinier.faire_cuire(client);
}
}