User Tools

Site Tools


buffer-java

This is an old revision of the document!


Bounded buffer in Java

The class Buffer can be implemented as follows.

public class Buffer
{
  private static final int SIZE = 10;
 
  private Object[] buffer;
  private int inCount;
  private int outCount;
 
  public Buffer()
  {
    this.buffer = new Object[Buffer.SIZE];
    this.inCount = 0;
    this.outCount = 0;
  }
 
  public void put(Object element)
  {
    this.buffer[this.inCount % Buffer.SIZE] = element;
    this.inCount++;
  }
 
  public Object get()
  {
    Object temp = buffer[outCount % SIZE];
    outCount++;
    return temp;
  }
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