Merge branch 'revert-1a0c8083' into 'main'

Revert "le Client utilise un sméaphore pour cuir son plat"

See merge request trochas/tp-4-wok!1
This commit is contained in:
Rochas Thibaut
2024-12-08 12:31:54 +00:00
5 changed files with 95 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
Nous avons la Class Au_Wok qui contient le main(), on y initialise les clients
ainsi que le restaurant, puis on start tous les clients.
Le constructeur Restaurant initialise le Buffet : un tableau de 4 Compartiment
(poisson, viande, légume et nouille), le Cusinier, le stand de cuisson,
l'Employe_du_buffet. Puis il star en suite start l'employer du buffet.
Le constructeur Restaurant initialise le Buffet, un tableau de 4 Compartiment
(poisson, viande, légume et nouille), le stand de cuisson, l'Employe_du_buffet
et le Cuisinier. Il star en suite start ces deux derniers.
Le run de Client va suivre ces étapes :
(1) Entrer dans le restaurant en vérifiant bien que le restaurant est libre
@@ -11,20 +11,22 @@ Le run de Client va suivre ces étapes :
le verrou du compartiment est libre avant de lui-même le verrouiller, après
c'être servi il déverrouille fait un notifyAll pour réveiller les clients
ou l'employer du buffet qui pourrait attendre.
(3) Cuire au stand, on utilise un sémaphore.
(3) Cuire au stand, en se mettant dans la queue (il notifyAll le stand pour
réveiller le cuisinier), quand son plat est cuit le cuisinier le sort de la
queue (le stand fait un notifyAll pour réveiller le client)
(4) Manger
(5) Sort, en faisant un notifyAll sur le restaurant pour prévenir tout autre
thread client attendant pour entrer
Le run du cuisinier, fait une boucle pour vérifier si un client est présent en
tête de la queue du stand de cuisson. Si oui, alors il fait cuire son plat.
Une fois fini, il sort le client le la queue et fait faire un notifyAll au stand
pour prévenir le client que son plat est fini.
L'employer du buffet fait le tour de chaque compartiment du buffet, s'il y a un
un buffet à moins de 100g alors il vérifie qu'il est libre (se met en wait sinon)
et le remplit.
Difficulté :
Nous n'avons pas vraiment rencontré de difficulté, on a juste eu parfois quelque
oublie (notifyAll manquant par exemple).
On avait aussi commencé dans Stand_de_cuisson par faire une Queue, on a ensuite
remplacé par un sémaphore, qui est au final beaucoup plus simple, mais le verrou
n'est plus équitable, ce n'est plus le Client qui attend depuis le plus long temps
qui est prioritaire.
Nous avons pas vraiment rencontré de difficulté, on a juste eu parfois quelque
oublie (notifyAll manquant par exemple)

View File

@@ -82,9 +82,16 @@ 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){
stand.faire_cuire_plat(this);
try{
while(stand.containsClient(this)){
stand.wait();
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println(this.getNameClient() + " : le plat est cuit");

View File

@@ -1,12 +1,35 @@
import java.util.Random;
public class Cuisinier extends Thread {
private Stand_de_cuisson stand;
public Cuisinier(){
public Cuisinier(Stand_de_cuisson stand_de_cuisson){
this.stand= stand_de_cuisson;
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{
@@ -16,5 +39,7 @@ 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,8 +7,27 @@ 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");
@@ -16,11 +35,12 @@ 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();
this.stand_de_cuisson = new Stand_de_cuisson(cuisinier);
this.cuisinier = new Cuisinier(stand_de_cuisson);
this.employeB.start();
this.cuisinier.start();
}
public synchronized boolean libre(){
return (MAX_CLIENTS>nbClient);

View File

@@ -1,11 +1,30 @@
import java.util.LinkedList;
import java.util.Queue;
public class Stand_de_cuisson {
private Cuisinier cuisinier;
private Queue<Client> clients = new LinkedList<Client>();
Stand_de_cuisson(Cuisinier cuisinier){
this.cuisinier = cuisinier;
//get le client en tête de queue
public synchronized Client getClient(){
return clients.peek();
}
public synchronized void faire_cuire_plat(Client client){
this.cuisinier.faire_cuire(client);
//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);
}
}