Results 1 to 1 of 1
- 01-13-2011, 12:28 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
Relay Buffer Flow Design Pattern Problem
Hi all,
I am having a little problem with my relay buffer which uses flow design patterns. The problem is that when the consumer should output the value of the Element, it is printing out "The item is: null". My code is a little bit long, but hopefully you can bare with me, and help me find out why it comes out as null:
Java Code:// Buffer.java public interface Buffer extends Put, Take { public void put ( Element item ) ; public Element take ( ) ; }Java Code:// DummyBuffer.java class DummyBuffer implements Buffer { private Element contents ; private boolean available = false; public synchronized Element take( ) { while ( !available ) { try { wait(); } catch ( InterruptedException e ) { } } available = false ; notifyAll( ) ; return contents ; } public synchronized void put( Element element ) { while ( available ) { try { wait( ) ; } catch ( InterruptedException e ) { } } contents = element ; available = true ; notifyAll() ; } }Java Code:// Element.java class Element extends Object { private Object element ; public Element( ) { super() ; element = new String( "This is an Element" ) ; } public Element( Object e ) { super() ; element = e ; } public String toString() { return element.toString() ; } }Java Code:// EndOfData.java class EndOfData extends Element { private final String DEFAULT_MESSAGE = "TERMINATE" ; private final String message ; // default message version EndOfData ( ) { message = DEFAULT_MESSAGE ; } // message version EndOfData( String message ) { this.message = message ; } public String toString () { return "EndOfData[" + message + "]" ; } }Java Code:// LinearStage.java abstract class LinearStage extends Stage { // 1 "take" input source private final TakeStage in ; // 1 "put" output source private final PutStage out ; public LinearStage( TakeStage in, PutStage out ) { // Stage constructor super() ; this.in = in ; this.out = out ; } public void run ( ) { Element element = in.take() ; // while not end of data retrieve more items while ( notEndOfData( element ) ) { Element processedElement = processData( element ) ; out.put( processedElement ) ; element = in.take() ; } // propagate the "EndOfData" out.put( new EndOfData() ) ; } // Abstract methods // used to indicate when the stage should terminate abstract protected boolean notEndOfData( Element element ) ; // used to ``process'' the data abstract protected Element processData( Element element ) ; }Java Code:// Put.java public interface Put { abstract public void put( Element item ) ; }Java Code:// PutStage.java abstract public class PutStage extends Stage implements Put { public PutStage( ) { // Stage constructor super() ; } }Java Code:// RB_Consumer.java class RB_Consumer extends PutStage { private Element item ; public RB_Consumer( ) { // PutStage's constructor super() ; } public synchronized void put( Element item ) { this.item = item ; } public void run() { // use `item' System.out.println( "The item: " + item ) ; } }Java Code:// RB_Producer.java class RB_Producer extends Stage { private Put nextStage ; public RB_Producer(Put nextStage ) { super() ; this.nextStage = nextStage ; } public void run( ) { Element item = new Element( "An Element" ) ; nextStage.put( item ) ; } }Java Code:// RelayBuffer.java public class RelayBuffer implements Runnable, Put { protected Put consumer ; protected Buffer buffer = new DummyBuffer() ; protected Thread pusher ; public RelayBuffer( PutStage consumer ) { this.consumer = consumer ; // create & start the ``pusher'' thread pusher = new Thread(this) ; pusher.start() ; } public synchronized void put( Element e ) { buffer.put( e ) ; } // pusher thread's body public void run( ) { while ( true ) { // retrieve item from internal buffer Element item = buffer.take() ; // pusher thread `pushes' data to consumer consumer.put( item ) ; } } }Java Code:// RelayBufferSystem.java class RelayBufferSystem { private RelayBuffer relaybuffer ; private Stage producer ; private PutStage consumer ; public RelayBufferSystem( ) { // construct system in right to left order // first create consumer consumer = new RB_Consumer() ; // pass reference to consumer to relaybuffer relaybuffer = new RelayBuffer( consumer ) ; // pass reference to relaybuffer to producer producer = new RB_Producer( relaybuffer ) ; // start threads producer.start() ; consumer.start() ; } }Java Code:// Stage.java class Stage extends Thread { private final String Descriptor = "Flow Design Pattern: Stage" ; public Stage( ) { // Thread constructor super() ; } public String getDescriptor() { return Descriptor ; } }Java Code:// Take.java public interface Take { abstract public Element take( ) ; }Sorry for the long code listing, hopefully you can help me find out why the consumer is printing out null.Java Code:abstract public class TakeStage extends Stage implements Take { public TakeStage( ) { // Stage constructor super() ; } }
Thanks


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks