correction du stand de cuissant et cuisinier

This commit is contained in:
Rochas
2024-12-07 12:37:03 +01:00
parent ce6a25f578
commit 3624e1ab66
9 changed files with 90 additions and 19 deletions

10
.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
# bloop and metals
.bloop
.bsp
.metals
project/metals.sbt
# vs code
.vscode
.idea

2
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,2 @@
{
}

View File

@@ -1,11 +1,13 @@
public class Au_Woke { public class Au_Woke {
private Client[] clients= new Client[100]; private Client[] clients;
private Restaurant restaurant =new Restaurant(); private Restaurant restaurant;
Au_Woke(){ Au_Woke(){
clients= new Client[4];
restaurant =new Restaurant();
restaurant.cuisinier.start(); restaurant.cuisinier.start();
restaurant.employeB.start(); restaurant.employeB.start();
for(int i=0; i<clients.length; i++){ for(int i=0; i<clients.length; i++){
clients[i] = new Client(restaurant); clients[i] = new Client(i,restaurant);
clients[i].start(); clients[i].start();
} }

View File

@@ -3,9 +3,11 @@ 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 Restaurant restaurant; protected Restaurant restaurant;
private int id;
public Client (Restaurant restaurant){ public Client (int id, Restaurant restaurant){
this.restaurant=restaurant; this.restaurant=restaurant;
this.id = id;
} }
@@ -27,14 +29,13 @@ public class Client extends Thread {
synchronized(compartiment){ synchronized(compartiment){
int r =new Random().nextInt(MAX_PORTION); int r =new Random().nextInt(MAX_PORTION);
try{ try{
while(r > compartiment.getQuantite()){ while(r > compartiment.getQuantite() && !compartiment.isLibre()){ //todo compartiment déjà occupé ?
compartiment.wait(); compartiment.wait();
} }
} catch (InterruptedException e){ } catch (InterruptedException e){
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("Client " + Thread.currentThread().getId() + " : a pris une portion de " + compartiment.getName()); compartiment.setLibre(false);
compartiment.servir(r); compartiment.servir(r);
try{ try{
long temps_servir =new Random().nextInt(300 - 200) + 200; long temps_servir =new Random().nextInt(300 - 200) + 200;
@@ -42,6 +43,9 @@ public class Client extends Thread {
}catch(InterruptedException e){ }catch(InterruptedException e){
e.printStackTrace(); e.printStackTrace();
} }
compartiment.setLibre(true);
System.out.println("Client " + this.id + " : a pris une portion de " + compartiment.getName());
compartiment.notifyAll(); compartiment.notifyAll();
} }
} }
@@ -52,7 +56,7 @@ public class Client extends Thread {
while(!restaurant.libre()){ while(!restaurant.libre()){
restaurant.wait(); restaurant.wait();
} }
System.out.println("Client " + Thread.currentThread().getId() + " : est entré dans le restaurant"); System.out.println("Client " + this.id + " : est entré dans le restaurant");
restaurant.ajouterClient(); restaurant.ajouterClient();
restaurant.notifyAll(); restaurant.notifyAll();
}catch (InterruptedException e) { }catch (InterruptedException e) {
@@ -63,7 +67,7 @@ public class Client extends Thread {
public void manger(){ public void manger(){
try{ try{
System.out.println("Client " + Thread.currentThread().getId() + " : mange"); System.out.println("Client " + this.id + " : mange");
long temps_manger =new Random().nextInt(2000 - 1000) + 1000; long temps_manger =new Random().nextInt(2000 - 1000) + 1000;
Thread.sleep(temps_manger); Thread.sleep(temps_manger);
}catch(InterruptedException e){ }catch(InterruptedException e){
@@ -73,7 +77,7 @@ public class Client extends Thread {
public void sort(){ public void sort(){
synchronized(restaurant){ synchronized(restaurant){
System.out.println("Client " + Thread.currentThread().getId() + " : sortir"); System.out.println("Client " + this.id + " : sorti");
restaurant.diminuerClient(); restaurant.diminuerClient();
restaurant.notifyAll(); restaurant.notifyAll();
} }
@@ -82,15 +86,32 @@ public class Client extends Thread {
public void cuir_au_stand(Stand_de_cuisson stand){ public void cuir_au_stand(Stand_de_cuisson stand){
synchronized(stand){ synchronized(stand){
stand.ajouter_client(this); stand.ajouter_client(this);
System.out.println("Client " + this.id + " : attend pour faire cuir son plat");
try{
while(stand.containsClient(this)){
stand.wait();
}
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("Client " + this.id + " : le plat est cuit");
/*
try{ try{
while (stand.attendClient()!= this) { //Verify s'il est meme client while (stand.attendClient()!= this) { //Verify s'il est meme client
wait(); wait();
} }
System.out.println("Client " + Thread.currentThread().getId() + " : fait cuir son plat"); System.out.println("Client " + this.id + " : fait cuir son plat");
stand.finir_cuit(this); stand.finir_cuit(this);
}catch (InterruptedException e){ }catch (InterruptedException e){
e.printStackTrace(); e.printStackTrace();
} }
*/
} }
} }
public String getNameClient(){
return ("client " + this.id);
}
} }

View File

@@ -2,6 +2,7 @@ public class Compartiment{
protected final int QUANTITE_MAX = 1000; protected final int QUANTITE_MAX = 1000;
private int quantite_courant; private int quantite_courant;
private String name; private String name;
private boolean libre;
Compartiment(String name){ Compartiment(String name){
quantite_courant = 500; quantite_courant = 500;
@@ -23,4 +24,12 @@ public class Compartiment{
public String getName(){ public String getName(){
return this.name; return this.name;
} }
synchronized boolean isLibre(){
return this.libre;
}
synchronized void setLibre(boolean libre){
this.libre = libre;
}
} }

View File

@@ -10,8 +10,7 @@ public class Cuisinier extends Thread {
public void run(){ public void run(){
while(Thread.currentThread().isDaemon()){ while(Thread.currentThread().isDaemon()){
//while(true){ Client client= stand.getClient();
Client client= stand.attendClient();
if(client!= null){ if(client!= null){
faire_cuire(client); faire_cuire(client);
} }
@@ -19,14 +18,17 @@ public class Cuisinier extends Thread {
} }
public void faire_cuire(Client client){ public void faire_cuire(Client client){
System.out.println("Cuisinier : fait cuire le plat de " + client.getNameClient()) ;
try{ try{
long temps_cuire =new Random().nextInt(300 - 100) + 100; //long temps_cuire =new Random().nextInt(300 - 100) + 100;
Thread.sleep(temps_cuire); //Thread.sleep(temps_cuire);
Thread.sleep(3000);
}catch(InterruptedException e){ }catch(InterruptedException e){
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ;
stand.nextClient();
stand.finir_cuit(client); stand.finir_cuit(client);
stand.notifyAll();
} }
} }

View File

@@ -11,6 +11,13 @@ public class Employe_du_buffet extends Thread{
while(Thread.currentThread().isDaemon()){ while(Thread.currentThread().isDaemon()){
for(int i = 0; i<buffet.length;i++){ for(int i = 0; i<buffet.length;i++){
if(buffet[i].getQuantite()<100){ if(buffet[i].getQuantite()<100){
try {
while(!buffet[i].isLibre()){
buffet[i].wait();
}
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("Employe Buffer : remplie de stand " + i); System.out.println("Employe Buffer : remplie de stand " + i);
buffet[i].remplir(); buffet[i].remplir();
} }

View File

@@ -30,7 +30,7 @@ public class Restaurant {
*/ */
public Restaurant(){ public Restaurant(){
this.nbClient=0; this.nbClient=0;
this.buffet[0] = new Compartiment("posson"); this.buffet[0] = new Compartiment("poisson");
this.buffet[1] = new Compartiment("viande"); this.buffet[1] = new Compartiment("viande");
this.buffet[2] = new Compartiment("légume"); this.buffet[2] = new Compartiment("légume");
this.buffet[3] = new Compartiment("nouille"); this.buffet[3] = new Compartiment("nouille");
@@ -38,7 +38,6 @@ public class Restaurant {
this.employeB = new Employe_du_buffet(buffet); this.employeB = new Employe_du_buffet(buffet);
this.stand_de_cuisson = new Stand_de_cuisson(); this.stand_de_cuisson = new Stand_de_cuisson();
this.cuisinier = new Cuisinier(stand_de_cuisson); this.cuisinier = new Cuisinier(stand_de_cuisson);
} }
public synchronized boolean libre(){ public synchronized boolean libre(){
return (MAX_CLIENTS>nbClient); return (MAX_CLIENTS>nbClient);

View File

@@ -3,7 +3,11 @@ import java.util.Queue;
public class Stand_de_cuisson { public class Stand_de_cuisson {
private Queue<Client> clients = new LinkedList<Client>(); private Queue<Client> clients = new LinkedList<Client>();
public synchronized Client attendClient(){
/*
* retourne le client qui est en tête de queue et le supprime de la queue
*/
public synchronized Client getNextClient2(){
while(clients.isEmpty()){ while(clients.isEmpty()){
try{ try{
wait(); wait();
@@ -13,6 +17,17 @@ public class Stand_de_cuisson {
} }
return clients.poll(); return clients.poll();
} }
//get le client en tête de queue
public synchronized Client getClient(){
return clients.peek();
}
//supprime le client en tête de queue
public synchronized void nextClient(){
clients.poll();
}
public synchronized void finir_cuit(Client client){ public synchronized void finir_cuit(Client client){
notifyAll(); notifyAll();
} }
@@ -21,4 +36,8 @@ public class Stand_de_cuisson {
clients.add(client); clients.add(client);
notify(); notify();
} }
public synchronized boolean containsClient(Client client){
return this.clients.contains(client);
}
} }