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

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,41 +75,29 @@ 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");
try{
while(stand.containsClient(this)){
stand.wait();
System.out.println(this.getNameClient() + " : attend pour faire cuir son plat");
synchronized(stand){
try{
while(stand.containsClient(this)){
stand.wait();
}
}catch (InterruptedException e){
e.printStackTrace();
}
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("Client " + this.id + " : 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();
}
*/
}
System.out.println(this.getNameClient() + " : le plat est cuit");
}
public String getNameClient(){
return ("client " + this.id);
return ("Client " + this.id);
}
}

View File

@@ -10,13 +10,26 @@ public class Cuisinier extends Thread {
public void run(){
while(Thread.currentThread().isDaemon()){
Client client= stand.getClient();
if(client!= null){
faire_cuire(client);
}
attendreClient();
}
}
private void attendreClient(){
Client client = stand.getClient();
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()) ;
try{
@@ -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){