PDC@UNL
Distributed Computing - Java Demonstration
Code Demonstration - Java version
We have written a producer-consumer simulation in Java that is available in the same Eclipse project available at https://github.com/cbourke/PDCatUNL
We have provided several classes (in unl.soc.producer_consumer) that
simulate a producer-consumer scenario:
RequestGeneratoris the producer class. It runs continuously in its own thread and generates a new request at random intervals.RequestHandleris the consumer class. It also runs continuously in its own thread and handles requests.HttpDelayRequestis the actual request class that is generated and handled. It simply makes a dummy request to a webserver that "simulates" work by making an artificial delay between 2 and 5 seconds (randomly)RequestManagercoordinates the simulation by managing all the threads and keeping track of all requests in a blocking queue.RequestMonitoris a special class that monitors the queue and reports how many requests are awaiting execution (it reports every 5 seconds).
Instructions
- Open the
Simulationclass and observe the parameters. You can specify how manyRequestGenerators andRequestHandlers to create in the simulation and an interval that specifies how often eachRequestGeneratorgenerates a request (initially it is between 2 and 5 seconds). - Run the simulation by running the
Simulationclass with the provided parameters and observe the output. TheRequestGenerators andRequestHandlers report on each action and theRequestMonitorperiodically reports how many tasks are waiting in the queue. The simulation runs continuously; to stop it you'll need to kill the program manually (the stop button in Eclipse). - Run/rerun the simulation for a few minutes with the following parameters and observe and note the results.
| Number of Producers | Number of Consumers | Interval Min | Interval Max |
|---|---|---|---|
| 5 |
5 |
2 | 6 |
| 1 |
5 |
2 | 6 |
| 5 |
1 | 2 | 6 |
| 5 |
5 |
1 | 2 |
| 5 |
5 | 5 | 10 |