🔥🧑‍💻🔥 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; private final PastryShop shop;
public Customer(PastryShop shop) { public Customer(PastryShop shop) {
System.out.println("I'm hungry");
this.shop = shop; this.shop = shop;
} }
@Override @Override
public void run() { public void run() {
System.out.println("customer running...");
shop.get(); shop.get();
} }
} }

@ -1,16 +1,21 @@
public class Main { public class Main {
public static void main(String[] args) { 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)); new Thread(new PastryChef(shop)).start();
Thread consumer1 = new Thread(new Customer(shop)); new Thread(new PastryChef(shop)).start();
Thread consumer2 = new Thread(new Customer(shop)); new Thread(new PastryChef(shop)).start();
Thread consumer3 = new Thread(new Customer(shop));
producer1.start(); int c = 1;
consumer1.start(); while(c++ < 110) {
consumer2.start(); new Thread(new Customer(shop)).start();
consumer3.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; private final PastryShop shop;
public PastryChef(PastryShop shop) { public PastryChef(PastryShop shop) {
System.out.println("I'm a chef");
this.shop = shop; this.shop = shop;
} }
@Override @Override
public void run() { public void run() {
System.out.println("chef running...");
shop.put(new Pastry()); shop.put(new Pastry());
} }
} }

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

Loading…
Cancel
Save