/* * BoundedBufferCObs.java (Implements Consumer Using Observable/Observer). * * Written by : Nitin Motgi (nmotgi@cs.ucf.edu) * * Portions copyright(c) 2000 to School of Electrical Engineering and * Computer Science, UCF, Orlando. * * Use and distribution of this source code are strictly governed by * terms and conditions set by the authors. * * $Id : BoundedBufferCObs.java, v1.0.0 02/07/2001. $ * * Revision History Revisited: */ import java.lang.*; import java.util.*; /***************************************************************************** * This class acts as observer and it is also observable from the same * point. It also acts as Consumer Thread which notifies every other * producer when there is no item present in the shared buffer. ****************************************************************************/ class BoundedBufferCObs extends Thread implements Observer{ /* Each consumer is given an ID so that the higher level routine can interact with a specific consumer.*/ int nID; /* End condition indicator set to push the Consumer from endlessly producing an item and put in the queue.*/ boolean bEndCondition; /* An indicator for a change in Observable class and hence can run to consume an item from the shared location.*/ boolean bModified; /* Handle to higher class in the hierachy.*/ BoundedBufferObs BBObs; /* This function is constructor for this class.*/ public BoundedBufferCObs(int nID,BoundedBufferObs BBObs){ this.nID = nID; /* Store ID.*/ bEndCondition = true; /* Consume one item */ this.BBObs = BBObs; bModified = false; /* The state of Observable is unchanged.*/ }/* End of BoundedBufferCObs.*/ /* So, when the Observable Object changes it's state it notifies each of its observers by calling their update methods.*/ public void update(Observable ob, Object arg){ ObsState State = (ObsState)arg; /* Get state of observable.*/ int nState = State.Get(); /* State count.*/ switch(nState){ /* When this message is captured it means that producer has terminated so it my turn to consume all the items in the buffer without any interruption.*/ case 3: bModified = true; break; /* This message signals Consumer that buffer is empty and it need not make an attempt to try reading from the shared buffer.*/ case 2: bModified = false; break; /* This signals consumer that there is an item present in queue.*/ case 1: bModified = true; break; /* Signals that consumer has to terminate now.*/ case 4: bEndCondition = false; break; }/* End of Switch.*/ }/* End of update implementation.*/ /* This program is heart of this thread.*/ public void run(){ /* Till the indicator turns false.*/ while(bEndCondition){ if(bModified == true){ BBObs.Get(); /* Indicates that Observable has changed and needs to go and make a check whether buffer is empty.*/ }/* End if.*/ }/* End of while.*/ }/* End of run.*/ }/* End of BoundedBufferCObs.*/