Read this entire document before you start working so you can get a feel for how the classes fit together.
In project 5, all seating reservations were made in a single-threaded manner. That is, if someone started making a reservation or canceling one, the program would not let a second person make or cancel a reservation until the first one was processed. In this project, you will change the way this works so that seats are reserved in a multithreaded manner: every time someone wants to reserve or cancel a block of seats, a new thread is generated that will run the reservation or cancellation code. The reason for this is so that when someone wants to reserve a block of seats that can't be accommodated, that thread will automatically block (pause) until enough seats become available.
You will implement this functionality with synchronized
blocks and/or methods and
by using wait()
and notifyAll()
to allow threads to signal each other.
Furthermore, since you will have multiple reservation/cancellation threads all running
simultaneously, I have constructed a class called TheaterDisplay
that shows a live
seating chart of the theater. This class, however, does not automatically monitor
the theater for live updates, so whenever the theater adds or loses a reservation, you should call
refresh()
on the TheaterDisplay object to allow the changes to be seen.
ReservationThread
class handles the task of making a new reservation.
The constructor accepts all the information necessary to make a reservation --- you should save this information to whatever
variables you determine are necessary. You should overload the run()
method so that when
the thread is start()
'ed, it will
keep trying to make the reservation until it succeeds. Hint: use wait()
and have
another thread send the notifyAll()
signal when the ReservationThread
should try again.
In other words, you will need to write the constructor and the run()
method.
CancellationThread
class handles the task of canceling an existing reservation.
(There's no particular reason for this to be a thread, since canceling
happens immediately, but it's more fun that way.)
This class's constructor accepts all the information necessary to cancel a reservation, and when the thread is start()
'ed, it should
cancel the reservation.
You will need to write the constructor and the run()
method.
In particular, you do not need to make any changes to the Ticketmaster
class, but
you should look at it so you understand how it works. It's not that much different than
project 5, except it starts threads for reserve and cancel instead of how project 5
did it, and the lookup command is commented out (though you can put it back if you really want).
The code should compile and run "out of the box," even without any changes on your part (though it won't make or cancel any reservations).
reserve kirlin 3 reserve sanders 4 cancel kirlinThe result should be kirlin gets seats 0-2, then after kirlin is canceled, sanders automatically steps in and grabs seats 0-3.
reserve sheard 9 reserve gottlieb 5 reserve seaton 4 reserve mouron 3 reserve dunwell 2 reserve bodine 1Here, the result is harder to predict. After all these command have been entered, sheard and bodine should have all ten seats (sheard 9 and bodine 1). If you then cancel sheard, a bunch of other reservations should take the empty seats automatically, but someone will still be left out. Start canceling people and make sure the last waiting reservation fills the gap as soon as the gap is big enough.