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:
RequestGenerator
is the producer class. It runs continuously in its own thread and generates a new request at random intervals.RequestHandler
is the consumer class. It also runs continuously in its own thread and handles requests.HttpDelayRequest
is 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)RequestManager
coordinates the simulation by managing all the threads and keeping track of all requests in a blocking queue.RequestMonitor
is a special class that monitors the queue and reports how many requests are awaiting execution (it reports every 5 seconds).
Instructions
- Open the
Simulation
class and observe the parameters. You can specify how manyRequestGenerator
s andRequestHandler
s to create in the simulation and an interval that specifies how often eachRequestGenerator
generates a request (initially it is between 2 and 5 seconds). - Run the simulation by running the
Simulation
class with the provided parameters and observe the output. TheRequestGenerator
s andRequestHandler
s report on each action and theRequestMonitor
periodically 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 |