Introduction
In this project you are asked to solve two thread synchronization problems using semaphores. The following sections describe each of the problems and provide an unsynchronized version of the program from which you can start. In addition to the starter programs provided below, you will also need to use the Semaphore
class that we have discussed to implement your solutions.
The Sandwich Shop Problem
The sandwich shop in this problem is very small. It has a single employee and there is room for only one customer to be in the shop at a time. This problem contains two types of threads. There is a single thread modeling an employee and there are multiple threads modeling customers.
The employee waits in the shop until a customer places an order. Once a customer places an order the employee takes a random amount of time to make the sandwich. When the sandwich is made, the employee carries the sandwich to the cash register and waits for the customer to pay. Once the customer pays, the employee waits for the next order.
Each customer thread takes a random amount of time to travel to the sandwich shop. After traveling the the customer arrives at the sandwich shop. If the shop is not empty the customer must wait until the shop is empty before entering. The sandwiches are excellent, so the customer will wait as long as it takes to get into the shop. When the shop is empty, one of the waiting customers enters the shop and places an order. The customer then browses around the shop for a random amount of time. After browsing, the customer goes to the cash register and waits for the employee to bring the sandwich. When the employee arrives at the cash register, the cutomer pays for the sandwich and leaves the shop.
Your solution for the employee will augment the run
methods in the EmployeeThread
class and the CustomerThreaad
class to enforce the synchronization constraints just described. Each of these run
methods currently contains method calls that indicate what the thread is doing and provide the random waits that have been described. Once synchronized these methods must be invoked as described below.
The EmployeeThread
must invoke the method waitForCustomer()
to indicate that it is waiting for a customer to order. It must invoke the makeSandwich()
method when it begins making a sandwich for the customer. When the sandwich is finished being made, the EmployeeThread
must invoke the atCashRegister()
method to indicate that the employee is waiting for payment. Finally the EmployeeThread
must invoke the PaymentAccepted()
method to indicate that the customer's payment has been taken.
The CustomerThread
must invoke the method travelToShop()
when it is first started. When the customer arrives at the shop it must invoke the arriveAtShop()
method. When the shop is empty and the customer has entered it must invoke the placeOrder()
method. After the order has been placed, the CustomerThread
must inoke the browseShop()
method. When the customer is finished browsing it must invoke the atRegister()
method. Finally, after the employee has taken the customer's payment the CustomerThread
must invoke the leaveShop()
method.
The starter code for this problem is provided in the SandwichShop.java file. This starter code provides unsynchronized versions of the EmployeeThread
and the CustomerThread
that display nicely formatted output. Be sure to run this code and understand the output before you start making modifications to the code. The only modifications that you may make to the provided code are to add public static
variables to the SandwichShop
class and to add synchornization code, using semaphores, to the run
methods within the EmployeeThread
and CustomerThread
classes.
The Roller Coaster Problem
In the roller coaster problem there are two types of threads. There is one TrainThread
that models the roller coaster train and numerous PassengerThreads
that model the passengers. These threads should behave as described below.
The thread for the train will wait while passengers load. When the train is full, and only when it is full, it will run a loop around the track. After completing a loop around the track it will wait while all of the passengers unload. Once the passengers unload, the train will again begin loading passengers.
The threads for the passengers will each wander around the amusement park for a random amount of time before getting in line for the roller coasters. Once in line a passenger waits for an opportunity to get on the train. Once on the train a passenger will wait until the train runs its loop around the track and begins unloading. Note: The "line" in this case need not necessarily be first-come-first-served. However, those passengers that get on a given train must be the same ones that get off of that train.
The starter code for this problem is provided in the RollerCoaster.java file. As with the starter code for the Sandwich Shop Problem, the provided code for the Roller Coaster Problem contains method calls that must be invoked to indicate the current state of the TrainThread
and each PassengerThread
. Also like the Sandwich Shop Problem, the only modifications that you may make to the provided code are to add public static
variables to the RollerCoaster
class and to add synchornization code, using semaphores, to the run
methods within the PassengerThread
and TrainThread
classes.
Submitting Your Solutions
Acknowledgment: this content is essentially identical to that developed by Professor Grant Braught for the Spring 2006 Operating Systems course, and I'm grateful for his permission to use it.