Two different approaches to constructing simulation programs
Assignment 4 is about the creation of a simulation program involving a system consisting of clients and shared resources. Here are two examples:
- Simulation of a bank where clients of different priority wait in line to be served by several tellers. Here is the description of the problem (available only in french), and here is a solution.
- Simulation of a machine shop and the processing of different types of products. Here is the description of the problem (version française), and here is a solution (zip file)
Comparison of the programming structure of these two simulation programs
These two programs use two very different programming structures. Here are some comments:
- The second program uses a structure where shared resources (the machines) are essentially represented by semaphores that provide for the waiting of agents (the products to be produced in the machine shop) when the machine is already occupied with another agent. There are no priorities, and FIFO waiting is not enforced. The time delays concerning the service times in the machines are programmed within the code of the agents by executing a sleep operation for the appropriate time period. The agents know in which order to machines must be visited (this depends on the type of project to be produced).
- The first program uses a structure where actions (represented by thread instances of type TimerTask) that are "scheduled" to be executed at a given time in the future by submitting them to an instance of a Timer Class which does the scheduling for the simulation program. For instance, a queue ("FileClient") defines the action of creating a new client ("ajouterClient") which will insert the next client into the queue. In this program, the main objects of the simulation do not have a run method with infinite loop, as traditional Java Threads, but they contain, instead, the definition of specific actions (in the form of TimerTask's) that are scheduled to be executed at appropriate times. This provides much flexibility and can be used for defining more complex scheduling schemes, including priorities, as compared with the programming structure of the second program.
- In both programs, special attention was given to the collection of appropriate information from the simulation. In the case of the second program, all such information is stored in an instance of the DataCollection class. In the case of the first program, this information is stored within the queues and collected by the "Terminer" TimerTask when it is scheduled at the end of the simulation period.