Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Starter code for the Sandwich Shop Problem
- */
- import java.util.concurrent.Semaphore;
- public class SandwichShopStarter {
- //DECLARE YOUR STATIC SEMAPHORES HERE
- private static Semaphore sema1 = new Semaphore(0);
- private static Semaphore sema2 = new Semaphore(0);
- private static int employee = 0;
- private static int customer = 0;
- public static void main(String[] args) {
- if (args.length != 1) {
- printUsage();
- }
- int num = 0;
- try {
- num = Integer.parseInt(args[0]); // number of Customers
- }
- catch (NumberFormatException e) {
- printUsage();
- }
- //INITIALIZE THE SEMAPHORES HERE THINK IF THEY START AT 0 OR 1
- System.out.println("Customer:\t\t\t\t\t\t\t\t\t\t\t| Employee:");
- System.out.print("Traveling\tArrived\t\tOrdering\tBrowsing\tAt Register\tLeaving");
- System.out.println("\t\t| Waiting\tSandwich Making\t\tAt Register\tPayment Accepted");
- System.out .println("---------------------------------------------------"
- + "---------------------------------------------+--------"
- + "-------------------------------------------------------------------");
- Thread emp = new EmployeeThread();
- emp.start();
- Thread[] custs = new Thread[num];
- for (int i = 0; i < num; i++) {
- custs[i] = new CustomerThread(i);
- custs[i].start();
- }
- for (int i = 0; i < num; i++) {
- try {
- custs[i].join();
- }
- catch (InterruptedException e) {
- }
- }
- System.exit(0);
- }
- private static void printUsage() {
- System.out.println("Usage: java SandwichShop <num>");
- System.out.println(" <num>: Number of customers.");
- System.exit(-1);
- }
- public static void randomSleep(int max) {
- try {
- Thread.sleep((int) (Math.random() * max));
- }
- catch (InterruptedException e) {
- }
- }
- private static class CustomerThread extends Thread {
- private int id;
- public CustomerThread(int id) {
- this.id = id;
- }
- public void run() {
- //HERE you add the acquire and release of the semaphore
- travelToShop();
- arriveAtShop();
- placeOrder();
- sema1.release();
- browseShop();
- try {sema2.acquire();} catch(InterruptedException e){}
- atRegister();
- sema1.release();
- try {sema2.acquire();} catch(InterruptedException e){}
- leaveShop();
- }
- private void travelToShop() {
- System.out.println("Customer " + id + "\t\t\t\t\t\t\t\t\t\t\t|");
- SandwichShopStarter.randomSleep(1000);
- }
- private void arriveAtShop() {
- System.out.println("\t\tCustomer " + id + "\t\t\t\t\t\t\t\t\t|");
- }
- private void placeOrder() {
- System.out.println("\t\t\t\tCustomer " + id + "\t\t\t\t\t\t\t|");
- }
- private void browseShop() {
- System.out.println("\t\t\t\t\t\tCustomer " + id + "\t\t\t\t\t|");
- SandwichShopStarter.randomSleep(1000);
- }
- private void atRegister() {
- System.out.println("\t\t\t\t\t\t\t\tCustomer " + id + "\t\t\t|");
- }
- private void leaveShop() {
- System.out.println("\t\t\t\t\t\t\t\t\t\tCustomer " + id + "\t|");
- }
- }
- private static class EmployeeThread extends Thread {
- public void run() {
- //HERE you add the acquire and release of the semaphore
- //try {sema1.acquire();} catch(InterruptedException e){}
- waitForCustomer();
- //insert semaphore here
- try {sema1.acquire();} catch(InterruptedException e){}
- makeSandwich();
- atCashRegister();
- sema2.release();
- try {sema1.acquire();} catch(InterruptedException e){}
- paymentAccepted();
- }
- private void waitForCustomer() {
- System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| Employee");
- }
- private void makeSandwich() {
- System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\tEmployee");
- SandwichShopStarter.randomSleep(1000);
- }
- private void atCashRegister() {
- System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\t\t\t\tEmployee");
- }
- private void paymentAccepted() {
- System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\t\t\t\t\t\tEmployee");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement