This commit is contained in:
Minh VU
2024-11-28 10:41:37 +01:00
parent d949e97583
commit d3f78c7ec1
2 changed files with 52 additions and 3 deletions

View File

@@ -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();
}
}

View File

@@ -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<MAX_CLIENTS){
nbClient++;
}
}
public synchronized void diminuerClient(){
if(nbClient<MAX_CLIENTS){
nbClient++;
}
}
}