Monitor for Santa, His Reindeer, and His Elves

In this simulation, there is one Santa Claus thread, 20 reindeer threads, and 11 elf threads. In the beginning, the reindeer threads are in the barn and the elf threads are in the toy factory. Santa Claus waits in his shop up at the North Pole, and can only be unblocked by either

When nine reindeer show up, Santa and his reindeer deliver presents (simulated with a nap()) around the world for a while. When three elves are having their problems solved by Santa in his shop, any other elf wishing to visit Santa's shop for help must wait outside the shop entrance (a) for the three elves currently in the shop to exit the shop, and (b) for two other elves to form a group of three to enter the shop together for help. If Santa awakens to see three elves in his shop for help and nine reindeer showing up, the reindeer get priority. When Santa and his reindeer return from delivering presents, the reindeer return to the heated barn and munch food and warm up (simulated with a nap()) for a while. Santa helps three elves solve toy assembly problems with a nap(). Elves work individually assemblying a toy in the factory with a nap(). Each elf periodically needs Santa's help at random times assemblying a toy.

As in all our programming assignments, use command line arguments to set the simulation parameters.

Remember that Java monitors are `signal and continue' so be sure to think about the ramifications of this as you design your monitor, in particular ``barging''. No semaphores and no busy waiting allowed. Do not use nap() within a synchronized monitor method.

For the purposes of this assignment you may assume that each notify() awakens the thread that has been waiting the longest. But it is unpredictable when that thread will get back into the monitor. It is possible for other threads that have called a monitor synchronized method to ``barge'' in ahead of it. Also, notifyAll() awakens all the waiting threads and it is unpredictable which thread will get back into the monitor first and whether another thread will ``barge'' in ahead of it.

Here is one way to approach a monitor programming assignment. Think of the monitor as maintaining the state of the system in integer and boolean data fields and the synchronized methods as making atomic changes to the state of the system. If a thread enters the monitor and needs a resource that is currently unavailable or needs to synchronize with another thread, then

while (condition) wait();
Any time a thread changes a variable that may affect the condition some other thread is waiting on, do a notifyAll() before exiting the method.

Animate your program using First do the base assignment as specified above and turn it in. Then do an animation of it as a separate program.