Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use this code as a guide /** * Starter code for the Sandwich Shop Problem */ imp

ID: 3799547 • Letter: U

Question

use this code as a guide

/**

* Starter code for the Sandwich Shop Problem

*/

import java.util.concurrent.Semaphore;

public class SandwichShopStarter {

   //DECLARE YOUR STATIC SEMAPHORES HERE

   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: | Employee:");

       System.out.print("Traveling Arrived Ordering Browsing At Register Leaving");

       System.out.println(" | Waiting Sandwich Making At Register Payment 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 ");

       System.out.println(" : Number of customers.");

       System.exit(-1);

   }

   public static void randomSleep(int max) {

       try {

           Thread.sleep((int) (Math.random() * max));

       }

       catch (InterruptedException e) {

       }

   }

}

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();

        browseShop();

       atRegister();

       leaveShop();

   }

   private void travelToShop() {

       System.out.println("Customer " + id + " |");

       SandwichShop.randomSleep(1000);

   }

   private void arriveAtShop() {

       System.out.println(" Customer " + id + " |");

   }

   private void placeOrder() {

       System.out.println(" Customer " + id + " |");

   }

   private void browseShop() {

       System.out.println(" Customer " + id + " |");

       SandwichShop.randomSleep(1000);

   }

   private void atRegister() {

       System.out.println(" Customer " + id + " |");

   }

   private void leaveShop() {

       System.out.println(" Customer " + id + " |");

   }

}

class EmployeeThread extends Thread {

   public void run() {

       while (true) {

       //HERE you add the acquire and release of the semaphore

          waitForCustomer();

          makeSandwich();

          atCashRegister();

          paymentAccepted();

       }

   }

   private void waitForCustomer() {

       System.out.println(" | Employee");

   }

   private void makeSandwich() {

       System.out.println(" | Employee");

       SandwichShop.randomSleep(1000);

   }

   private void atCashRegister() {

       System.out.println(" | Employee");

   }

   private void paymentAccepted() {

       System.out.println(" | Employee");

   }

}

This code simulates a Sandwich shop. You read in from the command line the number of Customer Threads. There is just one Employee Thread.

Here are the rules:

1.The Employee waits on one customer at a time.

2. The Employee cannot make the sandwich until the customer orders it.

3. After ordering the customer browses the store. After making the sandwich the employee waits at register for Customer.

4. Customer pays and leaves and the Employee gets the next customer

Add necessary synchronization to the starter code.(Use static variables that can be shared between threads). All you should have to do is add some static synchronization variables to the run method in both the Customer and Employee thread. Your only modification should be the declaring the static Semaphores, the initialization of the Semaphores and the acquire and release of those Semaphores in the run method.

Explanation / Answer

import java.util.concurrent.Semaphore;

public class SandwichShopStarter {

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: | Employee:");

       System.out.print("Traveling Arrived Ordering Browsing At Register Leaving");

       System.out.println(" | Waiting Sandwich Making At Register Payment Accepted");

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 ");

       System.out.println(" : Number of customers.");

       System.exit(-1);

   }

   public static void randomSleep(int max) {

       try {

           Thread.sleep((int) (Math.random() * max));

       }

       catch (InterruptedException e) {

       }

   }

}

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();

        browseShop();

       atRegister();

       leaveShop();

   }

   private void travelToShop() {

       System.out.println("Customer " + id + " |");

       SandwichShop.randomSleep(1000);

   }

   private void arriveAtShop() {

       System.out.println(" Customer " + id + " |");

   }

   private void placeOrder() {

       System.out.println(" Customer " + id + " |");

   }

   private void browseShop() {

       System.out.println(" Customer " + id + " |");

       SandwichShop.randomSleep(1000);

   }

   private void atRegister() {

       System.out.println(" Customer " + id + " |");

   }

   private void leaveShop() {

       System.out.println(" Customer " + id + " |");

   }

}

class EmployeeThread extends Thread {

   public void run() {

       while (true) {

       //HERE you add the acquire and release of the semaphore

          waitForCustomer();

          makeSandwich();

          atCashRegister();

          paymentAccepted();

       }

   }

   private void waitForCustomer() {

       System.out.println(" | Employee");

   }

   private void makeSandwich() {

       System.out.println(" | Employee");

       SandwichShop.randomSleep(1000);

   }

   private void atCashRegister() {

       System.out.println(" | Employee");

   }

   private void paymentAccepted() {

       System.out.println(" | Employee");

   }

}