resto
This commit is contained in:
@@ -3,11 +3,14 @@ import java.util.Random;
|
|||||||
public class Client extends Thread {
|
public class Client extends Thread {
|
||||||
private int MAX_PORTION = 100;
|
private int MAX_PORTION = 100;
|
||||||
protected Compartiment compartiment;
|
protected Compartiment compartiment;
|
||||||
|
protected Restaurant restaurant;
|
||||||
|
|
||||||
public Client (Compartiment c){
|
public Client (Compartiment c, Restaurant restaurant){
|
||||||
this.compartiment=c;
|
this.compartiment=c;
|
||||||
|
this.restaurant=restaurant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void prendre_portion(Compartiment compartiment){
|
public void prendre_portion(Compartiment compartiment){
|
||||||
synchronized(compartiment){
|
synchronized(compartiment){
|
||||||
int r =new Random().nextInt(MAX_PORTION);
|
int r =new Random().nextInt(MAX_PORTION);
|
||||||
@@ -19,13 +22,41 @@ public class Client extends Thread {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
compartiment.servir(r);
|
compartiment.servir(r);
|
||||||
int temps_servir =new Random().nextInt(300 - 200) + 200;
|
try{
|
||||||
//this.wait(temps_servir);
|
long temps_servir =new Random().nextInt(300 - 200) + 200;
|
||||||
|
Thread.sleep(temps_servir);
|
||||||
|
}catch(InterruptedException e){
|
||||||
|
System.out.println(e);
|
||||||
|
}
|
||||||
compartiment.notifyAll();
|
compartiment.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void entrer(){
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,22 @@ public class Restaurant {
|
|||||||
|
|
||||||
Employe_du_buffet employeB = new Employe_du_buffet(compPoisson,compViande,compLegume,compNouille);
|
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<MAX_CLIENTS){
|
||||||
|
nbClient++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void diminuerClient(){
|
||||||
|
if(nbClient<MAX_CLIENTS){
|
||||||
|
nbClient++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user