20-CS-694 Advanced Programming Techniques Spring 2012
Hints for Homework Assignment 3 (Alt)

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

Streams

Due: April 26, 2012 (submit instructions: here)

GateStream:
    1.   The GateStream class should extend the MultiStream class so it can make use of all the services for taking care of multiple consumers that MultiStream offers.
 
    2. A GateStream object is a producer and, in this case, TestProducer should subclass GateStream.
 
    3. A TestProducer object needs to know the special consumer that is the "gatekeeper." But a GateStream also needs this information. Hence, the the constructor of TestProducer should start out like this:
   TestProducer (Thread gate, ...) {
      super(gate, ...);
      ...
   }
and the GateStream class should start out like this:
   public class GateStream extends MultiStream {
      Thread gate;
      ...					
      GateStream (Thread gate, ...) {
         super(...);
         this.gate = gate;
         ...
      }
 
    4. GateStream needs to override the putIt() and next() methods of the MultiStream class to provide the service whereby requests for tokens from multiple consumers will not be satisfied until a request is made by one particular consumer. The putIt() method will have a notifyAll so that all of the gated consumer threads will be started when the gatekeeper arrives. But the notifyAll is not executed until the gatekeeper thread arrives so the producer must enter a wait block if it puts a token into the stream before that happens. The next() method will compare the consumer thread issuing an incoming request for a token with the gatekeeper thread. If there is not a match, the thread enters a wait. If there is a match, a notify wakes up the producer (if it is waiting in putIt()) which then executes the notifyAll that resumes the consumers waiting in next(). But the producer may not have put a token into the stream before the gatekeeper has arrived. In that case, in addition to the notify, some flag is set to prevent the producer thread from entering the wait block in putIt().