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