From 9452c0ea7794ba85f4ea0ed91cafc4cc819389b1 Mon Sep 17 00:00:00 2001 From: Minh VU Date: Tue, 3 Dec 2024 15:07:18 +0100 Subject: [PATCH] ok --- .gitignore | 10 ---------- src/Client.java | 35 +++++++++++++++++++++++------------ src/Cuisinier.java | 2 +- src/Stand_de_cuisson.java | 5 +++-- 4 files changed, 27 insertions(+), 25 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 9920c87..0000000 --- a/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ - -# bloop and metals -.bloop -.bsp -.metals -project/metals.sbt - -# vs code -.vscode -.idea diff --git a/src/Client.java b/src/Client.java index 9d52425..0a61387 100644 --- a/src/Client.java +++ b/src/Client.java @@ -29,14 +29,14 @@ public class Client extends Thread { this.wait(); } } catch (InterruptedException e){ - throw new RuntimeException(e); + e.printStackTrace(); } compartiment.servir(r); try{ long temps_servir =new Random().nextInt(300 - 200) + 200; Thread.sleep(temps_servir); }catch(InterruptedException e){ - System.out.println(e); + e.printStackTrace(); } compartiment.notifyAll(); } @@ -47,12 +47,12 @@ public class Client extends Thread { try{ while(!restaurant.libre()){ this.wait(); - } + } + restaurant.ajouterClient(); + restaurant.notifyAll(); }catch (InterruptedException e) { - throw new RuntimeException(e); - } - restaurant.ajouterClient(); - restaurant.notifyAll(); + e.printStackTrace(); + } } } @@ -61,17 +61,28 @@ public class Client extends Thread { long temps_manger =new Random().nextInt(2000 - 1000) + 1000; Thread.sleep(temps_manger); }catch(InterruptedException e){ - System.out.println(e); + e.printStackTrace(); } } public void sort(){ - restaurant.diminuerClient(); - restaurant.notifyAll(); + synchronized(restaurant){ + restaurant.diminuerClient(); + restaurant.notifyAll(); + } } public void cuir_au_stand(Stand_de_cuisson stand){ - stand.ajouter_client(this); - //TODO + synchronized(stand){ + stand.ajouter_client(this); + try{ + while (stand.attendClient()!= this) { //Verify s'il est meme client + wait(); + } + stand.finir_cuit(this); + }catch (InterruptedException e){ + e.printStackTrace(); + } + } } } diff --git a/src/Cuisinier.java b/src/Cuisinier.java index adacf95..99719dc 100644 --- a/src/Cuisinier.java +++ b/src/Cuisinier.java @@ -21,7 +21,7 @@ public class Cuisinier extends Thread { long temps_cuire =new Random().nextInt(300 - 100) + 100; Thread.sleep(temps_cuire); }catch(InterruptedException e){ - System.out.println(e); + e.printStackTrace(); } stand.finir_cuit(client); stand.notifyAll(); diff --git a/src/Stand_de_cuisson.java b/src/Stand_de_cuisson.java index 2c02318..f6c842a 100644 --- a/src/Stand_de_cuisson.java +++ b/src/Stand_de_cuisson.java @@ -7,8 +7,8 @@ public class Stand_de_cuisson { while(clients.isEmpty()){ try{ wait(); - }catch (Exception e) { - System.out.println(e); + }catch (InterruptedException e) { + e.printStackTrace(); } } return clients.poll(); @@ -19,5 +19,6 @@ public class Stand_de_cuisson { public synchronized void ajouter_client(Client client){ clients.add(client); + notify(); } }