This project will make use of two classes we have seen before in the Java API:
Also use the Utilities.java class, it contains methods which simplify the processes of talking to the user in Java and allows easy Thread sleeping.
Add instrumentation to print off whenever a thread is waiting. Distinguish between waiting for insertion and waiting for retrieval.
Replace the throws InterruptedException
of each of the two methods with
a try/catch block, similar to that found in the Utilites.java sleep method:
try { // write method code here } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
Add one method called getDouble which works exactly like getInteger, but processes and returns a double instead of an int.
This class will produce Objects to place in the BoundedBuffer and will implement the Runnable interface. It should add them at time intervals following the Poisson distribution for a given lambda. A Producer must have the following methods:
Producer(BoundedBuffer bb, double lambda)
The constructor for this class, which
will set BoundedBuffer bb
as a place to insert Objects using a time
interval based on lambda
.
void run()
This method will loop until interrupted, when it will
exit gracefully. Each loop, it will
try to insert an Object into the BoundedBuffer, and then sleep for a time interval
dictated by the Poisson distribution. Use the following psedocode to find k
,
and then sleep for 100 * k
milliseconds.
threshold = e ^ (- lambda) k = 0 p = 1 do: k++ u = uniform random number between 0 and 1 p = p * u while (p >= threshold) return k - 1
This class will consume Objects from the BoundedBuffer and will implement the Runnable interface. It should remove them at time intervals following the Poisson distribution for a given lambda. A Consumer must have the following methods:
Consumer(BoundedBuffer bb, double lambda)
The constructor for this class, which
will set BoundedBuffer bb
as a place to retrieve Objects using a time
interval based on lambda
.
void run()
This method will loop until interrupted, when it will exit gracefully. Each loop, it will try to retrieve an Object into the BoundedBuffer, and then sleep for a time interval dictated by the Poisson distribution. See above Producer.java for Poisson psedocode.
BufferAccess is the driver of the program and only contains a main method. From the user, get
the number of Producers (P), number of Consumers (C), and the user's choice of lambda
.
Create a BoundedBuffer object, and then create P Producer threads and C Consumer threads
using this BoundedBuffer and the given value for lambda
. Start all the threads and
prompt the user to hit Enter when they are finished.
When the user hits Enter, interrupt and stop all the Producers and Consumers and wait for them to complete, then exit the program.
As a user, let the program run for at least one minute, recording the output from BoundedBuffer when the threads are waiting. Test out your BufferAccess program with varying values for P, C and lambda, with lambda between 2 and 20. Report on your results and the effect of different inputs on the waiting of your Producers and Consumers.
cs.centenary.edu
server.
Create a subdirectory from your csc254
directory
called project2
and make it read, write and execute privileges for only the
owner (chmod 700). You must hand in your files for: