🔥🧑‍💻🔥 TP2-2

main
Alexis Drai 2 years ago
parent 32db15a67c
commit 85ad012d0f

@ -2,13 +2,11 @@ public class Customer implements Runnable {
private final PastryShop shop;
public Customer(PastryShop shop) {
System.out.println("I'm hungry");
this.shop = shop;
}
@Override
public void run() {
System.out.println("customer running...");
shop.get();
}
}

@ -1,16 +1,21 @@
public class Main {
public static void main(String[] args) {
PastryShop shop = new PastryShop();
boolean isFair = false;
PastryShop shop = new PastryShop(20, isFair);
Thread producer1 = new Thread(new PastryChef(shop));
Thread consumer1 = new Thread(new Customer(shop));
Thread consumer2 = new Thread(new Customer(shop));
Thread consumer3 = new Thread(new Customer(shop));
new Thread(new PastryChef(shop)).start();
new Thread(new PastryChef(shop)).start();
new Thread(new PastryChef(shop)).start();
producer1.start();
consumer1.start();
consumer2.start();
consumer3.start();
int c = 1;
while(c++ < 110) {
new Thread(new Customer(shop)).start();
}
new Thread(new PastryChef(shop)).start();
new Thread(new PastryChef(shop)).start();
System.out.println(c + " customers made");
}
}

@ -2,13 +2,11 @@ public class PastryChef implements Runnable {
private final PastryShop shop;
public PastryChef(PastryShop shop) {
System.out.println("I'm a chef");
this.shop = shop;
}
@Override
public void run() {
System.out.println("chef running...");
shop.put(new Pastry());
}
}

@ -1,53 +1,26 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;
public class PastryShop extends ArrayList<Pastry> {
private static final int MAX_STOCK_SIZE = 20;
public class PastryShop extends ArrayBlockingQueue<Pastry> {
public synchronized void put(Pastry pastry) {
while (getStock() == MAX_STOCK_SIZE) {
try {
System.out.println(getStock() + " pastries, producer blocked");
this.wait();
} catch (InterruptedException ex) {
// TODO what else can we do here?
System.err.println(Arrays.toString(ex.getStackTrace()));
}
public PastryShop(int capacity, boolean isFair) {
super(capacity, isFair);
}
while (getStock() < MAX_STOCK_SIZE) {
this.add(pastry);
System.out.println("A Pastry was put in the shop -- " + getStock() + " remaining");
this.notify();
public void put(Pastry pastry) {
while (this.offer(pastry)) { // fills up to max (offer -> false when full)
System.out.println("+");
}
}
public synchronized Pastry get() {
while (getStock() == 0) {
public Pastry get() {
Pastry res = null;
try {
System.out.println(getStock() + " pastries, consumer blocked");
this.wait();
} catch (InterruptedException ex) {
// TODO what else can we do here?
System.err.println(Arrays.toString(ex.getStackTrace()));
}
res = this.take();
} catch (InterruptedException e) {
e.printStackTrace();
// trust the process to get back on track on its own..?
}
Pastry res = this.get(0);
this.remove(res);
System.out.println("A Pastry was got from the shop -- " + getStock() + " remaining");
this.notify();
System.out.println("-");
return res;
}
public int getStock() {
return this.size();
}
public synchronized void prime() {
this.notify();
}
}

Loading…
Cancel
Save