diff --git a/src/Client.java b/src/Client.java index fc6f745..bb1135c 100644 --- a/src/Client.java +++ b/src/Client.java @@ -3,11 +3,14 @@ import java.util.Random; public class Client extends Thread { private int MAX_PORTION = 100; protected Compartiment compartiment; + protected Restaurant restaurant; - public Client (Compartiment c){ + public Client (Compartiment c, Restaurant restaurant){ this.compartiment=c; + this.restaurant=restaurant; } + public void prendre_portion(Compartiment compartiment){ synchronized(compartiment){ int r =new Random().nextInt(MAX_PORTION); @@ -19,13 +22,41 @@ public class Client extends Thread { throw new RuntimeException(e); } compartiment.servir(r); - int temps_servir =new Random().nextInt(300 - 200) + 200; - //this.wait(temps_servir); + try{ + long temps_servir =new Random().nextInt(300 - 200) + 200; + Thread.sleep(temps_servir); + }catch(InterruptedException e){ + System.out.println(e); + } compartiment.notifyAll(); } } public void entrer(){ + synchronized(restaurant){ + try{ + while(!restaurant.libre()){ + this.wait(); + } + }catch (InterruptedException e) { + throw new RuntimeException(e); + } + restaurant.ajouterClient(); + restaurant.notifyAll(); + } + } + public void manger(){ + try{ + long temps_manger =new Random().nextInt(2000 - 1000) + 1000; + Thread.sleep(temps_manger); + }catch(InterruptedException e){ + System.out.println(e); + } + } + + public void sort(){ + restaurant.diminuerClient(); + restaurant.notifyAll(); } } diff --git a/src/Restaurant.java b/src/Restaurant.java index 9217e8c..6e933e0 100644 --- a/src/Restaurant.java +++ b/src/Restaurant.java @@ -10,4 +10,22 @@ public class Restaurant { Employe_du_buffet employeB = new Employe_du_buffet(compPoisson,compViande,compLegume,compNouille); + public synchronized boolean libre(){ + if(nbClient>MAX_CLIENTS){ + return false; + }else { + return true; + } + } + public synchronized void ajouterClient(){ + if(nbClient