User Tools

Site Tools


buffer-java

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
buffer-java [2007/10/04 12:23] franckbuffer-java [2007/10/04 14:03] (current) franck
Line 32: Line 32:
     @param element the element to be added.     @param element the element to be added.
   */   */
-  public void put(Object element)+  public synchronized void put(Object element)
   {   {
     while (this.inCount - this.outCount >= Buffer.SIZE)     while (this.inCount - this.outCount >= Buffer.SIZE)
Line 48: Line 48:
     @return an element of the buffer.     @return an element of the buffer.
   */   */
-  public Object get()+  public synchronized Object get()
   {   {
     while (this.incount - this.outCount <=0)     while (this.incount - this.outCount <=0)
Line 81: Line 81:
   public Consumer(Buffer buffer)   public Consumer(Buffer buffer)
   {   {
 +    super();
     this.buffer = buffer;     this.buffer = buffer;
   }   }
  
   /**   /**
-    +    Gets 20 elements from the buffer and prints them.
   */   */
   public void run()   public void run()
Line 93: Line 94:
     for (int i = 0; i < ITERATION; i++)     for (int i = 0; i < ITERATION; i++)
     {     {
-      System.out.println((Integer) buffer.get());+      System.out.println((Integer) this.buffer.get());
       try       try
       {       {
Line 100: Line 101:
       catch (InterruptedException e){}       catch (InterruptedException e){}
     }     }
 +  }
 +}
 +</code>
 +
 +The class Producer can be implemented as follows.
 +
 +<code java>
 +/**
 +  This class represents a producer.
 +
 +  @author Franck van Breugel
 +
 +*/
 +public class Producer extends Thread
 +{
 +  private Buffer buffer;
 +
 +  /**
 +    Creates a producer for the given buffer.
 +
 +    @param buffer the buffer from which the producer produces.
 +  */
 +  public Producer(Buffer buffer)
 +  {
 +    super();
 +    this.buffer = buffer;
 +  }
 +
 +  /**
 +
 +  */
 +  public void run()
 +  {
 +    final int MILIS_IN_SEC = 1000;
 +    final int ITERATION = 20;
 +    for (int i = 0; i < ITERATION; i++)
 +    {
 +      this.buffer.put(new Integer(i));
 +      try
 +      {
 +        Thread.sleep((long) Math.random() * MILIS_IN_SEC);
 +      } 
 +      catch (InterruptedException e){}
 +    }
 +  }
 +}
 +</code>
 +
 +Finally, we give an app that puts everything together.
 +
 +<code java>
 +/**
 +  This app creates a buffer, a consumer and a producer.
 +
 +  @author Franck van Breugel
 +*/
 +public class BufferTest
 +{
 +  /**
 +    Creates a buffer, consumer and producer.
 +  */
 +  public static void main(String[] args)
 +  {
 +    Buffer buffer = new Buffer();
 +    Producer producer = new Producer(buffer);
 +    Consumer consumer = new Consumer(buffer);
 +    producer.start();
 +    consumer.start();
   }   }
 } }
 </code> </code>
  
buffer-java.1191500586.txt.gz · Last modified: 2007/10/04 12:23 by franck

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki