correction Employe du buffet + Stand de cuisson refait avec un notify et wait
This commit is contained in:
13
rapport.txt
13
rapport.txt
@@ -16,15 +16,16 @@ Le run de Client va suivre ces étapes :
|
||||
(5) Sort, en faisant un notifyAll sur le restaurant pour prévenir tout autre
|
||||
thread client attendant pour entrer
|
||||
|
||||
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.
|
||||
L'employer du buffet fait le tour de chaque Compartiment du Buffet, s'il y a un
|
||||
un compartiment à moins de 100g alors il vérifie qu'il est libre (se met en wait sinon)
|
||||
et le remplit. Après avoir fait un tour il fait bufet.sleep, le buffet le reveillera
|
||||
après qu'un client se soit servi.
|
||||
|
||||
|
||||
Difficulté :
|
||||
Nous n'avons pas vraiment rencontré de difficulté, on a juste eu parfois quelque
|
||||
oublie (notifyAll manquant par exemple).
|
||||
On avait aussi commencé dans Stand_de_cuisson par faire une Queue, on a ensuite
|
||||
décidé de simplement synchroniser le stand de cuisson et le client, qui est au final beaucoup plus simple, mais le verrou
|
||||
n'est plus équitable, ce n'est plus le Client qui attend depuis le plus long temps
|
||||
qui est prioritaire.
|
||||
décidé de simplement synchroniser le stand de cuisson et le client, qui est au final
|
||||
beaucoup plus simple, mais le verrou n'est plus équitable, ce n'est plus le Client
|
||||
qui attend depuis le plus long temps qui est prioritaire.
|
||||
21
src/Buffet.java
Normal file
21
src/Buffet.java
Normal file
@@ -0,0 +1,21 @@
|
||||
public class Buffet {
|
||||
|
||||
private Compartiment compartiment[];
|
||||
|
||||
Buffet(){
|
||||
compartiment = new Compartiment[4];
|
||||
this.compartiment[0] = new Compartiment("poisson");
|
||||
this.compartiment[1] = new Compartiment("viande");
|
||||
this.compartiment[2] = new Compartiment("légume");
|
||||
this.compartiment[3] = new Compartiment("nouille");
|
||||
}
|
||||
|
||||
public Compartiment getCompartiment(int i){
|
||||
return compartiment[i];
|
||||
}
|
||||
|
||||
public int length(){
|
||||
return compartiment.length;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,8 +13,8 @@ public class Client extends Thread {
|
||||
public void run(){
|
||||
try{
|
||||
entrer();
|
||||
for(int i = 0; i<this.restaurant.buffet.length; i++){
|
||||
prendre_portion(this.restaurant.buffet[i]);
|
||||
for(int i = 0; i<this.restaurant.buffet.length(); i++){
|
||||
prendre_portion(i);
|
||||
}
|
||||
cuir_au_stand(restaurant.stand_de_cuisson);
|
||||
manger();
|
||||
@@ -24,7 +24,8 @@ public class Client extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
public void prendre_portion(Compartiment compartiment){
|
||||
public void prendre_portion(int i){
|
||||
Compartiment compartiment = this.restaurant.buffet.getCompartiment(i);
|
||||
synchronized(compartiment){
|
||||
int r =new Random().nextInt(MAX_PORTION);
|
||||
try{
|
||||
@@ -47,6 +48,9 @@ public class Client extends Thread {
|
||||
|
||||
compartiment.notifyAll();
|
||||
}
|
||||
synchronized(this.restaurant.buffet){
|
||||
this.restaurant.buffet.notify();
|
||||
}
|
||||
}
|
||||
|
||||
public void entrer(){
|
||||
@@ -59,7 +63,7 @@ public class Client extends Thread {
|
||||
restaurant.ajouterClient();
|
||||
}catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,10 +88,19 @@ public class Client extends Thread {
|
||||
public void cuir_au_stand(Stand_de_cuisson stand){
|
||||
System.out.println(this.getNameClient() + " : attend pour faire cuir son plat");
|
||||
synchronized(stand){
|
||||
stand.faire_cuire_plat(this);
|
||||
try{
|
||||
while(stand.getClient()!=null){
|
||||
stand.wait();
|
||||
}
|
||||
stand.entrerDansLaQueue(this);
|
||||
while(stand.getClient()==this){
|
||||
stand.wait();
|
||||
}
|
||||
}catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println(this.getNameClient() + " : le plat est cuit");
|
||||
|
||||
}
|
||||
|
||||
public String getNameClient(){
|
||||
|
||||
@@ -5,7 +5,7 @@ public class Compartiment{
|
||||
private boolean libre;
|
||||
|
||||
Compartiment(String name){
|
||||
this.quantite_courant = 500;
|
||||
this.quantite_courant = 200;
|
||||
this.name = name;
|
||||
this.libre = true;
|
||||
}
|
||||
@@ -17,6 +17,7 @@ public class Compartiment{
|
||||
|
||||
synchronized void servir(int qte){
|
||||
quantite_courant -= qte;
|
||||
notifyAll(); //pour que l'employé du buffet vérifit le buffet
|
||||
}
|
||||
|
||||
public int getQuantite(){
|
||||
|
||||
@@ -1,9 +1,29 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class Cuisinier extends Thread {
|
||||
Stand_de_cuisson stand;
|
||||
|
||||
public Cuisinier(){
|
||||
public Cuisinier(Stand_de_cuisson stand){
|
||||
this.setDaemon(true);
|
||||
this.stand = stand;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
synchronized(stand){
|
||||
while(Thread.currentThread().isDaemon()){
|
||||
Client client = stand.getClient();
|
||||
if(client!=null){
|
||||
faire_cuire(client);
|
||||
}
|
||||
else{
|
||||
try {
|
||||
stand.wait();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void faire_cuire(Client client){
|
||||
@@ -15,5 +35,6 @@ public class Cuisinier extends Thread {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("# Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ;
|
||||
stand.cuissonFini();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,34 @@
|
||||
public class Employe_du_buffet extends Thread{
|
||||
|
||||
public Compartiment[] buffet;
|
||||
public Buffet buffet;
|
||||
|
||||
public Employe_du_buffet(Compartiment[] buffet){
|
||||
public Employe_du_buffet(Buffet buffet){
|
||||
this.buffet = buffet;
|
||||
this.setDaemon(true);
|
||||
}
|
||||
|
||||
public void run(){
|
||||
while(Thread.currentThread().isDaemon()){
|
||||
for(int i = 0; i<buffet.length;i++){
|
||||
if(buffet[i].getQuantite()<100){
|
||||
try {
|
||||
while(!buffet[i].isLibre()){
|
||||
buffet[i].wait();
|
||||
synchronized(buffet){
|
||||
while(Thread.currentThread().isDaemon()){
|
||||
for(int i = 0; i<buffet.length();i++){
|
||||
Compartiment compartiment = buffet.getCompartiment(i);
|
||||
|
||||
if(compartiment.getQuantite()<100){
|
||||
try {
|
||||
while(!compartiment.isLibre()){
|
||||
compartiment.wait();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Employe Buffer : remplie de stand " + compartiment.getName() + ", il ne restait plus que " + compartiment.getQuantite() +" g");
|
||||
compartiment.remplir();
|
||||
}
|
||||
System.out.println("Employe Buffer : remplie de stand " + buffet[i].getName() + ", il ne restait plus que " + buffet[i].getQuantite() +" g");
|
||||
buffet[i].remplir();
|
||||
}
|
||||
try {
|
||||
buffet.wait();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
public class Restaurant {
|
||||
protected final int MAX_CLIENTS = 20;
|
||||
|
||||
public int nbClient;
|
||||
private int nbClient;
|
||||
|
||||
public Compartiment[] buffet = new Compartiment[4];
|
||||
public Buffet buffet;
|
||||
public Employe_du_buffet employeB;
|
||||
public Stand_de_cuisson stand_de_cuisson;
|
||||
public Cuisinier cuisinier;
|
||||
@@ -11,16 +11,12 @@ public class Restaurant {
|
||||
|
||||
public Restaurant(){
|
||||
this.nbClient=0;
|
||||
this.buffet[0] = new Compartiment("poisson");
|
||||
this.buffet[1] = new Compartiment("viande");
|
||||
this.buffet[2] = new Compartiment("légume");
|
||||
this.buffet[3] = new Compartiment("nouille");
|
||||
|
||||
|
||||
this.buffet = new Buffet();
|
||||
this.stand_de_cuisson = new Stand_de_cuisson();
|
||||
this.employeB = new Employe_du_buffet(buffet);
|
||||
this.cuisinier = new Cuisinier();
|
||||
this.stand_de_cuisson = new Stand_de_cuisson(cuisinier);
|
||||
this.cuisinier = new Cuisinier(stand_de_cuisson);
|
||||
this.employeB.start();
|
||||
this.cuisinier.start();
|
||||
}
|
||||
public synchronized boolean libre(){
|
||||
return (MAX_CLIENTS>nbClient);
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
public class Stand_de_cuisson {
|
||||
|
||||
private Cuisinier cuisinier;
|
||||
|
||||
Stand_de_cuisson(Cuisinier cuisinier){
|
||||
this.cuisinier = cuisinier;
|
||||
private Client client_courant;
|
||||
|
||||
Stand_de_cuisson(){
|
||||
this.client_courant = null;
|
||||
}
|
||||
|
||||
public synchronized void faire_cuire_plat(Client client){
|
||||
this.cuisinier.faire_cuire(client);
|
||||
public void entrerDansLaQueue(Client client_courant) {
|
||||
notifyAll(); //pour réveiller de Cuisinier
|
||||
this.client_courant = client_courant;
|
||||
}
|
||||
|
||||
public Client getClient() {
|
||||
return client_courant;
|
||||
}
|
||||
|
||||
public void cuissonFini(){
|
||||
this.client_courant = null;
|
||||
notifyAll(); //prévient le client en attente, et les prochains client
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user