Question
Use this java Starter to start. This code simulates a Sandwich shop. You read in from the command line the number of Customer Threads. There
Use this java Starter to start. 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:
The Employee waits on one customer at a time.
The Employee cannot make the sandwich until the customer orders it.
After ordering the customer browses the store. After making the sandwich the employee waits at register for Customer.
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.
/**
* 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:\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
System.out.println("
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 + "\t\t\t\t\t\t\t\t\t\t\t|");
SandwichShop.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|");
SandwichShop.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|");
}
}
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("\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");
SandwichShop.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");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started