diff --git a/src/Customer.java b/src/Customer.java index fa3f9c2..120504b 100644 --- a/src/Customer.java +++ b/src/Customer.java @@ -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(); } } diff --git a/src/Main.java b/src/Main.java index 614ad25..c5b88aa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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"); } } diff --git a/src/PastryChef.java b/src/PastryChef.java index 8ae3808..7d69419 100644 --- a/src/PastryChef.java +++ b/src/PastryChef.java @@ -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()); } } diff --git a/src/PastryShop.java b/src/PastryShop.java index a297dc9..61fad75 100644 --- a/src/PastryShop.java +++ b/src/PastryShop.java @@ -1,53 +1,26 @@ -import java.util.ArrayList; -import java.util.Arrays; +import java.util.concurrent.ArrayBlockingQueue; -public class PastryShop extends ArrayList { - private static final int MAX_STOCK_SIZE = 20; +public class PastryShop extends ArrayBlockingQueue { - 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())); - } - } - while (getStock() < MAX_STOCK_SIZE) { - this.add(pastry); - System.out.println("A Pastry was put in the shop -- " + getStock() + " remaining"); + public PastryShop(int capacity, boolean isFair) { + super(capacity, isFair); + } - 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) { - 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())); - } - + public Pastry get() { + Pastry res = null; + try { + 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(); - } }