95 lines
2.8 KiB
Java
95 lines
2.8 KiB
Java
import java.util.Random;
|
|
|
|
public class Client extends Thread {
|
|
private int MAX_PORTION = 100;
|
|
protected Restaurant restaurant;
|
|
|
|
public Client (Restaurant restaurant){
|
|
this.restaurant=restaurant;
|
|
}
|
|
|
|
|
|
public void run(){
|
|
|
|
entrer();
|
|
for(int i = 0; i<this.restaurant.buffet.length; i++){
|
|
prendre_portion(this.restaurant.buffet[i]);
|
|
}
|
|
cuir_au_stand(restaurant.stand_de_cuisson);
|
|
manger();
|
|
sort();
|
|
|
|
}
|
|
|
|
public void prendre_portion(Compartiment compartiment){
|
|
synchronized(compartiment){
|
|
int r =new Random().nextInt(MAX_PORTION);
|
|
try{
|
|
while(r > compartiment.getQuantite()){
|
|
compartiment.wait();
|
|
}
|
|
} catch (InterruptedException e){
|
|
e.printStackTrace();
|
|
}
|
|
System.out.println("Clien " + Thread.currentThread().getId() + " : a pris une portion");
|
|
|
|
compartiment.servir(r);
|
|
try{
|
|
long temps_servir =new Random().nextInt(300 - 200) + 200;
|
|
Thread.sleep(temps_servir);
|
|
}catch(InterruptedException e){
|
|
e.printStackTrace();
|
|
}
|
|
compartiment.notifyAll();
|
|
}
|
|
}
|
|
|
|
public void entrer(){
|
|
synchronized(restaurant){
|
|
try{
|
|
while(!restaurant.libre()){
|
|
restaurant.wait();
|
|
}
|
|
System.out.println("Clien " + Thread.currentThread().getId() + " : est entré dans le restaurant");
|
|
restaurant.ajouterClient();
|
|
restaurant.notifyAll();
|
|
}catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void manger(){
|
|
try{
|
|
System.out.println("Clien " + Thread.currentThread().getId() + " : mange");
|
|
long temps_manger =new Random().nextInt(2000 - 1000) + 1000;
|
|
Thread.sleep(temps_manger);
|
|
}catch(InterruptedException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void sort(){
|
|
synchronized(restaurant){
|
|
System.out.println("Clien " + Thread.currentThread().getId() + " : sortir");
|
|
restaurant.diminuerClient();
|
|
restaurant.notifyAll();
|
|
}
|
|
}
|
|
|
|
public void cuir_au_stand(Stand_de_cuisson stand){
|
|
synchronized(stand){
|
|
stand.ajouter_client(this);
|
|
try{
|
|
while (stand.attendClient()!= this) { //Verify s'il est meme client
|
|
wait();
|
|
}
|
|
System.out.println("Clien " + Thread.currentThread().getId() + " : fait cuir son plat");
|
|
stand.finir_cuit(this);
|
|
}catch (InterruptedException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|