From 3624e1ab66dfb16b6fe83cb684629cd621a4a166 Mon Sep 17 00:00:00 2001 From: Rochas Date: Sat, 7 Dec 2024 12:37:03 +0100 Subject: [PATCH] correction du stand de cuissant et cuisinier --- .gitignore | 10 +++++++++ .vscode/settings.json | 2 ++ Au_Woke.java => src/Au_Woke.java | 8 ++++--- src/Client.java | 37 +++++++++++++++++++++++++------- src/Compartiment.java | 9 ++++++++ src/Cuisinier.java | 12 ++++++----- src/Employe_du_buffet.java | 7 ++++++ src/Restaurant.java | 3 +-- src/Stand_de_cuisson.java | 21 +++++++++++++++++- 9 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 .gitignore create mode 100644 .vscode/settings.json rename Au_Woke.java => src/Au_Woke.java (60%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9920c87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ + +# bloop and metals +.bloop +.bsp +.metals +project/metals.sbt + +# vs code +.vscode +.idea diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/Au_Woke.java b/src/Au_Woke.java similarity index 60% rename from Au_Woke.java rename to src/Au_Woke.java index 10e5059..079d16e 100644 --- a/Au_Woke.java +++ b/src/Au_Woke.java @@ -1,11 +1,13 @@ public class Au_Woke { - private Client[] clients= new Client[100]; - private Restaurant restaurant =new Restaurant(); + private Client[] clients; + private Restaurant restaurant; Au_Woke(){ + clients= new Client[4]; + restaurant =new Restaurant(); restaurant.cuisinier.start(); restaurant.employeB.start(); for(int i=0; i compartiment.getQuantite()){ + while(r > compartiment.getQuantite() && !compartiment.isLibre()){ //todo compartiment déjà occupé ? compartiment.wait(); } } catch (InterruptedException e){ e.printStackTrace(); } - System.out.println("Client " + Thread.currentThread().getId() + " : a pris une portion de " + compartiment.getName()); - + compartiment.setLibre(false); compartiment.servir(r); try{ long temps_servir =new Random().nextInt(300 - 200) + 200; @@ -42,6 +43,9 @@ public class Client extends Thread { }catch(InterruptedException e){ e.printStackTrace(); } + compartiment.setLibre(true); + System.out.println("Client " + this.id + " : a pris une portion de " + compartiment.getName()); + compartiment.notifyAll(); } } @@ -52,7 +56,7 @@ public class Client extends Thread { while(!restaurant.libre()){ restaurant.wait(); } - System.out.println("Client " + Thread.currentThread().getId() + " : est entré dans le restaurant"); + System.out.println("Client " + this.id + " : est entré dans le restaurant"); restaurant.ajouterClient(); restaurant.notifyAll(); }catch (InterruptedException e) { @@ -63,7 +67,7 @@ public class Client extends Thread { public void manger(){ try{ - System.out.println("Client " + Thread.currentThread().getId() + " : mange"); + System.out.println("Client " + this.id + " : mange"); long temps_manger =new Random().nextInt(2000 - 1000) + 1000; Thread.sleep(temps_manger); }catch(InterruptedException e){ @@ -73,7 +77,7 @@ public class Client extends Thread { public void sort(){ synchronized(restaurant){ - System.out.println("Client " + Thread.currentThread().getId() + " : sortir"); + System.out.println("Client " + this.id + " : sorti"); restaurant.diminuerClient(); restaurant.notifyAll(); } @@ -82,15 +86,32 @@ public class Client extends Thread { public void cuir_au_stand(Stand_de_cuisson stand){ synchronized(stand){ stand.ajouter_client(this); + System.out.println("Client " + this.id + " : attend pour faire cuir son plat"); + try{ + while(stand.containsClient(this)){ + stand.wait(); + } + }catch (InterruptedException e){ + e.printStackTrace(); + } + System.out.println("Client " + this.id + " : le plat est cuit"); + + + /* try{ while (stand.attendClient()!= this) { //Verify s'il est meme client wait(); } - System.out.println("Client " + Thread.currentThread().getId() + " : fait cuir son plat"); + System.out.println("Client " + this.id + " : fait cuir son plat"); stand.finir_cuit(this); }catch (InterruptedException e){ e.printStackTrace(); } + */ } } + + public String getNameClient(){ + return ("client " + this.id); + } } diff --git a/src/Compartiment.java b/src/Compartiment.java index 3b1e480..67f9694 100644 --- a/src/Compartiment.java +++ b/src/Compartiment.java @@ -2,6 +2,7 @@ public class Compartiment{ protected final int QUANTITE_MAX = 1000; private int quantite_courant; private String name; + private boolean libre; Compartiment(String name){ quantite_courant = 500; @@ -23,4 +24,12 @@ public class Compartiment{ public String getName(){ return this.name; } + + synchronized boolean isLibre(){ + return this.libre; + } + + synchronized void setLibre(boolean libre){ + this.libre = libre; + } } diff --git a/src/Cuisinier.java b/src/Cuisinier.java index 91d895a..091004a 100644 --- a/src/Cuisinier.java +++ b/src/Cuisinier.java @@ -10,8 +10,7 @@ public class Cuisinier extends Thread { public void run(){ while(Thread.currentThread().isDaemon()){ - //while(true){ - Client client= stand.attendClient(); + Client client= stand.getClient(); if(client!= null){ faire_cuire(client); } @@ -19,14 +18,17 @@ public class Cuisinier extends Thread { } public void faire_cuire(Client client){ + 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); + //long temps_cuire =new Random().nextInt(300 - 100) + 100; + //Thread.sleep(temps_cuire); + Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } + System.out.println("Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ; + stand.nextClient(); stand.finir_cuit(client); - stand.notifyAll(); } } diff --git a/src/Employe_du_buffet.java b/src/Employe_du_buffet.java index af0fd26..5d0546b 100644 --- a/src/Employe_du_buffet.java +++ b/src/Employe_du_buffet.java @@ -11,6 +11,13 @@ public class Employe_du_buffet extends Thread{ while(Thread.currentThread().isDaemon()){ for(int i = 0; inbClient); diff --git a/src/Stand_de_cuisson.java b/src/Stand_de_cuisson.java index f6c842a..8e92cb0 100644 --- a/src/Stand_de_cuisson.java +++ b/src/Stand_de_cuisson.java @@ -3,7 +3,11 @@ import java.util.Queue; public class Stand_de_cuisson { private Queue clients = new LinkedList(); - public synchronized Client attendClient(){ + + /* + * retourne le client qui est en tête de queue et le supprime de la queue + */ + public synchronized Client getNextClient2(){ while(clients.isEmpty()){ try{ wait(); @@ -13,6 +17,17 @@ public class Stand_de_cuisson { } return clients.poll(); } + + //get le client en tête de queue + public synchronized Client getClient(){ + return clients.peek(); + } + + //supprime le client en tête de queue + public synchronized void nextClient(){ + clients.poll(); + } + public synchronized void finir_cuit(Client client){ notifyAll(); } @@ -21,4 +36,8 @@ public class Stand_de_cuisson { clients.add(client); notify(); } + + public synchronized boolean containsClient(Client client){ + return this.clients.contains(client); + } }