The Producer/Consumer Model in Java
This assignment has five parts:
- Implement a bounded Queue class that takes the size
as a parameter to the constructor. The enqueue and
dequeue methods insert and remove Java Objects,
respectively. They must be synchronized to avoid race conditions
between the producer and consumer.
- Write a
Producer class that generates random data
(or reads it from a file) and enqueues that data into the queue. Also
write a Consumer class that extends class
Thread. The Consumer attempts to dequeue
elements from the queue and prints the results to the screen. The
Producer and Consumer should either extend
class Thread or implement interface
Runnable.
- Use notifyAll and wait to prevent deadlocks
between the
Producer and Consumer. In
addition, integrate a timed wait
mechanism into the enqueue and dequeue
methods so that they will throw a TimeoutException if the
methods take too long to execute.
- Add command line arguments to have multiple
Producer
and Consumer objects to test the effectiveness of the
bounded Queue.
- Measure the amount of time required to send n message from
producers to consumers and generate a metric that indicates the time
spent to process each message.
Script to run the Thread demo.
References
Flanagan, David; Java in a Nutshell Section 9 on Advanced Threads
Gosling, Arnold; The Java Programming Language Chapter 9 on Threads
The Java tutorial has a chapter on
Threads of Control
The Javasoft Java API Documentation on java.lang.thread
General help, the Java language and API documentation on
the JavaSoft server.
Using Solaris Threads: Java Runtime Experiences