parent
32db15a67c
commit
85ad012d0f
@ -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");
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue