技术中心

这里象征着我们的态度和能力

>环形缓冲器Java实现
发布者:中国IT实验室    信息来源:中国IT实验室    发布时间:2012-01-09      浏览次数:4957
分享到:

新浪微博

腾讯微博

QQ空间

豆瓣网

QQ好友

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  在数据采取时,经常用户缓冲器来暂时存放数据,显然,此时一定要有一个相互排斥机制以防止生产者和消费者进程同时对这个缓冲器中的同一个元素进行存取。同时,系统还要确保缓冲器已满时生产者进程不再试着往里添加信息,消费者进程在缓冲器为空时也不去取信息。

  具体实现如下:

  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();

  }

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730