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/03 20:10] franckbuffer-java [2007/10/04 14:03] (current) franck
Line 4: Line 4:
  
 <code java> <code java>
 +/**
 +  This class represents a bounded buffer.
 +
 +  @author Franck van Breugel
 +*/
 public class Buffer public class Buffer
 { {
Line 12: Line 17:
   private int outCount;   private int outCount;
  
 +  /**
 +    Creates an empty buffer.
 +  */
   public Buffer()   public Buffer()
   {   {
Line 19: Line 27:
   }   }
  
-  public void put(Object element)+  /** 
 +    Puts the given element in this buffer. 
 + 
 +    @param element the element to be added. 
 +  */ 
 +  public synchronized void put(Object element)
   {   {
 +    while (this.inCount - this.outCount >= Buffer.SIZE)
 +    {
 +      this.wait();
 +    }
     this.buffer[this.inCount % Buffer.SIZE] = element;     this.buffer[this.inCount % Buffer.SIZE] = element;
     this.inCount++;     this.inCount++;
 +    this.notifyAll();
   }   }
  
-  public Object get()+  /** 
 +    Removes an element from the buffer and returns it. 
 + 
 +    @return an element of the buffer. 
 +  */ 
 +  public synchronized Object get()
   {   {
-    Object temp = buffer[outCount % SIZE]; +    while (this.incount - this.outCount <=0) 
-    outCount++;+    { 
 +      this.wait(); 
 +    } 
 +    Object temp = this.buffer[this.outCount % Buffer.SIZE]; 
 +    this.outCount++
 +    this.notifyAll();
     return temp;     return temp;
   }   }
 +}
 +</code>
  
 +The class Consumer can be implemented as follows.
  
 +<code java>
 +/**
 +  This class represents a consumer.
  
 +  @author Franck van Breugel
 +*/
 +public class Consumer extends Thread
 +{
 +  private Buffer buffer;
 +
 +  /**
 +    Creates a consumer for the given buffer.
 +
 +    @param buffer the buffer from which the consumer consumes.
 +  */
 +  public Consumer(Buffer buffer)
 +  {
 +    super();
 +    this.buffer = buffer;
 +  }
 +
 +  /**
 +    Gets 20 elements from the buffer and prints them.
 +  */
 +  public void run()
 +  {
 +    final int MILIS_IN_SEC = 1000;
 +    final int ITERATION = 20;
 +    for (int i = 0; i < ITERATION; i++)
 +    {
 +      System.out.println((Integer) this.buffer.get());
 +      try
 +      {
 +        Thread.sleep((long) Math.random() * MILIS_IN_SEC);
 +      } 
 +      catch (InterruptedException e){}
 +    }
 +  }
 +}
 </code> </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>
 +
buffer-java.1191442229.txt.gz · Last modified: 2007/10/03 20:10 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki