知识大全 环形缓冲器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相关参考
在环形网络中,寄存器插入控制方式的接口装置是怎样组成和工作的?
如图所示,每一节点均附加一个网络接口装置,它包括接收缓冲器RSR、发送缓冲器(TSR)和控制开关SW等部分。当该节点既不发送也不接收信息时,开关置于位置l,环上的信息直接传送到下—节点。当此节点要
在环形网络中,传送的信息首先放入节点的缓冲寄存器内,当网络空闲或发现一个报文的结尾标志时,环路断开并将缓冲寄存器插入环路中。缓冲寄存器在环路中按先进先出方式工作:一面从有关节点输入信息,一面输出信
在环形网络中,传送的信息首先放入节点的缓冲寄存器内,当网络空闲或发现一个报文的结尾标志时,环路断开并将缓冲寄存器插入环路中。缓冲寄存器在环路中按先进先出方式工作:一面从有关节点输入信息,一面输出信
Java中的缓冲区 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 如果将同步I/O方式下的数据传
毕业设计有个远程协助功能得到对方的屏幕后老是会闪很是不爽今天用java的双缓冲技术解决了代码如下本类重写了Swing中的JLabel当Label重绘时会默认的调用它的update方法主要用于清除界
Java程序性能优化-缓冲(Buffer)(1) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
知识大全 Java程序性能优化-缓冲(Buffer)(2)[2]
Java程序性能优化-缓冲(Buffer)(2)[2] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
知识大全 Java程序性能优化-缓冲(Buffer)(2)[1]
Java程序性能优化-缓冲(Buffer)(2)[1] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
JavaIO之有缓冲的文本输入 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 输入就是Input
Java进阶:一个简单Thread缓冲池的实现 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!在应用