correction de bug, normalement tout marche + rapport

This commit is contained in:
Rochas
2024-12-07 16:46:49 +01:00
parent f5cb2d1582
commit 9f6dabf199
7 changed files with 77 additions and 68 deletions

28
rapport.txt Normal file
View File

@@ -0,0 +1,28 @@
Nous avons la Class Au_Wok qui contient le main(), on y initialise lesclients
ainsi que le restaurant, puis on start tous les clients.
Le constructeur Restaurant initialise le Buffet, un tableau de 4 Compartiment
(poisson, viande, légume et nouille), le stand de cuisson, l'Employe_du_buffet
et le Cuisinier. Il star en suite start ces deux derniers.
Le run de Client va suivre ces étapes :
(1) Entrer dans le restaurant en vérifiant bien que le restaurant est libre
(2) Prendre une portion de chaque Compartiment du buffet, en vérifiant
le verrou du compartiment est libre avant de lui-même le verrouiller, après
c'être servi il déverrouille fait un notifyAll pour réveiller les clients
ou l'employer du buffet qui pourrait attendre.
(3) Cuire au stand, en se mettant dans la queue (il notifyAll le stand pour
réveiller le cuisinier), quand son plat est cuit le cuisinier le sort de la
queue (le stand fait un notifyAll pour réveiller le client)
(4) Manger
(5) Sort, en faisant un notifyAll sur le restaurant pour prévenir tout autre
thread client attendant pour entrer
Le run du cuisinier, fait une boucle pour vérifier si un client est présent en
tête de la queue du stand de cuisson. Si oui, alors il fait cuire son plat.
Une fois fini, il sort le client le la queue et fait faire un notifyAll au stand
pour prévenir le client que son plat est fini.
L'employer du buffet fait le tour de chaque compartiment du buffet, s'il y a un
un buffet à moins de 100g alors il vérifie qu'il est libre (se met en wait sinon)
et le remplit.

10
src/Au_Wok.java Normal file
View File

@@ -0,0 +1,10 @@
public class Au_Wok {
public static void main(String[] args){
Client[] clients= new Client[30];
Restaurant restaurant =new Restaurant();
for(int i=0; i<clients.length; i++){
clients[i] = new Client(i,restaurant);
clients[i].start();
}
}
}

View File

@@ -1,18 +0,0 @@
public class Au_Woke {
private Client[] clients;
private Restaurant restaurant;
Au_Woke(){
clients= new Client[4];
restaurant =new Restaurant();
restaurant.cuisinier.start();
restaurant.employeB.start();
for(int i=0; i<clients.length; i++){
clients[i] = new Client(i,restaurant);
clients[i].start();
}
}
public static void main(String[] args){
new Au_Woke();
}
}

View File

@@ -10,7 +10,6 @@ public class Client extends Thread {
this.id = id;
}
public void run(){
try{
entrer();
@@ -44,7 +43,7 @@ public class Client extends Thread {
e.printStackTrace();
}
compartiment.setLibre(true);
System.out.println("Client " + this.id + " : a pris une portion de " + compartiment.getName());
System.out.println(this.getNameClient() + " : a pris une portion de " + compartiment.getName());
compartiment.notifyAll();
}
@@ -56,9 +55,8 @@ public class Client extends Thread {
while(!restaurant.libre()){
restaurant.wait();
}
System.out.println("Client " + this.id + " : est entré dans le restaurant");
System.out.println("+ " + this.getNameClient() + " : est entré dans le restaurant");
restaurant.ajouterClient();
restaurant.notifyAll();
}catch (InterruptedException e) {
e.printStackTrace();
}
@@ -67,7 +65,7 @@ public class Client extends Thread {
public void manger(){
try{
System.out.println("Client " + this.id + " : mange");
System.out.println(this.getNameClient() + " : mange");
long temps_manger =new Random().nextInt(2000 - 1000) + 1000;
Thread.sleep(temps_manger);
}catch(InterruptedException e){
@@ -77,16 +75,16 @@ public class Client extends Thread {
public void sort(){
synchronized(restaurant){
System.out.println("Client " + this.id + " : sorti");
System.out.println("- " + this.getNameClient() + " : sorti");
restaurant.diminuerClient();
restaurant.notifyAll();
}
}
public void cuir_au_stand(Stand_de_cuisson stand){
synchronized(stand){
stand.ajouter_client(this);
System.out.println("Client " + this.id + " : attend pour faire cuir son plat");
System.out.println(this.getNameClient() + " : attend pour faire cuir son plat");
synchronized(stand){
try{
while(stand.containsClient(this)){
stand.wait();
@@ -94,24 +92,12 @@ public class Client extends Thread {
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("Client " + this.id + " : le plat est cuit");
}
System.out.println(this.getNameClient() + " : le plat est cuit");
/*
try{
while (stand.attendClient()!= this) { //Verify s'il est meme client
wait();
}
System.out.println("Client " + this.id + " : fait cuir son plat");
stand.finir_cuit(this);
}catch (InterruptedException e){
e.printStackTrace();
}
*/
}
}
public String getNameClient(){
return ("client " + this.id);
return ("Client " + this.id);
}
}

View File

@@ -10,12 +10,25 @@ public class Cuisinier extends Thread {
public void run(){
while(Thread.currentThread().isDaemon()){
attendreClient();
}
}
private void attendreClient(){
Client client = stand.getClient();
if(client!= null){
synchronized(stand){
try{
while(client==null){
stand.wait();
client= stand.getClient();
}
}
catch(InterruptedException e){
e.printStackTrace();
}
}
faire_cuire(client);
}
}
}
public void faire_cuire(Client client){
System.out.println("Cuisinier : fait cuire le plat de " + client.getNameClient()) ;
@@ -27,7 +40,7 @@ public class Cuisinier extends Thread {
}
System.out.println("Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ;
stand.nextClient();
stand.finir_cuit(client);
stand.finir_cuit();
}
}

View File

@@ -35,9 +35,12 @@ public class Restaurant {
this.buffet[2] = new Compartiment("légume");
this.buffet[3] = new Compartiment("nouille");
this.employeB = new Employe_du_buffet(buffet);
this.stand_de_cuisson = new Stand_de_cuisson();
this.employeB = new Employe_du_buffet(buffet);
this.cuisinier = new Cuisinier(stand_de_cuisson);
this.employeB.start();
this.cuisinier.start();
}
public synchronized boolean libre(){
return (MAX_CLIENTS>nbClient);

View File

@@ -4,19 +4,6 @@ import java.util.Queue;
public class Stand_de_cuisson {
private Queue<Client> clients = new LinkedList<Client>();
/*
* retourne le client qui est en tête de queue et le supprime de la queue
*/
public synchronized Client getNextClient2(){
while(clients.isEmpty()){
try{
wait();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
return clients.poll();
}
//get le client en tête de queue
public synchronized Client getClient(){
@@ -28,13 +15,13 @@ public class Stand_de_cuisson {
clients.poll();
}
public synchronized void finir_cuit(Client client){
public synchronized void finir_cuit(){
notifyAll();
}
public synchronized void ajouter_client(Client client){
clients.add(client);
notify();
notifyAll();
}
public synchronized boolean containsClient(Client client){