知识大全 环形缓冲器Java实现

Posted

篇首语:满堂花醉三千客,一剑霜寒十四州。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 环形缓冲器Java实现相关的知识,希望对你有一定的参考价值。

环形缓冲器Java实现  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

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

  具体实现如下

  view plaincopy to clipboardprint?

  package app

  public class CircularBuffer

  int bufsize SensorRecord[] store int numberOfEntries = int front = int back =

  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 + if(back ==  bufsize)

  back = numberOfEntries += notify() /** * 取出数据* @return * @throws InterruptedException */ synchronized SensorRecord get() throws InterruptedException SensorRecord result = new SensorRecord( ) if( == numberOfEntries )

  wait() result = store[front] System out println( get + result toString()) front += if(front == bufsize)

  front = numberOfEntries = notify() return result

  

   package app

  public class CircularBuffer

  int bufsize SensorRecord[] store int numberOfEntries = int front = int back =

  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 + if(back ==  bufsize)

  back = numberOfEntries += notify() /** * 取出数据* @return * @throws InterruptedException */ synchronized SensorRecord get() throws InterruptedException SensorRecord result = new SensorRecord( ) if( == numberOfEntries )

  wait() result = store[front] System out println( get + result toString()) front += if(front == bufsize)

  front = numberOfEntries = notify() return result

  

  

  完整代码如下(仅供学习参考)

  view plaincopy to clipboardprint?

  package app

  public class BufferPool

  public static CircularBuffer  buf = new CircularBuffer( )

  

  package app

  public class Get implements Runnable

  public void run() while (true) try Thread sleep( ) 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() * ) int degree = (int) (Math random() * ) SensorRecord rec = new SensorRecord(num degree) try Thread sleep( ) BufferPool buf put(rec) catch (InterruptedException e) // TODO Auto generated catch block e printStackTrace()

  

  package app

  public class SensorRecord

  public SensorRecord(int num int degree ) // TODO Auto generated constructor stub this num = num this degree = degree

  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 thread = new Thread(put) thread start() thread start()

   package app

  public class BufferPool

  public static CircularBuffer  buf = new CircularBuffer( )

  

  package app

  public class Get implements Runnable

  public void run() while (true) try Thread sleep( ) 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() * ) int degree = (int) (Math random() * ) SensorRecord rec = new SensorRecord(num degree) try Thread sleep( ) BufferPool buf put(rec) catch (InterruptedException e) // TODO Auto generated catch block e printStackTrace()

  

  package app

  public class SensorRecord

  public SensorRecord(int num int degree ) // TODO Auto generated constructor stub this num = num this degree = degree

  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 thread = new Thread(put) thread start() thread start()

cha138/Article/program/Java/hx/201311/25728

相关参考