Multi threaded queues
Solution
Burrito.java
importjava.util.ArrayList;
importjava.util.List;
public class Burrito {
private static final int CUSTOMER_NUM = 30;
private static final int SERVER_NUM = 3;
public static void main(String[] args) {
List<Server> servers = new ArrayList<>(SERVER_NUM);
List<Customer> customers = new ArrayList<>(CUSTOMER_NUM);
for (int i = 1; i <= SERVER_NUM; i++) {
Server s = new Server(i);
s.start();
servers.add(s);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < CUSTOMER_NUM; i++) {
Customer c = new Customer();
c.start();
customers.add(c);
}
try {
for (Customer c : customers) {
c.join();
}
for (Server s : servers) {
s.finish();
s.interrupt();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Customer.java
importjava.util.Random;
importjava.util.concurrent.Semaphore;
public class Customer extends Thread {
private static final int SHOP_CAPACITY = 15;
private static Semaphore s = new Semaphore(1);
private static Semaphore shop = new Semaphore(SHOP_CAPACITY);
private static Semaphore counter = new Semaphore(1);
private static Random r = new Random(System.nanoTime());
private static long totalVisitPeople;
private long id;
private long orderNumber;
private Order order;
public void run() {
try {
joinShop();
order();
waiting();
pay();
leaveShop();
} catch (InterruptedException e) {
}
}
private void pay() throws InterruptedException {
counter.acquire();
printMsg(“paying”);
Thread.sleep(1); // waiting time simulation
counter.release();
}
private void waiting() throws InterruptedException {
printMsg(“Standing”);
order.waitFilled();
}
private void joinShop() throws InterruptedException {
printMsg(“Arrived”);
shop.acquire();
printMsg(“Entering shop with an order of ” + orderNumber + ” burrito(s)”);
}
private void order() throws InterruptedException {
OrderManagement.putOrder(order);
printMsg(“Order placed”);
}
private void leaveShop() {
printMsg(“Leaving shop”);
shop.release();
}
private void printMsg(String msg) {
System.out.println(“Customer ” + id + “: ” + msg);
}
public Customer() {
try {
s.acquire();
totalVisitPeople++;
id = totalVisitPeople;
orderNumber = Math.abs(r.nextLong()) % 20 + 1;
order = new Order(id, orderNumber);
s.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Order.java
importjava.util.concurrent.Semaphore;
public class Order {
longpeopleId;
longorderNumber;
private Semaphore s = new Semaphore(1);
public Order(long id, long order) {
peopleId = id;
orderNumber = order;
try {
s.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void waitFilled() throws InterruptedException {
s.acquire();
}
public void filled() {
s.release();
}
}
OrderManagement.java
importjava.util.ArrayList;
importjava.util.List;
importjava.util.concurrent.Semaphore;
public class OrderManagement {
private static Semaphore s = new Semaphore(1);
private static Semaphore emptyLock = new Semaphore(1);
static List<Order> orders = new ArrayList<>();
public static void putOrder(Order o) throws InterruptedException {
s.acquire();
orders.add(o);
Thread.sleep(1); // waiting time simulation
s.release();
emptyLock.release();
}
public static Order takeOrder() throws InterruptedException {
if (orders.isEmpty()) {
emptyLock.tryAcquire();
}
emptyLock.acquire();
s.acquire();
Order result = orders.get(0);
for (Order o : orders) {
if (o.orderNumber<result.orderNumber) {
result = o;
}
}
orders.remove(result);
s.release();
return result;
}
}
Server.java
public class Server extends Thread {
private long beef = Long.MAX_VALUE;
private long cheese = Long.MAX_VALUE;
private long tortilla = Long.MAX_VALUE;
privateint id;
private volatile boolean running = true;
public Server(int i) {
id = i;
}
public void finish() {
running = false;
}
public void run() {
try {
while (running) {
Order order = OrderManagement.takeOrder();
System.out.println(“Customer ” + order.peopleId + “: getting service from server ” + id);
for (int i = 0; i <order.orderNumber; i = i + 3) {
takeBeef();
takeCheese();
takeTortilla();
makeBurrito();
System.out.println(“Customer ” + order.peopleId + “: ”
+ ((i + 3) <order.orderNumber ? i + 3 : order.orderNumber) + ” burritos made”);
}
System.out.println(“Customer ” + order.peopleId + “: order is finished, prepare to pay”);
order.filled();
}
} catch (InterruptedException e) {
}
}
private void makeBurrito() {
try {
Thread.sleep(1); // waiting time simulation
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public long takeBeef() {
return beef–;
}
public long takeCheese() {
return cheese–;
}
public long takeTortilla() {
return tortilla–;
}
}