当前位置:编程学习 > JAVA >>

环形缓冲器Java实现

在数据采取时,经常用户缓冲器来暂时存放数据,显然,此时一定要有一个相互排斥机制以防止生产者和消费者进程同时对这个缓冲器中的同一个元素进行存取。同时,系统还要确保缓冲器已满时生产者进程不再试着往里添加信息,消费者进程在缓冲器为空时也不去取信息。
  具体实现如下:
  view plaincopy to clipboardprint?
  package app;
  public class CircularBuffer {
  int bufsize;SensorRecord[] store;int numberOfEntries = 0;int front = 0;int back = 0;
  CircularBuffer(int n){ bufsize = n;store =  new SensorRecord[bufsize];
  }
  /** * 存放数据* @param rec要存放的数据对象* @throws InterruptedException */ synchronized void put(SensorRecord rec) throws InterruptedException{ if(numberOfEntries == bufsize)
  wait();store[back] = new SensorRecord(rec.num, rec.degree);System.out.println("put " + rec.toString());back = back + 1;if(back ==  bufsize)
  back = 0;numberOfEntries += 1;notify();} /** * 取出数据* @return * @throws InterruptedException */ synchronized SensorRecord get() throws InterruptedException{ SensorRecord result = new SensorRecord(-1,-1);if( 0 == numberOfEntries )
  wait();result = store[front];System.out.println("get " + result.toString());front += 1;if(front == bufsize)
  front = 0;numberOfEntries -= 1;notify();return result;
  }
  } package app;
  public class CircularBuffer {
  int bufsize;SensorRecord[] store;int numberOfEntries = 0;int front = 0;int back = 0;
  CircularBuffer(int n){ bufsize = n;store =  new SensorRecord[bufsize];
  }
  /** * 存放数据* @param rec要存放的数据对象* @throws InterruptedException */ synchronized void put(SensorRecord rec) throws InterruptedException{ if(numberOfEntries == bufsize)
  wait();store[back] = new SensorRecord(rec.num, rec.degree);System.out.println("put " + rec.toString());back = back + 1;if(back ==  bufsize)
  back = 0;numberOfEntries += 1;notify();} /** * 取出数据* @return * @throws InterruptedException */ synchronized SensorRecord get() throws InterruptedException{ SensorRecord result = new SensorRecord(-1,-1);if( 0 == numberOfEntries )
  wait();result = store[front];System.out.println("get " + result.toString());front += 1;if(front == bufsize)
  front = 0;numberOfEntries -= 1;notify();return result;
  }
  }
  完整代码如下(仅供学习参考):
  view plaincopy to clipboardprint?
  package app;
  public class BufferPool {
  public static CircularBuffer  buf = new CircularBuffer(100);
  }
  package app;
  public class Get implements Runnable {
  public void run() { while (true) { try { Thread.sleep(1000);BufferPool.buf.get();} catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();}
  }
  package app;
  public class Put implements Runnable {
  public void run() { while (true) { int num = (int) (Math.random() * 1000);int degree = (int) (Math.random() * 1000);SensorRecord rec = new SensorRecord(num, degree);try { Thread.sleep(10);BufferPool.buf.put(rec);} catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();}
  }
  package app;
  public class SensorRecord {
  public SensorRecord(int num2, int degree2) { // TODO Auto-generated constructor stub this.num = num2;this.degree = degree2;}
  int num;int degree;
  public String  toString(){ return new String("num: " + num  + "; degree: " + degree);}
  }
  package app;
  public class TestBuffer {
  /** * @param args */
  public static void main(String[] args) { Get get = new Get();Put put = new Put();Thread thread = new Thread(get);Thread thread2 = new Thread(put);thread.start();thread2.start();
  } package app;
  public class BufferPool {
  public static CircularBuffer  buf = new CircularBuffer(100);
  }
  package app;
  public class Get implements Runnable {
  public void run() { while (true) { try { Thread.sleep(1000);BufferPool.buf.get();} catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();}
  }
  package app;
  public class Put implements Runnable {
  public void run() { while (true) { int num = (int) (Math.random() * 1000);int degree = (int) (Math.random() * 1000);SensorRecord rec = new SensorRecord(num, degree);try { Thread.sleep(10);BufferPool.buf.put(rec);} catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();}
  }
  package app;
  public class SensorRecord {
  public SensorRecord(int num2, int degree2) { // TODO Auto-generated constructor stub this.num = num2;this.degree = degree2;}
  int num;int degree;
  public String  toString(){ return new String("num: " + num  + "; degree: " + degree);}
  }
  package app;
  public class TestBuffer {
  /** * @param args */
  public static void main(String[] args) { Get get = new Get();Put put = new Put();Thread thread = new Thread(get);Thread thread2 = new Thread(put);thread.start();thread2.start();
  }

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,