correction du stand de cuissant et cuisinier
This commit is contained in:
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal 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
2
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
public class Au_Woke {
|
||||
private Client[] clients= new Client[100];
|
||||
private Restaurant restaurant =new Restaurant();
|
||||
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(restaurant);
|
||||
clients[i] = new Client(i,restaurant);
|
||||
clients[i].start();
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ import java.util.Random;
|
||||
public class Client extends Thread {
|
||||
private int MAX_PORTION = 100;
|
||||
protected Restaurant restaurant;
|
||||
private int id;
|
||||
|
||||
public Client (Restaurant restaurant){
|
||||
public Client (int id, Restaurant restaurant){
|
||||
this.restaurant=restaurant;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,14 +29,13 @@ public class Client extends Thread {
|
||||
synchronized(compartiment){
|
||||
int r =new Random().nextInt(MAX_PORTION);
|
||||
try{
|
||||
while(r > compartiment.getQuantite()){
|
||||
while(r > compartiment.getQuantite() && !compartiment.isLibre()){ //todo compartiment déjà occupé ?
|
||||
compartiment.wait();
|
||||
}
|
||||
} catch (InterruptedException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Client " + Thread.currentThread().getId() + " : a pris une portion de " + compartiment.getName());
|
||||
|
||||
compartiment.setLibre(false);
|
||||
compartiment.servir(r);
|
||||
try{
|
||||
long temps_servir =new Random().nextInt(300 - 200) + 200;
|
||||
@@ -42,6 +43,9 @@ public class Client extends Thread {
|
||||
}catch(InterruptedException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
compartiment.setLibre(true);
|
||||
System.out.println("Client " + this.id + " : a pris une portion de " + compartiment.getName());
|
||||
|
||||
compartiment.notifyAll();
|
||||
}
|
||||
}
|
||||
@@ -52,7 +56,7 @@ public class Client extends Thread {
|
||||
while(!restaurant.libre()){
|
||||
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.notifyAll();
|
||||
}catch (InterruptedException e) {
|
||||
@@ -63,7 +67,7 @@ public class Client extends Thread {
|
||||
|
||||
public void manger(){
|
||||
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;
|
||||
Thread.sleep(temps_manger);
|
||||
}catch(InterruptedException e){
|
||||
@@ -73,7 +77,7 @@ public class Client extends Thread {
|
||||
|
||||
public void sort(){
|
||||
synchronized(restaurant){
|
||||
System.out.println("Client " + Thread.currentThread().getId() + " : sortir");
|
||||
System.out.println("Client " + this.id + " : sorti");
|
||||
restaurant.diminuerClient();
|
||||
restaurant.notifyAll();
|
||||
}
|
||||
@@ -82,15 +86,32 @@ public class Client extends Thread {
|
||||
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();
|
||||
}
|
||||
}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 " + Thread.currentThread().getId() + " : fait cuir son plat");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ public class Compartiment{
|
||||
protected final int QUANTITE_MAX = 1000;
|
||||
private int quantite_courant;
|
||||
private String name;
|
||||
private boolean libre;
|
||||
|
||||
Compartiment(String name){
|
||||
quantite_courant = 500;
|
||||
@@ -23,4 +24,12 @@ public class Compartiment{
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
synchronized boolean isLibre(){
|
||||
return this.libre;
|
||||
}
|
||||
|
||||
synchronized void setLibre(boolean libre){
|
||||
this.libre = libre;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ public class Cuisinier extends Thread {
|
||||
|
||||
public void run(){
|
||||
while(Thread.currentThread().isDaemon()){
|
||||
//while(true){
|
||||
Client client= stand.attendClient();
|
||||
Client client= stand.getClient();
|
||||
if(client!= null){
|
||||
faire_cuire(client);
|
||||
}
|
||||
@@ -19,14 +18,17 @@ public class Cuisinier extends Thread {
|
||||
}
|
||||
|
||||
public void faire_cuire(Client client){
|
||||
System.out.println("Cuisinier : fait cuire le plat de " + client.getNameClient()) ;
|
||||
try{
|
||||
long temps_cuire =new Random().nextInt(300 - 100) + 100;
|
||||
Thread.sleep(temps_cuire);
|
||||
//long temps_cuire =new Random().nextInt(300 - 100) + 100;
|
||||
//Thread.sleep(temps_cuire);
|
||||
Thread.sleep(3000);
|
||||
}catch(InterruptedException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Cuisinier : a fini de cuire le plat de " + client.getNameClient()) ;
|
||||
stand.nextClient();
|
||||
stand.finir_cuit(client);
|
||||
stand.notifyAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,13 @@ public class Employe_du_buffet extends Thread{
|
||||
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();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
System.out.println("Employe Buffer : remplie de stand " + i);
|
||||
buffet[i].remplir();
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class Restaurant {
|
||||
*/
|
||||
public Restaurant(){
|
||||
this.nbClient=0;
|
||||
this.buffet[0] = new Compartiment("posson");
|
||||
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");
|
||||
@@ -38,7 +38,6 @@ public class Restaurant {
|
||||
this.employeB = new Employe_du_buffet(buffet);
|
||||
this.stand_de_cuisson = new Stand_de_cuisson();
|
||||
this.cuisinier = new Cuisinier(stand_de_cuisson);
|
||||
|
||||
}
|
||||
public synchronized boolean libre(){
|
||||
return (MAX_CLIENTS>nbClient);
|
||||
|
||||
@@ -3,7 +3,11 @@ import java.util.Queue;
|
||||
|
||||
public class Stand_de_cuisson {
|
||||
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()){
|
||||
try{
|
||||
wait();
|
||||
@@ -13,6 +17,17 @@ public class Stand_de_cuisson {
|
||||
}
|
||||
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){
|
||||
notifyAll();
|
||||
}
|
||||
@@ -21,4 +36,8 @@ public class Stand_de_cuisson {
|
||||
clients.add(client);
|
||||
notify();
|
||||
}
|
||||
|
||||
public synchronized boolean containsClient(Client client){
|
||||
return this.clients.contains(client);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user