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