Thursday, September 16, 2010

Due Tuesday September 21, no later than Tuesday September 28

Extend the Producer/Consumer problem discussed in class.  The Producer creates String("1","2". etc) and puts them into a box.  The Box can hold up to a maximum number of Strings, say 4.  The Consumer gets Strings from the Box and displays them.  The strings must be written to the screen in the same order in which they are put into the box.

Design and implement Producer, Consumer, and Box classes.  Note the Box should be implemented as a Queue class.  The Queue must be "thread safe".  The Producer sleeps a random time between 1 to 100 ms before it places another item in the queue and the Consumer sleeps a random time between 1 to 75 ms before it takes another item from the Queue.  Display an appropriate message each time the Producer must wait for the Queue (when it is full) and each time the Consumer must wait for the queue (it is empty).  That is, some blocking should be observed.

You can choose either the approach of using Thread subclasses or the Runnable interface.  All exceptions should be handled in a responsible manner.

EXTRA CREDIT:"  Use multiple Producers (5) and Consumers (3).  when a Producer finishes it should output a message "I ------am done producing" and the Consumers go until there are no more Producers and the Queue is empty.

Be sure to develop good test cases.

No comments:

Post a Comment